0

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?

2
  • If you do it without foreach do you get the same thing? Commented Apr 4, 2016 at 15:14
  • its recommended to use CURLOPT_VERBOSE option for debugging.If you do not have PHP error that should help you to get more info. Commented Apr 5, 2016 at 6:15

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Curl_error() was able to show there was a problem getting an SSL certificate so just changed protocol from https to http and it works! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.