亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP curl請求API
P粉311089279
P粉311089279 2023-08-13 18:12:22
0
1
439
<p>我有一個使用JavaScript呼叫API的函數(shù),並且運作得很好:</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: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>當我嘗試將其轉(zhuǎn)換為PHP時,我只從API中得到一個空白的回應(yīng)。我做錯了什麼? </p><p> 請注意,當我使用my_curl()從API檢索$MY_ACCESS_TOKEN時,它的工作正常。 </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粉311089279
P粉311089279

全部回覆(1)
P粉513318114

當使用json_encode時,應(yīng)該提供一個關(guān)聯(lián)數(shù)組或一個要編碼為JSON的對象,而不是一個字串。在你的情況下,你提供了一個字串,你在$headers陣列中混合了頭部和post資料。我已經(jīng)添加了缺少的CURLOPT_POSTFIELDS選項,以便在cURL請求中包含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);
?>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板