converting mashape api response to php string

2019-08-09 17:19发布

问题:

Below is my code, I am trying to get particular mashape api response (operator) in php string.

require_once 'vendor/autoload.php';
$response = Unirest\Request::get("https://sphirelabs-mobile-number-portability-india-operator-v1.p.mashape.com/index.php?number=8055144322",
array(
"X-Mashape-Key" => "XXXXXXXXX",
"Accept" => "application/json"
)
);
json_decode($response, true);
echo $response['operator'];

Response:

Unirest\Response Object ( 
  [code] => 200 
  [raw_body] => {"Telecom circle":"Maharashtra","Operator":"Reliance GSM","Is MNP":"False"} 
  [body] => stdClass Object ( 
       [Telecom circle] => Maharashtra 
       [Operator] => Reliance GSM 
       [Is MNP] => False 
   ) 
   [headers] => Array ( [0] => HTTP/1.1 200 OK [Accept-Ranges] => none [Content-Encoding] => gzip [Content-Type] => application/json [Date] => Thu, 19 Feb 2015 14:04:39 GMT [Server] => Mashape/5.0.6 [Vary] => Accept-Encoding [Content-Length] => 91 [Connection] => keep-alive ) )

回答1:

You don't need to json_decode this. It's already a php Object.

$response->body->Operator should give you the result you're looking for.



标签: php mashape