2016-06-14 19 views
11

我試圖使用PHP API添加在列表中的聯繫人,但其拋出波紋管段錯誤如何添加使用列表中的聯繫人(發送網格)PHP API

字符串(51)「{」錯誤「: {「message」:「請求正文無效」}]}「{」email「:」[email protected]「,」first_name「:」hh「,」last_name「:」User「}

我正在使用波紋管代碼片段:

$url = 'https://api.sendgrid.com/v3'; 
$request = $url.'/contactdb/lists/12345/recipients'; //12345 is list_id 
$params = array(
'email' => '[email protected]', 
'first_name' => 'hh', 
'last_name' => 'User' 
); 
$json_post_fields = json_encode($params); 
// Generate curl request 
$ch = curl_init(); 
$headers = 
array("Content-Type: application/json", 
"Authorization: Bearer SG.XXXXXXXX"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Apply the JSON to our curl call 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_post_fields); 
$data = curl_exec($ch); 
if (curl_errno($ch)) { 
print "Error: " . curl_error($ch); 
} else { 
// Show me the result 
var_dump($data); 
curl_close($ch); 
} 
echo $json_post_fields; 

任何人都可以告訴我如何解決是問題。

+0

您可以使用 curl_setopt($ CH,CURLOPT_USERPWD, 「用戶名:密碼」); //你的憑證在這裏 –

+0

在$ params = array中是否使用了正確的值(array('section? – Murali

+0

@Murali值在數組部分是正確的 –

回答

8

您應該檢查您發送的請求,並將正文中的JSON與有效的請求進行比較,以真正瞭解發生了什麼。 json_encode的輸出將是一個數組,但API需要一個對象。你的請求主體必須

[{"email":"[email protected]","first_name":"hh","last_name":"User"}] 

什麼你現在正在做正確的派遣

{"email":"[email protected]","first_name":"hh","last_name":"User"} 

可以解決這個問題的幾種方法。你可以使用你最喜歡的字符串操作函數來附加括號,或者你可以跳過編碼並將JSON作爲字符串發送(因爲你指定了內容類型)。

+0

正如你所說,我已經改變了我的代碼,現在我可以使用$ request = $ url添加聯繫人列表。'/ contactdb/recipients';但我仍然無法使用{list_id} $ request = $ url。'/ contactdb/lists/12345/recipients'在特定列表中添加聯繫人; // 12345是list_id這是新代碼的鏈接http://pastebin.com/ErPkVaNa –

+0

正如[文檔]中所述(https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add -a-Single-Recipient-to-a-List-POST),要添加到特定的列表中,必須提供'recipient_id'。收件人與列表有一對多的關係。 – bwest

+0

$ url。'/ contactdb/lists/123/recipients/ddfg123我在代碼中使用但仍顯示「消息」:「沒有提供有效的收件人」 –

2

在查看sendgrid API並在我自己的服務器上測試後,我能夠將聯繫人添加到聯繫人列表中。由於您已經創建了列表,下一步是創建要添加到列表中的收件人。你可以這樣做

<?php 

$url = 'https://api.sendgrid.com/v3/'; 
$request = $url.'contactdb/recipients'; //12345 is list_id 
$params = array(array(
'email' => '[email protected]', 
'first_name' => 'Amit', 
'last_name' => 'Kumar' 
)); 
$json_post_fields = json_encode($params); 
// Generate curl request 
$ch = curl_init(); 
$headers = 
array("Content-Type: application/json", 
"Authorization: Bearer SG.000000"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Apply the JSON to our curl call 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_post_fields); 
$data = curl_exec($ch); 
if (curl_errno($ch)) { 
print "Error: " . curl_error($ch); 
} else { 
// Show me the result 
curl_close($ch); 
} 
var_dump($data); 
?> 

創建收件人後,你現在可以將它們添加到列表中。你會得到一個像這樣的id YW1pdGtyYXlAZ21haWwuY29t這是你的電子郵件ID的base64編碼。

<?php 

$url = 'https://api.sendgrid.com/v3/'; 
$request = $url.'contactdb/lists/12345/recipients/YW1pdGtyYXlAZ21haWwuY29t'; //12345 is list_id 

// Generate curl request 
$ch = curl_init(); 
$headers = 
array("Content-Type: application/json", 
"Authorization: Bearer SG.00000000"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Apply the JSON to our curl call 
$data = curl_exec($ch); 
if (curl_errno($ch)) { 
print "Error: " . curl_error($ch); 
} else { 
// Show me the result 
curl_close($ch); 
} 
var_dump($data); 
?> 

補充說,你可以驗證,如果用戶已添加到列表

<?php 

$url = 'https://api.sendgrid.com/v3/'; 
$request = $url.'contactdb/lists/12345/recipients?page_size=100&page=1'; //12345 is list_id 

// Generate curl request 
$ch = curl_init(); 
$headers = 
array("Content-Type: application/json", 
"Authorization: Bearer SG.000000"); 
curl_setopt($ch, CURLOPT_GET, true); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Apply the JSON to our curl call 
$data = curl_exec($ch); 
if (curl_errno($ch)) { 
print "Error: " . curl_error($ch); 
} else { 
// Show me the result 

curl_close($ch); 
} 
var_dump($data); 
?> 

後注:最好的辦法就是因爲大部分代碼被重複創建一個類。我將爲sendgrid做一個包裝類,然後在這裏發佈它,並且能夠通過sendgrid API執行所有可能的任務。

+0

好的,我會嘗試這個 –

+0

非常感謝你「你的電子郵件ID是base64編碼」我很難在文檔中發現這個事實。 – sfscs

0

第一個想到的是你需要插入您的電子郵件發送網收件人:

<?php 

    $curl = curl_init(); 

     $email = "[email protected]"; 

    curl_setopt_array($curl, array(

     CURLOPT_URL => "https://api.sendgrid.com/v3/contactdb/recipients", 

     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_ENCODING => "", 
     CURLOPT_MAXREDIRS => 10, 
     CURLOPT_SSL_VERIFYPEER => false, 
     CURLOPT_TIMEOUT => 30, 
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
     CURLOPT_CUSTOMREQUEST => "POST", 
     CURLOPT_POSTFIELDS => "[{\"email\": \".$email.\"}]", 
     CURLOPT_HTTPHEADER => array(
     "authorization: Bearer SG.0000000", 
     "content-type: application/json" 
    ), 
    )); 

    $response = curl_exec($curl); 
    $err = curl_error($curl); 

    curl_close($curl); 

    if ($err) { 
     echo "cURL Error #:" . $err; 
    } else { 
     echo $response; 
     $a = json_decode($response); 
     $b = $a->persisted_recipients; //get id of email 
     $r_id = $b[0];    // store it 

    } 

    ?> 

後,通過這樣的方式在列表中插入它。

$curl = curl_init(); 


curl_setopt_array($curl, array(

    CURLOPT_URL => "https://api.sendgrid.com/v3/contactdb/lists/123456/recipients/$r_id", 

    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "POST", 
    CURLOPT_POSTFIELDS => "null", 
    CURLOPT_HTTPHEADER => array(
    "authorization: Bearer SG.0000000000", 
    "content-type: application/json" 
), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 

更多信息,請訪問:https://sendgrid.com/docs/API_Reference/api_v3.html

+0

歡迎來到SO。請閱讀[如何回答](http://stackoverflow.com/help/how-to-answer)。 – thewaywewere