PHP curl请求循环没有输出

时间:2016-04-04 15:12:29

标签: php json curl

我在PHP中构建一个循环,将curl请求发布到服务器。我发送JSON并希望返回一个报告,该报告输出JSON中包含的参数。我的代码如下:

$json = '{"report":{"special_pixel_reporting":false,"report_type":"network_analytics","timezone":"EST5EDT","start_date":"2016-03-01 00:00:00","end_date":"2016-04-01 00:00:00","filters":[{"seller_member_id":["273"]}],"columns":["imps","cost"],"row_per":[],"pivot_report":false,"fixed_columns":[],"show_usd_currency":false,"orders":["imps","cost"],"name":"Network Analytics Report - 04\/4\/2016","ui_columns":["imps","cost"],"filter_objects":[]}}';

foreach ($results as $k => $v) {
$ch = curl_init('https://api.appnexus.com/report');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization:'.$v['token'],
  'Content-Type: application/json'));
$output = curl_exec($ch);
var_dump($output);
};

其中$ results是一个包含身份验证令牌的2D数组(我将其用作迭代器)。我希望为每个令牌返回一个输出,但无法从服务器获得响应。

基本上我试图在PHP中复制这个curl请求。

curl -X POST -d '{"report":{"special_pixel_reporting":false,"report_type":"network_analytics","timezone":"EST5EDT","start_date":"2016-03-01 00:00:00","end_date":"2016-04-01 00:00:00","filters":[{"seller_member_id":["273"]}],"columns":["imps","cost"],"row_per":[],"pivot_report":false,"fixed_columns":[],"show_usd_currency":false,"orders":["imps","cost"],"name":"Network Analytics Report - 04\/4\/2016","ui_columns":["imps","cost"],"filter_objects":[]}}' --header "Authorization:[AUTH HERE]" https://api.appnexus.com/report 

有没有人对我的PHP代码为什么不返回任何输出有任何建议?

1 个答案:

答案 0 :(得分:0)

也许您应该使用curl_error()

检查卷曲错误
<?php
$json = '{"report":{"special_pixel_reporting":false,"report_type":"network_analytics","timezone":"EST5EDT","start_date":"2016-03-01 00:00:00","end_date":"2016-04-01 00:00:00","filters":[{"seller_member_id":["273"]}],"columns":["imps","cost"],"row_per":[],"pivot_report":false,"fixed_columns":[],"show_usd_currency":false,"orders":["imps","cost"],"name":"Network Analytics Report - 04\/4\/2016","ui_columns":["imps","cost"],"filter_objects":[]}}';

foreach ($results as $k => $v) {
    $ch = curl_init('https://api.appnexus.com/report');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Authorization:'.$v['token'],
      'Content-Type: application/json'));

    $output = curl_exec($ch);

    if($output === false){
        echo 'Curl error: ' . curl_error($ch);
    }
    else{
        var_dump($output);
    }
    curl_close($ch);
}