试图获取非对象属性的 Guzzle 响应



我有一个我正在尝试处理的cron命令,我已经意识到我对Guzzle并不像我希望的那样熟悉。但这是我命令的一部分:

foreach($osdClaimResponses as $ocr){
$client = new GuzzleHttpClient(['headers' => ['Accept' => 'application/json', 'Authorization' => 'XXXXXXXXXXXXXXXXXXXXXXX']]);
$response = $client->request('GET', $endpoint, ['query' => [
'events' => $events, 
'transmissions' => $ocr->transmission_id,
]]);
$body = $response->getBody()->getContents();
$this->info($body);
if($body->total_count > 0){
foreach($body->results as $result){
$this->info($result);
if($result->rcpt_to == $ocr->destination){
$ocr->initial_open_at = CarbonCarbon::parse($result->timestamp);
$ocr->initial_open_ip_address = $result->ip_address;
$ocr->save();
}
}
}
}

现在,初始请求效果很好,我能够在我可以适当地看到响应的行中看到对我的请求的适当响应(我将在下面发布$this->info($body)。但是,如果我转到下一行,则会收到一个错误,指出Trying to get property 'total_count' of non-object.

正如您在下面的返回中看到的,有一个名为total_count的属性,那么我做错了什么?

{
"results":[
{
"template_version                                           ":"0",
"friendly_from":"XXXXX",
"subject":"[DRAFT] SXAL Claim",
"ip_pool":"shared",
"sending_domain":"XXXXX",
"                                           rcpt_tags":[
],
"type":"open",
"raw_rcpt_to":"XXXXX",
"msg_from":"XXXXX",
"rcpt_to":"XXXXX",
"geo_ip":{
"zip":80216,
"continent":"NA",
"country":"US",
"city":"Denv                                           er",
"latitude":39.7845,
"region":"CO",
"postal_code":"80216",
"longitude":XXXXX
},
"transmission_id":"XXXXX",
"user_agent":"Outlook-iOS/711.3102771.p                                           rod.iphone (4.8.0)",
"click_tracking":true,
"rcpt_meta":{
},
"message_id":"XXXXX",
"ip_address":"XXXXX",
"initial_pixel":true,
"recipient_domain                                           ":"XXXXX.com",
"event_id":"XXXXX",
"routing_domain":"XXXXX",
"sending_ip":"192.174.87.34",
"template_id":"template_697731362943421639",
"delv_method":"esmtp",
"customer_id":XXX,
"open_tracking":true,
"injection_time":"2019-10-23T15:37:10.000Z",
"msg_size":"41644",
"timestamp":"2019-10-23T16:03:34.                                           000Z"
}
],
"total_count":3,
"links":{
}
}

你能检查$body的类型吗? 错误说你尝试

获取非对象的属性"total_count">

$body = $response->getBody()->getContents();  //Read the remaining contents of the body as a string

试试这段代码$result = json_decode($response->getBody()); $body = $result->responses[0]->beans;

相关内容

  • 没有找到相关文章

最新更新