2016-04-12 14 views
1

Sitede oturum açtıklarında, gönderim yapmak için tek bir alıcı eklemek istiyorum. Eklendikten sonra, kullanıcıya e-posta gönderir ve bunları bir listeye eklerim.PHP ile SendGrid v3 API'sine Alıcı Ekleme

Ancak kullanıcıyı SendGrid'e eklerken sorun yaşıyorum.

https://api.sendgrid.com/v3/contactdb/recipients

add_user_new($email); 
function add_user_new($email) { 

$url = 'https://api.sendgrid.com/v3/contactdb/recipients'; 

$params =array(array(
    //'name' => 'this is a reserved field', 
    'email'=> '[email protected]' 
)); 

$json_post_fields = json_encode($params); 

// Generate curl request 
$ch = curl_init($request); 

$headers = array("Authorization: Bearer api_key_here"); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
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; 

} 

En fazla bu yanıttır, emin değilim ne kaçırıyorum:

Onların belgeleri (https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add-Recipients-POST) burada onların ayrıntılarını POST gereken bir kullanıcı eklemek için söylüyor. JSON geçersiz biçimlendirilmiş olduğundan, söyledikleri hata.

{"name":"[email protected]"} 
+0

daki $ json_post_fields değişkeni do kodlayın:

string(51) "{"errors":[{"message":"request body is invalid"}]} 

Bu benim JSON benziyor şeydir. Bir istek gövdesi yok, "list_id" ve "recipient_id" URL’de. Ve hiç bir 'isim 'parametresi yok ... – bwest

+0

@bwest Üzgünüz, belgelerdeki yanlış çizgiye bağlandım. "Add Recantents POST", list_id veya recipient_id için istekte bulunmaz, çünkü siz SendGrid'e henüz kimliğinizi eklemediniz, bu yüzden bir listeye eklemeden önceki adım budur. –

+0

Ahh Anladım. Bu durumda, en azından 'e-posta' anahtarını vücutta sağlamanız gerekir. – bwest

cevap

-1

yine dokümanlar bir göz atın JSON biçiminde

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_post_fields));