【问题标题】:SendGrid Add new Contact to marketing contact list using php curlSendGrid 使用 php curl 将新联系人添加到营销联系人列表
【发布时间】:2019-11-14 18:04:23
【问题描述】:

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

我正在尝试遵循此文档,但没有成功: 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"

      ),
    ));

【问题讨论】:

    标签: php json curl sendgrid


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      不必处理所有反斜杠和转义的一个小技巧是在 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.
      }
      
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-15
        • 1970-01-01
        相关资源
        最近更新 更多