在Yandex.Direct的GET请求中为空

问题描述 投票:-1回答:1

我正在尝试使用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值。那么我做错了什么?

php json request yandex yandex-api
1个回答
0
投票

报告以TSV格式返回,而不是JSON。这就是出现这个问题的原因。

© www.soinside.com 2019 - 2024. All rights reserved.