【发布时间】:2019-02-20 11:26:42
【问题描述】:
我正在尝试使用 Yandex.Direct API (https://api.direct.yandex.com/json/v5/reports) 获取报告数据,但出现以下错误:
Trying to get property 'result' of non-object
我的参数:
$params = array(
'SelectionCriteria' => array(
'DateFrom' => $startDate,
'DateTo' => $endDate,
'Filter' => array(array(
'Field' => 'CampaignId',
'Operator' => 'EQUALS',
'Values' => array($campaign->getId())
))
),
'FieldNames' => array('Date', 'CriterionId'),
'ReportName' => 'Yandex actual report',
'ReportType' => 'CUSTOM_REPORT',
'DateRangeType' => 'CUSTOM_DATE',
'Format' => 'TSV',
'IncludeDiscount' => 'YES',
'IncludeVAT' => 'NO'
);
然后我将这个数组传递给下一个方法:
$data = $this->client->call("https://api.direct.yandex.com/json/v5/reports", "get", $params);
还有函数本身:
public function call($url, $method, $params, $headers = []){
$client = new \GuzzleHttp\Client();
$query = [
'method' => $method,
'params' => $params
];
$defHeaders = array_merge([
'Content-type' => 'application/json; charset=utf-8',
'Authorization'=> "Bearer ".$this->token,
],$headers);
$res = $client->request('POST', $url, [
'json' => $query,
'headers' => $defHeaders
]);
$res = json_decode($res->getBody()->getContents());
if(isset($res->error)){
throw new YdException($res->error->error_string, $res->error->error_code, $res->error->error_detail);
}
return $res->result;
}
当我 var_dump($res) 时,我得到 NULL 值。那我做错了什么?
【问题讨论】:
-
“当我 var_dump($res)” - 在什么时候?请不要在 json_decode 行之后说……因为那你当然应该首先检查
$res->getBody()->getContents()返回的内容。 -
在函数调用中,您将“GET”定义为方法,但在您的函数中定义“POST”。这是故意的吗?考虑到您要发送带有参数的正文这一事实,我猜想 yandex API 需要 POST...
-
@04FS 我检查了 $res->getBody()->getContents() 返回的内容 - 它返回一个数组。
-
那么 json_decode 想要一个字符串作为第一个参数,而不是一个数组。
-
@04FS 字符串上的相同问题。
标签: php json request yandex yandex-api