PHP Curl Json Response

Hi,

I am in the process of building a call to a rest service.  I am correctly making the calls to the service etc, but I am having a slight issue with pulling specific data out.

Given the below response:

"{"nextpageuri":null,
"previouspageuri":null,
"sms_messages":
[{"created":"Fri, 08 Dec 2017 03:34:55 +0000",
"id":"cf4083fd852758af8bca5cffb77771d7",
"from":"0419000000","destination":"61417000000",
"message":"SAMPLE MSG",
"uri":"\/2016-11-30\/sms\/cf4083fd852758af8bca5cffb77771d7",
"receipts":{"delivered":"Fri, 08 Dec 2017 03:34:58 +0000"}}],"message":"Response follows","api_version":"2016-11-30","status":200,"uri":"\/2016-11-30\/sms\/cf4083fd852758af8bca5cffb77771d7"}"

Open in new window


This is what I am attempting to use to seperate out the receipts:
  // MAKE AN OBJECT
$Callresult = json_decode($response);
echo "attempt 1:".$response->sms_messages[0]->receipts;

But I keep getting an error of:
Notice:  Trying to get property of non-object

and if I try:
echo "attempt 1:".$response->sms_messages[0]->receipts;
i get:
Catchable fatal error:  Object of class stdClass could not be converted to string

if anyone could help it would be most appreciated!!!
mavmanauNetwork Engineer/SysadminAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
receipts is an object with a propery called delivered so you can't just echo out receipts. You'll need to echo out the property:

echo "attempt 1:".$response->sms_messages[0]->receipts->delivered;
0
Chris StanyonWebDevCommented:
Also, it's the raw JSON that is called $response. You are converting that into an objet called $Callresult, so actually what you need is this:

echo "attempt 1:".$Callresult->sms_messages[0]->receipts->delivered;
1
mavmanauNetwork Engineer/SysadminAuthor Commented:
HI,

I just realised that I did vardump on $response rather than the vardump of $Callresult.

it looks like this:

object(stdClass)#4 (7) {
  ["nextpageuri"]=>
  NULL
  ["previouspageuri"]=>
  NULL
  ["sms_messages"]=>
  array(1) {
    [0]=>
    object(stdClass)#5 (7) {
      ["created"]=>
      string(31) "Fri, 08 Dec 2017 03:34:55 +0000"
      ["id"]=>
      string(32) "cf4083fd852758af8bca5cffb77771d7"
      ["from"]=>
      string(10) "0419000000"
      ["destination"]=>
      string(11) "61417000000"
      ["message"]=>
      string(147) "SAMPLE MSG"
      ["uri"]=>
      string(48) "/2016-11-30/sms/cf4083fd852758af8bca5cffb77771d7"
      ["receipts"]=>
      object(stdClass)#6 (1) {
        ["delivered"]=>
        string(31) "Fri, 08 Dec 2017 03:34:58 +0000"
      }
    }
  }
  ["message"]=>
  string(16) "Response follows"
  ["api_version"]=>
  string(10) "2016-11-30"
  ["status"]=>
  int(200)
  ["uri"]=>
  string(48) "/2016-11-30/sms/cf4083fd852758af8bca5cffb77771d7"
}

given that result, how would I best access the receipts section?
0
Chris StanyonWebDevCommented:
I've already shown you how to access the receipts:

$Callresult->sms_messages[0]->receipts->delivered;

Have a read back through my previous answers.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mavmanauNetwork Engineer/SysadminAuthor Commented:
many thanks for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.