naspy971 naspy971 -4 years ago 100
PHP Question

Sendgrid cURL authentication GET API in php

So I've been trying to figure out how to manage this for a few days but nothing so far...

Here's my problem : I'm trying to retrieve a json object from a SendGrid GET API.
-> https://api.sendgrid.com/v3/suppression/bounces/

Which needs cURL authentication, as the support told me :

curl --request GET \
--url https://api.sendgrid.com/v3/suppression/bounces/
--header 'authorization: Bearer YOUR_API_KEY'
--header 'Content-Type: application/json'


So since I'm pretty new to cURL, I can't manage to retrieve the datas with this :

$ch = curl_init('https://api.sendgrid.com/v3/suppression/bounces/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

$bounces = json_decode(file_get_contents($response),true);


I get this error :

Warning: file_get_contents({"errors":[{"field":null,"message":"resource not found"}]}): failed to open stream: No such file or directory in ...


Thanks ! :)

Answer Source

Try to use the curl

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/suppression/bounces/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

$headers = array();
$headers[] = "Authorization: Bearer YOUR_API_KEY";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Edited based on your JSON response I could try to parse with these

$response = '[{"created":1495048030,"email":"dfezfezfezfezfezfez@exelcia-it.com","reason":"550 5.4.1 [dfezfezfezfezfezfez@exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] ","status":"5.4.1"},{"created":1494945503,"email":"squestgel@exelcia-it.com","reason":"550 5.4.1 [squestgel@exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT004.eop-EUR01.prod.protection.outlook.com] ","status":"5.4.1"}]';
$json = json_decode($response , true) ; 
echo '<pre>'.print_r($json, true).'</pre>';
foreach ($json as $data) {
    echo $data['email'].'<br>';
}

And here is the result I got.

        Array
(
    [0] => Array
        (
            [created] => 1495048030
            [email] => dfezfezfezfezfezfez@exelcia-it.com
            [reason] => 550 5.4.1 [dfezfezfezfezfezfez@exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] 
            [status] => 5.4.1
        )

    [1] => Array
        (
            [created] => 1494945503
            [email] => squestgel@exelcia-it.com
            [reason] => 550 5.4.1 [squestgel@exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT004.eop-EUR01.prod.protection.outlook.com] 
            [status] => 5.4.1
        )

)

dfezfezfezfezfezfez@exelcia-it.com
squestgel@exelcia-it.com
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download