PHP Curl Json Response


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:

[{"created":"Fri, 08 Dec 2017 03:34:55 +0000",
"message":"SAMPLE MSG",
"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:
$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:


Have a read back through my previous answers.
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;
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;
mavmanauNetwork Engineer/SysadminAuthor Commented:

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

it looks like this:

object(stdClass)#4 (7) {
  array(1) {
    object(stdClass)#5 (7) {
      string(31) "Fri, 08 Dec 2017 03:34:55 +0000"
      string(32) "cf4083fd852758af8bca5cffb77771d7"
      string(10) "0419000000"
      string(11) "61417000000"
      string(147) "SAMPLE MSG"
      string(48) "/2016-11-30/sms/cf4083fd852758af8bca5cffb77771d7"
      object(stdClass)#6 (1) {
        string(31) "Fri, 08 Dec 2017 03:34:58 +0000"
  string(16) "Response follows"
  string(10) "2016-11-30"
  string(48) "/2016-11-30/sms/cf4083fd852758af8bca5cffb77771d7"

given that result, how would I best access the receipts section?
mavmanauNetwork Engineer/SysadminAuthor Commented:
many thanks for your help!
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.