0

I got the following response code from a web service API.

stdClass Object ( [balance] => 998 
[batch_id] => 243941208 
[cost] => 1 
[num_messages] => 1 
[message] => stdClass Object ( [num_parts] => 1 [sender] => TMTLCO [content] => @U0D070D240D4D00200D120D300D4100200D1F0D460D380D4D0D310D4D0D310D4D002000200D060D230D4D ) [receipt_url] => [custom] => [messages] => Array ( [0] => stdClass Object ( [id] => 117250619 [recipient] => XXXXXXXX ) ) [status] => success ) 

How can i extract the individual variables like balance, batch_id,cost,num_messages and others from this response using php ??

user3790186
  • 239
  • 6
  • 21
  • 1
    Is that an API result? It looks like a print_r from PHP... That is not really intended for (de)serialization. It might be worth asking the people if they are willing to serialize the data according to some standard format like json or xml.... – Gerard van Helden May 14 '16 at 14:52
  • @Gerard van Helden It is print_r..print_r($response); – user3790186 May 14 '16 at 15:51

1 Answers1

0

You have received API response in JSON and have used json_decode($response)? It returns object. And you can access values as object properties. For example:

$obj = json_decode($response);
echo "Balance = {$obj->balance}"; // will output 'Balance = 998'
echo "Batch ID = {$obj->batch_id}"; // will output 'Batch ID = 243941208'
echo "Message sender = {$obj->message->sender}"; // will output 'Message sender = TMTLCO'
Andrew
  • 1,858
  • 13
  • 15