2017-05-21 113 views
0

所以我一直在试图找出如何管理这几天,但没有那么远,Sendgrid卷曲认证GET API

这里是我的问题:我试图检索JSON来自SendGrid GET API的对象。 - >https://api.sendgrid.com/v3/suppression/bounces/

这就需要卷曲认证,为支撑告诉我:

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

所以,因为我是很新,卷曲,我不能设法与该检索DATAS:

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

谢谢! :)

+0

您是否尝试过: '$反弹= json_decode($响应,真正的);'? –

+0

我终于通过了,谢谢 – naspy971

回答

1

尝试使用卷曲

$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":"[email protected]","reason":"550 5.4.1 [[email protected]]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] ","status":"5.4.1"},{"created":1494945503,"email":"[email protected]","reason":"550 5.4.1 [[email protected]]: 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] => [email protected] 
      [reason] => 550 5.4.1 [[email protected]]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] 
      [status] => 5.4.1 
     ) 

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

) 

[email protected] 
[email protected] 
+0

感谢您的回答,我真的想通了,我不得不这样把网址:https://api.sendgrid.com/v3/suppression/bounces没有'/'在最后。但是现在,当我执行curl_exec时,我在页面中获得原始json数据,但这不是我想要的,我实际上想要json解码结果并对其进行foreach,但是当我尝试$ response = curl_exec($ ch)时;和$ foreach($ response as $ key => $ value) { \t \t echo $ value ['email']。'
'; } 我收到警告:为/ foreach()中的/ Users/gb/Desktop/MAMP/array 2.php提供的无效参数第13行 – naspy971

+0

您能否提供JSON响应示例..你把它解码为对象还是数组? –

+0

我回答了你作为上述响应 – naspy971

-1

[{ “创建”:1495048030, “电子邮件”: “[email protected]”, “理由”:“550 5.4.1 [[email protected]] :收件人地址被拒绝:访问被拒绝 [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] “,”status“:”5.4.1“},{”created“:1494945503,”email“:”squestgel @ exelcia-it.com“,”reason“:”550 5.4.1 [[email protected]]:收件人地址被拒绝:访问被拒绝[DB5EUR01FT004.eop-EUR01.prod.protection.outlook.com] “,”status“:”5.4.1“}]

我也试过$ response = json_decode(curl_exec($ ch),true);和foreach这个响应,但仍为foreach()提供的无效参数。

顺便说一句,目前我试图通过这个foreash循环,但最终我会使用1 curl请求,并会做一个array_merge,如果我可以。

1

我设法做我想做的事:

//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>'; 
    }