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?
 
Chris StanyonConnect With a Mentor Commented:
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
 
Chris StanyonCommented:
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 StanyonCommented:
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
 
mavmanauNetwork Engineer/SysadminAuthor Commented:
many thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.