Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SendGrid使用php curl将新联系人添加到营销联系人列表_Php_Json_Curl_Sendgrid - Fatal编程技术网

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

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

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

我试图遵循此文档,但没有成功:

我试过使用他们的工具,但它总是说不正确的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"

      ),
    ));

您只需在
$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请求的
    CURLOPT_POSTFIELDS
    中输入编码的JSON对象


<?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.
}


?>