PHP 컬 요청 API
<p>JavaScript를 사용하여 API를 호출하는 함수가 있는데 잘 작동합니다. </p>
<p><br /></p>
<pre class="brush:js;toolbar:false;">var 헤더 = {
승인: "전달자" + MY_ACCESS_TOKEN
};
var requestParams = {
메소드: "POST",
콘텐츠 유형: "응용 프로그램/json",
헤더: 헤더,
페이로드: JSON.stringify({
쿼리: '쿼리 {adSet(id: "' + MY_ADSET_ID + '") {insights(timeRange: {from: "2023-08-01T00:00:00Z", Until: "2023-08-10T23:59:59Z" }timeIncrement: 매일) {타임스탬프는 {노출 전환 OfferwallImpressions OfferwallAverageRank 지출}}}}을 보고합니다.
})
var response = UrlFetchApp.fetch(MY_API_ENDPOINT, requestParams);
var data = JSON.parse(response);</pre>
<p><br /></p>
<p>이것을 PHP로 변환하려고 하면 API에서 빈 응답만 받습니다. 내가 뭘 잘못했나요? </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", 기한: "2023-08-10T23:59:59Z"} timeIncrement: DAILY) {타임스탬프 보고서 {노출 전환 OfferwallImpressions OfferwallAverageRank 지출}}}}');
$끝점 = $MY_API_ENDPOINT;
$헤더 = 배열(
"콘텐츠 유형: 애플리케이션/json",
"권한 부여: 전달자 " .
$postdata
);
$data = my_curl($endpoint, $headers);
var_dump($data);
함수 my_curl($endpoint, $headers) {
$ch = 컬_초기화();
컬_setopt($ch, CURLOPT_URL, $endpoint);
컬_setopt($ch, CURLOPT_POST, 1);
컬_setopt($ch, CURLOPT_HTTPHEADER, $headers);
컬_setopt($ch, CURLOPT_HEADER, false);
cur_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = 컬_exec($ch);
컬_닫기($ch);
$json = json_decode($server_output, 참);
반환($json);
}
</pre>
<p><br /></p>