2

I got response from URL like this. I want value of sid. How can I get this?

{"response": "success","body":{ "sid" : "5f255c86a", "role" : "user" }}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
soni8010
  • 385
  • 1
  • 6
  • 19
  • possible duplicate of [Parsing JSON file with PHP](http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – Sougata Bose Aug 10 '15 at 09:40

2 Answers2

2

It JSON format so use json_decode() function.

$response = json_decode($response);

echo $response->body->sid;

or

$response = json_decode($response, true);
echo $response['body']['sid'];

If you pass second argument as true then it will return associative array.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Robert
  • 19,800
  • 5
  • 55
  • 85
1

The above response looks to b JSON so you can use json_decode()function.

Try

$response = file_get_contents($url);
$json = json_decode($response);

echo $json->body->sid;

Note You need to replace $url with URL of response page.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50