Sendgrid cURL 身份验证 GET API in php.



所以我几天来一直在试图弄清楚如何管理这个问题,但到目前为止什么都没有......

这是我的问题:我正在尝试从SendGrid GET API检索json对象。-> https://api.sendgrid.com/v3/suppression/bounces/

这需要cURL身份验证,正如支持告诉我的那样:

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

因此,由于我对cURL很陌生,因此我无法使用此方法检索数据:

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

我收到此错误:

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

谢谢 ! :)

尝试使用 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);

编辑根据您的 JSON 响应,我可以尝试使用这些

解析
$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>';
}

这是我得到的结果。

        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

我设法做了我想做的事情:

 //opening curl sessions
    $ch1 = curl_init('https://api.sendgrid.com/v3/suppression/bounces');
    $ch2 = curl_init('https://api.sendgrid.com/v3/suppression/invalid_emails');
    //setting options to ch1
    curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
    //setting options to ch2
    curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
    //exec 2 curl sessions
    $resp1 = curl_exec($ch1);
    $resp2 = curl_exec($ch2);
//Merging 2 curl arrays and converting into json
    $array = json_encode(array_merge(json_decode($resp1, true),json_decode($resp2, true)));
//Decoding result
    $arr = json_decode($array, true);
    foreach ($arr as $data) {
        echo $data['email'].'<br>';
    }

最新更新