0

I have a cron command I'm trying to work on and I have realized I'm not as familiar with Guzzle as I wish I was. But here's a portion of my command:

    foreach($osdClaimResponses as $ocr){
        $client = new \GuzzleHttp\Client(['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 = Carbon\Carbon::parse($result->timestamp);
                   $ocr->initial_open_ip_address = $result->ip_address;
                   $ocr->save();
                }

            }
        }
    }

Now, the initial request works wonderful and I am able to see an appropriate response to my request (of which I will post below), on the line $this->info($body) I can appropriately see the response. However if I go to the next line, I get an error that states Trying to get property 'total_count' of non-object.

As you can see by the return below, there is a property called total_count, so what am I doing wrong?

{
   "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":{
   }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Matthew
  • 1,565
  • 6
  • 32
  • 56
  • I think `$response->getBody()->getContents()` is a json encoded content. So try `$body = json_decode($response->getBody()->getContents())` – Tharaka Dilshan Oct 23 '19 at 17:49

1 Answers1

0

can you check the type of $body ? the error say that you try

to get property 'total_count' of non-object

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

try this code $result = json_decode($response->getBody()); $body = $result->responses[0]->beans;

KRA
  • 22
  • 1
  • 7