search

Home  >  Q&A  >  body text

PHP curl request API

<p>I have a function that calls the API using JavaScript and it works fine: </p> <p><br /></p> <pre class="brush:js;toolbar:false;">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);</pre> <p><br /></p> <p>When I try to convert this to PHP, I just get a blank response from the API. What did i do wrong? </p><p> Please note that when I use my_curl() to retrieve $MY_ACCESS_TOKEN from the API, it works fine. </p> <pre class="brush:php;toolbar:false;">$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); } </pre> <p><br /></p>
P粉311089279P粉311089279566 days ago378

reply all(1)I'll reply

  • P粉513318114

    P粉5133181142023-08-14 09:32:51

    When using json_encode, you should provide an associative array or an object to be encoded to JSON, not a string. In your case you are providing a string and you are mixing the headers and post data in the $headers array. I've added the missing CURLOPT_POSTFIELDS option to include 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);
    ?>

    reply
    0
  • Cancelreply