【问题标题】:Sendgrid cURL authentication GET API in phpphp中的Sendgrid cURL认证GET API
【发布时间】:2017-05-21 13:40:41
【问题描述】:

所以我这几天一直在想办法解决这个问题,但到目前为止还没有……

这是我的问题:我正在尝试从 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 ...

谢谢! :)

【问题讨论】:

  • 你试过了吗:$bounces = json_decode($response, true);
  • 我终于熬过去了,谢谢

标签: php rest curl sendgrid


【解决方案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":"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

【讨论】:

  • 感谢您的回答,我真的想通了,我不得不这样输入网址:api.sendgrid.com/v3/suppression/bounces,最后没有'/'。但是现在我在执行 curl_exec 时会在页面中获取原始 json 数据,但这不是我想要的,我实际上想对结果进行 json 解码并对其进行 foreach,但是当我尝试 $response = curl_exec($ch);和 foreach($response as $key => $value) { echo $value['email'].'
    ';我收到警告:在第 13 行的 /Users/gb/Desktop/MAMP/array 2.php 中为 foreach() 提供的参数无效
  • 你能提供 JSON 响应的例子吗。你是把它解码为一个对象还是一个数组?
  • 我在上面回复了你
  • 请根据您的问题更新您现有的 cURL 代码
  • 没关系,我将您的解决方案与 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);这允许我获取数据,现在我必须弄清楚如何从 2 个 url 获取数据,我想有一些 curl 函数可以做到这一点。谢谢大佬!
【解决方案2】:

我成功地做了我想做的事:

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

【讨论】:

    猜你喜欢
    • 2015-10-31
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多