P粉5133181142023-08-14 09:32:51
當使用json_encode時,應該提供一個關聯數組或一個要編碼為JSON的對象,而不是一個字串。在你的情況下,你提供了一個字串,你在$headers陣列中混合了頭部和post資料。我已經添加了缺少的CURLOPT_POSTFIELDS選項,以便在cURL請求中包含JSON編碼的post數據,使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); ?>