2016-04-12 35 views
1

私が探しているのは、自分のサイトにサインアップするときにsendgridに単一の受信者を追加することです。追加が終わると、ユーザーに電子メールを送信してリストに追加します。Sendgrid v3 APIにPHPを使って受信者を追加する方法

しかし、私はSendgridにユーザーを追加する際に問題があります。

彼らのドキュメント(https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html#Add-Recipients-POSTは)あなたがここにその詳細をPOSTする必要があるユーザーを追加すると言う:

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; 

} 

これは私が、私が欠けているかわからない得る応答です。彼らが言うエラーは、JSONの形式が正しくないためです。

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

これは私のJSONは次のようになります。

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

に再びドキュメントを見てみましょうか、あなたの$ json_post_fields変数をエンコードします。リクエストボディは存在せず、 'list_id'と' recipient_id'がURLに入ります。そして、名前パラメータはまったくありません... – bwest

+0

@bwest申し訳ありませんが、私はドキュメントの間違った行にリンクしました。 "Add Recipients POST"は、あなたがまだSendgridにIDを持っていないので、list_idまたはrecipient_idを要求しないので、これをリストに追加する前のステップです。 –

+0

ああそうです。その場合、最低限、あなたは 'email'キーを本体に用意する必要があります。 – bwest

答えて

-1

はJSON形式

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_post_fields)); 
関連する問題