-1

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?

tohhy
  • 145
  • 1
  • 3
  • 11
  • 1
    _“When I var_dump($res)”_ - at which point? Please don’t say after the json_decode line … because then you should have of course checked what `$res->getBody()->getContents()` returns in the first place. – 04FS Feb 20 '19 at 11:32
  • In the function call, you define 'GET' as method, yet in your function you define 'POST'. Is this intentionally? Considering the fact that you're sending a body with parameters, I would have guessed that the yandex API expects POST... – Erik Reder Feb 20 '19 at 11:35
  • @04FS I checked what $res->getBody()->getContents() returns - it returns an array. – tohhy Feb 20 '19 at 11:45
  • Well json_decode wants a string as first parameter, not an array. – 04FS Feb 20 '19 at 11:49
  • @04FS The same issue on string. – tohhy Feb 20 '19 at 12:15
  • And “on string” means what now, _exactly_? If you have a string value that looks like JSON, and you still get null after json_decode, then your next step should be to see what json_last_error has to say. – 04FS Feb 20 '19 at 12:49
  • @04FS my json_last_error returns 4, that means that it is JSON_ERROR_SYNTAX, right? – tohhy Feb 20 '19 at 12:55
  • Yes. (`var_dump(JSON_ERROR_SYNTAX);` can show you the value behind such a constant when in question.) – 04FS Feb 20 '19 at 12:58
  • @04FS If I type var_dump(JSON_ERROR_SYNTAX), then it returns int(4). – tohhy Feb 20 '19 at 13:35
  • (Yes, that was what I was saying, that you can get the numeric value this constant represents that way, if there was any doubt. Not really relevant to your problem, so don’t focus on this minor aspect now.) – 04FS Feb 20 '19 at 13:38
  • @04FS The problem is that the request returns TSV format, and there is no property called 'result', that's why I get the error. – tohhy Feb 20 '19 at 14:10
  • TSV is tab-separated values, that is a format similar to CSV … no clue what made you think trying to use json_decode on that would make sense even remotely in the first place. – 04FS Feb 20 '19 at 14:14

1 Answers1

0

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

tohhy
  • 145
  • 1
  • 3
  • 11