PHP curl请求API
P粉311089279
P粉311089279 2023-08-13 18:12:22
[PHP讨论组]

我有一个使用JavaScript调用API的函数,运行得很好:


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:59Z"}timeIncrement: DAILY) {timestamps reports {impressions conversions offerwallImpressions offerwallAverageRank spend}}}}'
  })

var response = UrlFetchApp.fetch(MY_API_ENDPOINT, requestParams);
var data = JSON.parse(response);


当我尝试将其转换为PHP时,我只从API得到一个空白的响应。我做错了什么?

请注意,当我使用my_curl()从API检索$MY_ACCESS_TOKEN时,它的工作正常。

$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);
}


P粉311089279
P粉311089279

全部回复(1)
P粉513318114

当使用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);
?>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号