Reputation: 2088
I am trying to add contact in list using php api but its throwing bellow snippet error
string(51) "{"errors":[{"message":"request body is invalid"}]} " {"email":"[email protected]","first_name":"hh","last_name":"User"}
I am using bellow snippet code:
$url = 'https://api.sendgrid.com/v3';
$request = $url.'/contactdb/lists/12345/recipients'; //12345 is list_id
$params = array(
'email' => '[email protected]',
'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;
Can any one tell me how to resolve this issue.
Upvotes: 16
Views: 11110
Reputation: 11
First think is you will need to insert your email to send grid recipients:
<?php
$curl = curl_init();
$email = "[email protected]";
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
}
?>
after that insert it in list by doing this way.
$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;
}
for more info visit:https://sendgrid.com/docs/API_Reference/api_v3.html
Upvotes: 0
Reputation: 3485
After looking at sendgrid API and then testing on my own server I was able to add contacts to the contact list. As you already created a list next step is to create recipients to be added to the list. You can do this way
<?php
$url = 'https://api.sendgrid.com/v3/';
$request = $url.'contactdb/recipients'; //12345 is list_id
$params = array(array(
'email' => '[email protected]',
'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);
?>
After creating recipients you can now add them to list. You will get an id like this YW1pdGtyYXlAZ21haWwuY29t which is base64 encode of your email id.
<?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);
?>
After adding that you can verify if user has been added to list
<?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);
?>
Note: best way is to create a class as most of the codes are being repeated. I will make a wrapper class for sendgrid and post it here soon with ability to do all task that is possible through sendgrid API.
Upvotes: 5
Reputation: 9814
You should inspect the request that you are sending and compare the JSON in the body to a valid request to really see what's happening. The output of your json_encode
here will be an array, but the API expects an object. Your request body needs to be
[{"email":"[email protected]","first_name":"hh","last_name":"User"}]
And what you are doing right now is sending
{"email":"[email protected]","first_name":"hh","last_name":"User"}
You can fix this several ways. You could use your favorite string manipulation functions to append the brackets, or you could skip the encoding and send the JSON as a string (since you're specifying the content type).
Upvotes: 9