0

我必须创建一个 PHP 脚本,它将在我的 sendinblue 数据库中创建新联系人。Sendinblue 有一个 API,其中包含一个 PHP 代码生成器来完成此操作。该文档位于:https ://developers.sendinblue.com/reference#createcontact

这是我的代码:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sendinblue.com/v3/contacts",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "{\"email\":\"testCreateContact@test.com\",\"emailBlacklisted\":true,\"smsBlacklisted\":false,\"listIds\":[2],\"updateEnabled\":true}",
    CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "api-key: *****",
        "content-type: application/json"
    ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

现在我正在寻找如何attributes使用 POST 请求发送字段,因为您无法attributes在文档页面上添加代码创建器:在此处输入图像描述

所以我不明白attributes在我的代码中包含该字段的语法。谢谢!

编辑:所以现在我尝试了这部分代码:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);


curl_setopt($curl, CURLOPT_URL, "https://api.sendinblue.com/v3/contacts");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, [
    'email' => 'testCreateContact@test.com',
    'emailBlacklisted' => true,
    'smsBlacklisted' => false,
    'listIds' => [2],
    'updateEnabled' => true,
    'attributes' => json_encode([
        "FNAME" => "Elly",  //ERROR LINE
        "LNAME" => "Roger",
    ]),
  ]);

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "accept: application/json",
    "api-key: ****",
    "content-type: application/json"
  ));

但它仍然不起作用,我得到了这个错误: Array to string conversion +这个: {"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}

文档说属性必须是一个对象,所以我真的不知道该怎么做。文档非常好,但他们在这里遗漏了一些东西......(没有例子)

4

2 回答 2

2

从它的外观来看,只需将其添加到 POST 字段中即可CURLOPT_POSTFIELDS。然而,除了几个简单的真/假声明之外,尝试操作 JSON 编码的字符串很快就会变得混乱,尤其是在处理 3d 数组并担心在 PHP 中转义引号时。我建议您改为使用 PHP 数组来简化事情,这也将更容易attributes在您的 POST 请求中包含该字段。

编辑:从远程服务器返回的错误消息来看,他们的 API 要求将 POST 数据作为 JSON 对象而不是标准'para1=val1&para2=val2&...'格式发送,因此您只需对整个 POST 数组进行 JSON 编码以创建一个JSON编码对象:

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
    'email' => 'testCreateContact@test.com',
    'emailBlacklisted' => true,
    'smsBlacklisted' => false,
    'listIds' => [2],
    'updateEnabled' => true,
    'attributes' => [
        'FNAME' => 'Elly',
        'LNAME' => 'Roger',
    ],
]));

或者如果你想使用curl_setopt_array(),你可以这样做:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_URL => 'https://api.sendinblue.com/v3/contacts',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode([
        'email' => 'testCreateContact@test.com',
        'emailBlacklisted' => true,
        'smsBlacklisted' => false,
        'listIds' => [2],
        'updateEnabled' => true,
        'attributes' => [
            'FNAME' => 'Elly',
            'LNAME' => 'Roger',
        ],
    ]),
    CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'api-key: *****',
        'content-type: application/json'
    ),
));
于 2019-12-09T23:22:29.500 回答
0

我找到了一个可行的解决方案:

  CURLOPT_URL => "https://api.sendinblue.com/v3/contacts",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"attributes\":{\"NOM\":\"james\"},\"email\":\"testCreateContact@test.com\",
  \"emailBlacklisted\":false,\"smsBlacklisted\":false,\"listIds\":[2],\"updateEnabled\":true}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "api-key: ****",
    "content-type: application/json"
  ),
));```

Thx
于 2019-12-10T11:06:43.140 回答