我想从以下RESTful响应中获取名称值
{
"login": "mdo",
"id": 98681,
"url": "https://api.github.com/users/mdo",
"html_url": "https://github.com/mdo",
"type": "User",
"site_admin": true,
"name": "Mark Otto",
"company": "GitHub",
"blog": "http://mdo.fm",
"location": "San Francisco, CA",
}
这是我的代码。到目前为止,我只能设法检索完整的结果。
<?php
//step1
$cSession = curl_init();
//step2
curl_setopt($cSession, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
curl_setopt($cSession,CURLOPT_URL,"https://api.github.com/users/mdo");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
//step5
echo $result;
?>
答案 0 :(得分:2)
只需使用json_decode方法:
$obj = json_decode($result);
echo $obj->{'name'};
答案 1 :(得分:1)
您可以先使用以下语句使用json_decode
解码结果。
$arr = json_decode($result, true);
然后您可以通过$arr['name'];
echo $arr['name'];
这很简单,试试这个。