1

I have a little problem that I can't get my head around. I am new to php. I want to extract the top_secret from here:

Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] 
=> Array ( [content-type] => application/json; charset=UTF-8 [expires] => Sat, 01 Jan 2000 00:00:00
 GMT   [content-length] => 250 ) ) [body] => 

{"top_secret":"WELLTHISISTOPSECRETSONOONESHOULDEVERSEEIT","token_type":x,"expires_in":x} [response] => 
Array ( [code] => 200 [message] => OK ) [cookies] => Array ( ) [filename] => 
[http_response] => WP_HTTP_Requests_Response Object ( [response:protected] => 
Requests_Response Object ( [body] => 
{"top_secret":"WELLTHISISTOPSECRETSONOONESHOULDEVERSEEIT","token_type":x,"expires_in":x} [raw] => 
HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8
{"top_secret":"WELLTHISISTOPSECRETSONOONESHOULDEVERSEEIT","token_type":"bearer","expires_in":x} 

What I tried:

<?php
parse_str($response['body'], $response);
$field=$response['top_secret'];
echo $top_secret;
?>

But nothing appears. Any idea what I am doing wrong? I want to echo the following string:

WELLTHISISTOPSECRETSONOONESHOULDEVERSEEIT

Amit Verma
  • 40,709
  • 21
  • 93
  • 115

1 Answers1

0

The parse_str function parses strings as if they were the query string passed via a URL, e.g. something like first=value&arr[]=foo+bar&arr[]=baz.

But your $response['body'] is apparently a JSON string. You will have to run it through json_decode, e.g.

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

See the documentation for

Gordon
  • 312,688
  • 75
  • 539
  • 559