0
{"status":"ok","params":{"stream_token":"token=lTQ4sJx9vK7pR7kgeYVDgQ&e=1448284525&u=37997"}}

I would like to parse this string and need

token=lTQ4sJx9vK7pR7kgeYVDgQ&e=1448284525&u=37997

Hows that possible in PHP?

echo json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}');

throuse the following error:

Catchable fatal error: Object of class stdClass could not be converted to string

Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62
  • Don't try eching an object; just retrieve the value you want from the object: `$value = json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}'); echo $value->stream_token;` – Mark Baker Nov 23 '15 at 11:42
  • yes, its for just help – Muhammad Muazzam Nov 23 '15 at 11:58

1 Answers1

3

Add the extra parameter to json_decode to retrieve the result as an associative array:

$data = json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}', TRUE);
$url= $data['params']['stream_token'];    
var_dump($url); // token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"

To parse the url variables use parse_str:

parse_str($url, $fragments);
var_dump($fragments);//
/*
array(3) {
  ["token"]=>
  string(22) "U8h5Ma12SrlizPoFm-Nc5w"
  ["e"]=>
  string(10) "1448285819"
  ["u"]=>
  string(5) "37997"
}
*/
ka_lin
  • 9,329
  • 6
  • 35
  • 56