SendGrid使用php curl将新联系人添加到营销联系人列表
您好,我正在尝试使用我的网站上的自定义表单添加一个新的联系人到我们的营销列表,每个联系人将包含一封电子邮件和名字 我试图遵循此文档,但没有成功: 我试过使用他们的工具,但它总是说不正确的JSON,但当我使用在线验证器时,它说正确,我不知道如何匹配他们的工具来让我的请求发布 这是我当前的代码:SendGrid使用php curl将新联系人添加到营销联系人列表,php,json,curl,sendgrid,Php,Json,Curl,Sendgrid,您好,我正在尝试使用我的网站上的自定义表单添加一个新的联系人到我们的营销列表,每个联系人将包含一封电子邮件和名字 我试图遵循此文档,但没有成功: 我试过使用他们的工具,但它总是说不正确的JSON,但当我使用在线验证器时,它说正确,我不知道如何匹配他们的工具来让我的请求发布 这是我当前的代码: $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.send
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"]}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer SG.OCFHb********P3iuikQ.bqKdM-da7X609ZNo9UT7y*********u5fDlfQo80o",
"content-type: application/json"
),
));
您只需在
$first\u name
后面添加}
字符即可这是有效的JSON:
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"}]}",
您可以在validator网站上查看它。不必处理所有反斜杠和转义的一个小技巧是在PHP中使用预定义的对象文字,通过使用前面带有(object)的数组来定义嵌套在结构中的对象
- 制作一个对象,使用
JSON\u encode将其转换为JSON
- 在cURL请求的
中输入编码的JSON对象CURLOPT_POSTFIELDS
<?php
// make custom fields object for entry into the cURL later on:
// THERE IS NO OBJECT LITERAL IN PHP, but using (object) in front of an array, voila
$contact_info = (object)[
"list_ids" => [
"eeee-eeee-eeee-eeee-eeeeexample" // array of list ids here.
],
"contacts" => [ // this is an array of objects (array[object]), according to the api-docs.
(object)[
"email" => "email@example.com",
"country" => "the country",
"city" => "the city",
"custom_fields" => (object)[
"e1_T" => "sign-up-page-name", // only custom fields use ids as the key.
"e2_T" => "next-custom-field" // keep adding custom fields in this array.
]
]
]
];
// now all we have to do is to encode the object into JSON:
$json_contact_data = json_encode($contact_info);
// now add the contact:
$curl = curl_init();
// update the contact
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json_contact_data, // here we enter the pre-configured json from above.
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $your_api_key . "",
"content-type: application/json"
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$decoded = json_decode($response, true); // decode into associative array.
if ($err) {
echo "cURL Error #: " . $err;
} else {
print_r($decoded); // print the decoded json message.
}
?>