San*_*ate 13 php email curl sendgrid
我正在尝试使用php api在列表中添加联系人,但它正在抛出波纹管代码片段错误
string(51)"{"errors":[{"message":"请求正文无效"}]}"{"email":"hello@test.com","first_name":"hh","last_name" :"用户"}
我使用的是以下代码段:
$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;
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何解决这个问题.
您应该检查您要发送的请求,并将正文中的JSON与有效请求进行比较,以便真正了解正在发生的情况.这里的输出json_encode将是一个数组,但API需要一个对象.您的要求机构需要
[{"email":"hello@test.com","first_name":"hh","last_name":"User"}]
Run Code Online (Sandbox Code Playgroud)
而你现在正在做的是发送
{"email":"hello@test.com","first_name":"hh","last_name":"User"}
Run Code Online (Sandbox Code Playgroud)
您可以通过多种方式解决此问题 您可以使用您喜欢的字符串操作函数来附加括号,或者您可以跳过编码并将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);
?>
Run Code Online (Sandbox Code Playgroud)
创建收件人后,您现在可以将他们添加到列表中。你会得到一个像 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);
?>
Run Code Online (Sandbox Code Playgroud)
添加后,您可以验证用户是否已添加到列表中
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
注意:最好的方法是创建一个类,因为大多数代码都在重复。我将为 sendgrid 制作一个包装类,并很快将其发布在这里,以便能够通过 sendgrid API 完成所有可能的任务。