在php中从JSON中提取正文部分时出错

时间:2012-09-27 07:46:22

标签: php json decode

我正在创建http_client对象并发出GET请求。以下是我的代码。

$httpc = new HTTP_CLIENT();
$httpc->get("$endpointUrl?operation=getchallenge&username=$userName");
$response = $httpc->currentResponse();
print_r($response);
output: Array ( [url] => http://localhost:8888//webservice.php?operation=getchallenge&username=admin [code] => 200 [headers] => Array ( [date] => Thu, 27 Sep 2012 07:30:17 GMT [server] => Apache/2.0.52 (Win32) PHP/5.2.6 [x-powered-by] => PHP/5.2.6 [expires] => Thu, 19 Nov 1981 08:52:00 GMT [cache-control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0 [pragma] => no-cache [content-length] => 102 [connection] => close [content-type] => application/json ) [body] => {"success":true,"result":{"token":"50640089780c4","serverTime":1348731017,"expireTime":1348731317}} )

jsonResponse = Zend_JSON::decode($response['body']);

我试图从这个数组中提取“body”部分,但我得到的值是空白的。如果可以,请帮助我。

注意:您可以在[body]部分的开头看到输出具有JUNK值。

2 个答案:

答案 0 :(得分:1)

由于这是一个API并且它们显然有一些BOM字符,因此您应该将它们解析并解码:

$jsonResponse = Zend_JSON::decode(substr($response['body'], 6));

答案 1 :(得分:0)

试试这个

var_dump(Zend_Json_Decoder::decode($data, Zend_Json::TYPE_ARRAY));