Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
如何使用(发送网格)php api在列表中添加联系人_Php_Email_Curl_Sendgrid - Fatal编程技术网

如何使用(发送网格)php api在列表中添加联系人

如何使用(发送网格)php api在列表中添加联系人,php,email,curl,sendgrid,Php,Email,Curl,Sendgrid,我试图使用PHPAPI在列表中添加联系人,但下面的代码段出错 字符串(51)“{”错误“:[{”消息“:“请求正文无效”}]}“{”电子邮件“:”hello@test.com“,”姓“:”hh“,”姓“:”用户“} 我正在使用以下代码片段: $url = 'https://api.sendgrid.com/v3'; $request = $url.'/contactdb/lists/12345/recipients'; //12345 is list_id $params = array(

我试图使用PHPAPI在列表中添加联系人,但下面的代码段出错

字符串(51)“{”错误“:[{”消息“:“请求正文无效”}]}“{”电子邮件“:”hello@test.com“,”姓“:”hh“,”姓“:”用户“}

我正在使用以下代码片段:

$url = 'https://api.sendgrid.com/v3';
$request =  $url.'/contactdb/lists/12345/recipients';  //12345 is list_id
$params = array(
'email' => 'hello@test.com',
'first_name' => 'hh', 
'last_name' => 'User'
  );
$json_post_fields = json_encode($params);
// Generate curl request
$ch = curl_init();
$headers = 
array("Content-Type: application/json",
"Authorization: Bearer SG.XXXXXXXX");
curl_setopt($ch, CURLOPT_POST, true);   
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Apply the JSON to our curl call
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_post_fields);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
echo $json_post_fields;

有谁能告诉我如何解决这个问题。

您应该检查发送的请求,并将正文中的JSON与有效请求进行比较,以真正了解发生了什么。这里的
json\u encode
的输出将是一个数组,但API需要一个对象。您的请求主体需要

[{"email":"hello@test.com","first_name":"hh","last_name":"User"}]
你现在正在做的是发送

{"email":"hello@test.com","first_name":"hh","last_name":"User"}

你可以用几种方法解决这个问题。您可以使用自己喜欢的字符串操作函数添加括号,也可以跳过编码并将JSON作为字符串发送(因为您正在指定内容类型)。

查看sendgrid API,然后在我自己的服务器上进行测试后,我能够将联系人添加到联系人列表中。由于您已经创建了一个列表,下一步是创建要添加到列表中的收件人。你可以这样做

<?php

$url = 'https://api.sendgrid.com/v3/';
$request =  $url.'contactdb/recipients';  //12345 is list_id
$params = array(array(
'email' => 'amitkray@gmail.com',
'first_name' => 'Amit',
'last_name' => 'Kumar'
));
$json_post_fields = json_encode($params);
// Generate curl request
$ch = curl_init();
$headers = 
array("Content-Type: application/json",
"Authorization: Bearer SG.000000");
curl_setopt($ch, CURLOPT_POST, true);   
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Apply the JSON to our curl call
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_post_fields);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
curl_close($ch);
}
var_dump($data);
?>

创建收件人后,现在可以将其添加到列表中。您将获得一个类似于YW1PDGTYXLAZ21HAWWUY29T的id,它是您电子邮件id的base64编码

<?php

$url = 'https://api.sendgrid.com/v3/';
$request =  $url.'contactdb/lists/12345/recipients/YW1pdGtyYXlAZ21haWwuY29t';  //12345 is list_id

// Generate curl request
$ch = curl_init();
$headers = 
array("Content-Type: application/json",
"Authorization: Bearer SG.00000000");
curl_setopt($ch, CURLOPT_POST, true);   
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Apply the JSON to our curl call
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
curl_close($ch);
}
var_dump($data);
?>

添加后,您可以验证用户是否已添加到列表中

<?php

$url = 'https://api.sendgrid.com/v3/';
$request =  $url.'contactdb/lists/12345/recipients?page_size=100&page=1';  //12345 is list_id

// Generate curl request
$ch = curl_init();
$headers = 
array("Content-Type: application/json",
"Authorization: Bearer SG.000000");
curl_setopt($ch, CURLOPT_GET, true);   
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Apply the JSON to our curl call
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result

curl_close($ch);
}
var_dump($data);
?>


注意:最好的方法是创建一个类,因为大多数代码都是重复的。我将为sendgrid创建一个包装类,并很快将其发布到此处,以便能够通过sendgrid API完成所有可能的任务。

首先考虑的是,您需要插入电子邮件以向grid收件人发送邮件:

    <?php

    $curl = curl_init();

      $email = "example@mail.com";

    curl_setopt_array($curl, array(

      CURLOPT_URL => "https://api.sendgrid.com/v3/contactdb/recipients",

      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "[{\"email\": \".$email.\"}]",
      CURLOPT_HTTPHEADER => array(
        "authorization: Bearer SG.0000000",
        "content-type: application/json"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
        $a = json_decode($response);
        $b = $a->persisted_recipients; //get id of email 
        $r_id  = $b[0];                // store it

     }

    ?>

更多信息请访问:

您可以使用curl_setopt($ch,CURLOPT_USERPWD,“用户名:密码”)//您的凭证将显示在此处,
$params=array(array)中是否使用了正确的值(
section?@Murali值在数组部分是正确的。您没有正确进行身份验证。请参阅此处的文档@bwest您能提供示例代码或就我的问题提供一些答案吗?正如您所说,我已经更改了代码,现在我可以使用$request=$url在列表中添加联系人。'/contactdb/recipients';但我仍然无法添加contac使用{list_id}的特定列表中的t$request=$url./contactdb/lists/12345/recipients';/12345是列表id这是新代码的链接,如图所示,要添加到特定列表中,您必须提供一个
收件人id
。收件人与列表有一对多关系。$url./contactdb/lists/123/recipients/ddfg123我在代码中使用过,但仍然显示“消息”:“未提供有效的收件人”URL很好。你的有效负载是错误的。请阅读我发布的链接。收件人id会出现在有效负载中,而不是电子邮件、姓名等。列表id会保留在URL中。你收到的错误消息完全正确,因为你没有提供收件人id。请在请求更多帮助之前彻底阅读文档。我已经尝试过了,但仍然没有l我无法在特定列表中添加联系人,您能否在粘贴栏或当前答案中填写详细代码。欢迎使用SO。请阅读此内容。感谢您提供的“您的电子邮件id的base64编码”我在文档中很难找到此事实。
$curl = curl_init();


curl_setopt_array($curl, array(

  CURLOPT_URL => "https://api.sendgrid.com/v3/contactdb/lists/123456/recipients/$r_id", 

  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "null",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer SG.0000000000",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}