Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP curl request to API

I have a call to an API using Javascript which works like a charm:

var headers = {
  Authorization: "Bearer " + MY_ACCESS_TOKEN
};

var requestParams = {
  method: "POST",
  contentType: "application/json",
  headers: headers,
  payload: JSON.stringify({
    query: 'query {adSet(id: "' + MY_ADSET_ID + '") {insights(timeRange: {from: "2023-08-01T00:00:00Z", until: "2023-08-10T23:59:59Z"}timeIncrement: DAILY) {timestamps reports {impressions conversions offerwallImpressions offerwallAverageRank spend}}}}'
  })

var response = UrlFetchApp.fetch(MY_API_ENDPOINT, requestParams);
var data = JSON.parse(response);

When I try to convert this to PHP I simply get a blank repsonse from the API. What am I doing wrong?
Note that my_curl () works correctly when I use it to retrieve $MY_ACCESS_TOKEN from the API.

$postdata = json_encode('query: query {adSet(id: "' . $MY_ADSET_ID . '") {insights(timeRange: {from: "2023-08-01T00:00:00Z", until: "2023-08-10T23:59:59Z"} timeIncrement: DAILY) {timestamps reports {impressions conversions offerwallImpressions offerwallAverageRank spend}}}}');
$endpoint = $MY_API_ENDPOINT;
$headers = array (
    "Content-Type: application/json",
    "Authorization: Bearer " . $MY_ACCESS_TOKEN,
    $postdata
);

$data = my_curl ($endpoint, $headers);
var_dump ($data);

function my_curl ($endpoint, $headers) {
    $ch = curl_init ();

    curl_setopt ($ch, CURLOPT_URL, $endpoint);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt ($ch, CURLOPT_HEADER, false);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    curl_close ($ch);

    $json = json_decode ($server_output, true);

    return ($json);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you’re using json_encode, you should provide an associative array or an object to encode into JSON, not a string. In your case, you’re providing a string,You are mixing the headers and the post data in the $headers array.I have added the missing CURLOPT_POSTFIELDS option to include the JSON-encoded post data in the cURL request, allowing the API to receive the query correctly.

<?php
$MY_ACCESS_TOKEN = "your_access_token";
$MY_ADSET_ID = "your_adset_id";
$MY_API_ENDPOINT = "your_api_endpoint";

$postdata = json_encode([
    'query' => 'query {adSet(id: "' . $MY_ADSET_ID . '") {insights(timeRange: {from: "2023-08-01T00:00:00Z", until: "2023-08-10T23:59:59Z"} timeIncrement: DAILY) {timestamps reports {impressions conversions offerwallImpressions offerwallAverageRank spend}}}}'
]);

$endpoint = $MY_API_ENDPOINT;
$headers = array(
    "Content-Type: application/json",
    "Authorization: Bearer " . $MY_ACCESS_TOKEN
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close($ch);

$json = json_decode($server_output, true);

var_dump($json);
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading