国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP-Curl-Anfrage-API
P粉311089279
P粉311089279 2023-08-13 18:12:22
0
1
444
<p>Ich habe eine Funktion, die die API mithilfe von JavaScript aufruft, und sie funktioniert einwandfrei: </p> <p><br /></p> <pre class="brush:js;toolbar:false;">var headers = { Autorisierung: ?Bearer“ + MY_ACCESS_TOKEN }; var requestParams = { Methode: ?POST“, contentType: ?application/json“, überschriften: überschriften, Nutzlast: JSON.stringify({ query: 'query {adSet(id: "' + MY_ADSET_ID + '") {insights(timeRange: {from: "2023-08-01T00:00:00Z", bis: "2023-08-10T23:59:59Z" }timeIncrement: DAILY) {timestamps reporting {Impressions Conversions offerwallImpressions offerwallAverageRank spend}}}}' }) var Response = UrlFetchApp.fetch(MY_API_ENDPOINT, requestParams); var data = JSON.parse(response);</pre> <p><br /></p> <p>Wenn ich versuche, dies in PHP zu konvertieren, erhalte ich nur eine leere Antwort von der API. Was habe ich falsch gemacht? </p><p> Bitte beachten Sie, dass es einwandfrei funktioniert, wenn ich my_curl() verwende, um $MY_ACCESS_TOKEN von der API abzurufen. </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", bis: "2023-08-10T23:59:59Z"} timeIncrement: DAILY) {timestamps reporting {impressions Conversions offerwallImpressions offerwallAverageRank spend}}}}'); $endpoint = $MY_API_ENDPOINT; $headers = Array ( ?Inhaltstyp: application/json“, ?Autorisierung: Inhaber“ $MY_ACCESS_TOKEN, $postdata ); $data = my_curl ($endpoint, $headers); var_dump ($data); Funktion 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粉311089279
P粉311089279

Antworte allen(1)
P粉513318114

當(dāng)使用json_encode時(shí),應(yīng)該提供一個(gè)關(guān)聯(lián)數(shù)組或一個(gè)要編碼為JSON的對(duì)象,而不是一個(gè)字符串。在你的情況下,你提供了一個(gè)字符串,你在$headers數(shù)組中混合了頭部和post數(shù)據(jù)。我已經(jīng)添加了缺失的CURLOPT_POSTFIELDS選項(xiàng),以便在cURL請(qǐng)求中包含JSON編碼的post數(shù)據(jù),使API能夠正確接收查詢。

<?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);
?>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage