SendGrid使用php curl将新联系人添加到营销联系人列表

时间:2019-11-14 18:04:23

标签: php json curl sendgrid

您好,我想在我的网站上使用自定义表单将新联系人添加到我们的营销列表中,每个联系人将包含一封电子邮件和名字。

我正在尝试遵循此文档,但没有成功: https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

我尝试使用他们的工具,但是它总是说不正确的JSON,但是当我使用在线验证器时,它说的正确,我不知道如何匹配他们的工具才能发布请求。

这是我当前的代码:

   $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"

      ),
    ));

2 个答案:

答案 0 :(得分:0)

您只应在}之后添加$first_name个字符。
这是有效的JSON:

  CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"}]}",

您可以通过https://jsonformatter.curiousconcept.com/验证者网站进行检查。

答案 1 :(得分:0)

一个不必处理所有反斜杠和转义的小技巧是在 PHP 中使用预定义的对象字面量,通过使用前面带有 (object) 的数组来定义嵌套在结构中的对象。

  • 创建一个对象,使用 json_encode 将其转换为 JSON
  • 将编码的 JSON 对象输入到 cURL 请求中的 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.
}


?>