recherche

Maison  >  Questions et réponses  >  le corps du texte

API de requête PHP curl

<p>J'ai une fonction qui appelle l'API en utilisant JavaScript et elle fonctionne très bien : </p> <p><br /></p> <pre class="brush:js;toolbar:false;">var en-têtes = { Autorisation : "Porteur" + MY_ACCESS_TOKEN } ; var requêteParams = { méthode : "POST", contentType : "application/json", en-têtes : en-têtes, charge utile : JSON.stringify({ requête : 'query {adSet(id : "' + MY_ADSET_ID + '") {insights(timeRange : {from : "2023-08-01T00:00:00Z", jusqu'à : "2023-08-10T23:59:59Z" }timeIncrement : DAILY) {rapports d'horodatage {impressions conversions offerwallImpressions offerwallAverageRank dépenses}}}}' }) var réponse = UrlFetchApp.fetch(MY_API_ENDPOINT, requestParams); var data = JSON.parse(response);</pre> <p><br /></p> <p>Lorsque j'essaie de convertir ceci en PHP, je reçois simplement une réponse vide de l'API. Qu'ai-je fait de mal? </p><p> Veuillez noter que lorsque j'utilise my_curl() pour récupérer $MY_ACCESS_TOKEN depuis l'API, cela fonctionne correctement. </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", jusqu'à : "2023-08-10T23:59:59Z"} timeIncrement : DAILY) {rapports d'horodatage {impressions conversions offerwallImpressions offerwallAverageRank dépenses}}}}'); $point de terminaison = $MY_API_ENDPOINT ; $en-têtes = tableau ( "Type de contenu : application/json", "Autorisation : Porteur " $MY_ACCESS_TOKEN, $postdonnées ); $data = my_curl ($endpoint, $headers); var_dump ($données); fonction my_curl ($endpoint, $headers) { $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, $endpoint); curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_HTTPPHEADER, $headers); curl_setopt ($ch, CURLOPT_HEADER, false); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, vrai); $server_output = curl_exec($ch); curl_close ($ch); $json = json_decode ($server_output, true) ; retourner ($json); } ≪/pré> <p><br /></p>
P粉311089279P粉311089279566 Il y a quelques jours381

répondre à tous(1)je répondrai

  • P粉513318114

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

    Lorsque vous utilisez json_encode, vous devez fournir un tableau associatif ou un objet à encoder en JSON, pas une chaîne. Dans votre cas, vous fournissez une chaîne et vous mélangez les en-têtes et les données de publication dans le tableau $headers. J'ai ajouté l'option CURLOPT_POSTFIELDS manquante pour inclure les données de publication codées JSON dans les requêtes cURL, permettant ainsi à l'API de recevoir correctement la requête.

    <?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);
    ?>

    répondre
    0
  • Annulerrépondre