简体   繁体   中英

PHP curl request loop no output

I'm building a loop in PHP to post curl requests to a server. I'm sending JSON and hoping to return a report which outputs parameters included in the JSON. My code is as follows:

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

where $results is a 2D array containing the authentication token (which I'm using as an iterator). I'm expecting to return an output for each token but can't get a response from the server.

Essentially I'm trying to replicate this curl request in PHP.

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 

Does anyone have any advice as to why my PHP code won't return any output?

Perhaps you should check for curl errors with 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM