将mashape api响应转换为php字符串

时间:2015-02-19 13:46:22

标签: php mashape

下面是我的代码,我试图在php字符串中获得特定的mashape api响应(运算符)。

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'];

响应:

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 个答案:

答案 0 :(得分:1)

你不需要json_decode这个。它已经是一个php对象。

$response->body->Operator应该会为您提供您正在寻找的结果。