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

wixjitnu  于 2023-02-11  发布在  PHP
关注(0)|答案(3)|浏览(151)

嗨,我试图使用我的网站上的自定义表单添加一个新的联系人到我们的营销列表,每个联系人将包含一个电子邮件和名字。
我尝试遵循此文档,但没有成功: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"

      ),
    ));
58wvjzkj

58wvjzkj1#

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

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

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

2admgd59

2admgd592#

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

  • 创建一个对象,使用json_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.
}

?>
oprakyz7

oprakyz73#

这是最好的办法:

$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 => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POSTFIELDS =>'{"list_ids": ["list-ID"], "contacts": [{"email": "' . $_GET["email"] . '"}]}',
    CURLOPT_HTTPHEADER => array(
        ': ',
        'Authorization: Bearer SG.000000000',
        'Content-Type: application/json'
    ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    echo $response;
    die();

相关问题