phpjsonrequestyandexyandex-api

Null in GET request from Yandex.Direct


I'm trying to get reports data using Yandex.Direct API (https://api.direct.yandex.com/json/v5/reports) and I get the following error:

Trying to get property 'result' of non-object

My params:

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

Then I pass this array to the next method:

$data = $this->client->call("https://api.direct.yandex.com/json/v5/reports", "get", $params);

And the function itself:

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

When I var_dump($res), I get NULL value. So what am I doing wrong?


Solution

  • The report is returned in TSV format, not JSON. That's why this problem appeared.