Sendinblue:将联系人添加到列表时,电子邮件格式无效

时间:2019-06-13 09:18:00

标签: php sendinblue

使用Sendinblue API,我得到一个Bad Request响应:{“ code”:“ invalid_parameter”,“ message”:“无效的电子邮件格式”} when I'm trying to use the function AddContactToList`。

尝试创建联系人时,我没有问题。联系人随电子邮件和属性一起发送。我现在要在列表中添加现有联系人。

这是将联系人添加到列表页面的代码:

// Configure API key authorization: api-key
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'xxxxxxxxxxxxxxxxxx');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api-key', 'Bearer');
// Configure API key authorization: partner-key
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'xxxxxxxxxxxxxxxxxxxxxxx');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('partner-key', 'Bearer');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);

$row = 0;
if (($handle = fopen("contactlist.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {

            $row++;

            usleep(1000);

            if($row>70){

                $listId = 42; // int | Id of the list
                $contactEmails = new \SendinBlue\Client\Model\AddContactToList(); // \SendinBlue\Client\Model\AddContactToList | Emails addresses of the contacts
                $contactEmails['emails'] =$data[1];
                echo $data[1];echo '<br>';
                try {
                    $result = $apiInstance->addContactToList($listId, $contactEmails);
                    print_r($result);
                } catch (Exception $e) {
                    echo 'Exception when calling ContactsApi->addContactToList: ', $e->getMessage(), PHP_EOL;
                }

                echo '<br>';echo '<br>';echo '<br>';
            }

            if($row==400){
                die();
            }
    }
    fclose($handle);
}

?>

这是我得到的结果:

xxxxx@example.com 调用ContactsApi-> addContactToList时发生异常:

  

[400]客户端错误:POST   https://api.sendinblue.com/v3/contacts/lists/42/contacts/add个结果   在400错误请求响应中:   {“代码”:“ invalid_parameter”,“消息”:“无效的电子邮件格式”}

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是我的工作代码:

require_once(__DIR__ . '/vendor/autoload.php');

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR-APIKEY');
$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', 'YOUR-APIKEY');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
  new GuzzleHttp\Client(),
  $config
);

$createAttribute = new \SendinBlue\Client\Model\CreateAttribute([
  'name'      => 'Your Name',
  'type'      => 'email' // email, sms
]);

$createContact = new \SendinBlue\Client\Model\CreateContact([
  'email' => 'test@email.be',
  'attributes' => $createAttribute,
  'listIds' => [28], // Int() | Number of your list in your account
]); // \SendinBlue\Client\Model\CreateContact | Values to create a contact

try {
  $result = $apiInstance->createContact($createContact);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling ContactsApi->createContact: ', $e->getMessage(), PHP_EOL;
}