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

时间:2016-06-14 07:08:04

标签: php email curl sendgrid

我正在尝试使用php api在列表中添加联系人但是它正在抛出波纹管片段错误

  

字符串(51)" {"错误":[{"消息":"请求正文无效"}]}" {"电子邮件":" hello@test.com","如first_name":" HH""姓氏&#34 ;: "用户"}

我正在使用以下代码段:

$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;

任何人都可以告诉我如何解决这个问题。

3 个答案:

答案 0 :(得分:9)

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

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

你现在正在做的是发送

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

您可以通过以下几种方式解决此问题。您可以使用自己喜欢的字符串操作函数来附加括号,也可以跳过编码并将JSON作为字符串发送(因为您要指定内容类型)。

答案 1 :(得分:4)

在查看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);
?>

创建收件人后,您现在可以将其添加到列表中。您将获得一个类似于YW1pdGtyYXlAZ21haWwuY29t的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完成所有任务。

答案 2 :(得分:0)

首先想一想,您需要插入电子邮件以发送网格收件人:

    <?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 = 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;
}

了解更多信息,请访问:https://sendgrid.com/docs/API_Reference/api_v3.html