我需要从api获取特定数据的帮助。 JSON数据看起来像这样如何从json获取特定数据?
array(1)
{
[0]=> string(18) "{"errorCode":0
"members":[
{"userId":"32"
"name":"Joy"
"age":"22"
"country":"USA"
"orders":133
"active":0}
{"userId":"38"
"name":"greg"
"age":"29"
"country":"CA"
"orders":19
"active":0}
{"userId":"29"
"name":"bob"
"age":"33"
"country":"USA"
"orders":67
"active":0}
],"total":59},
Array"
}
我想回去只有人在美国是超过25
到目前为止我的代码是
<?PHP
$url="https://api.someplace.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$content = curl_exec($ch);
$response = curl_getinfo($ch);
$combine = "$content, $response";
curl_close ($ch);
$openBracket = str_replace("{","{\n",$combine);
$comma = str_replace(",",",\n",$openBracket);
$result = str_replace(',', '<br />', $comma);
$ary = array($result);
echo $ary;
?>
这将返回的是整个json文件,但是当我试图对数据做任何事情的时候都不起作用。我知道我需要某种foreach循环,if/else语句或者json_decode,我不会因为它们没有工作而让我感到厌烦,因为它们都不起作用,我只返回所有json数据的返回,NULL,{ ,或只是一个空白页面。
我不确定cURL是否搞乱了事情,我以前从来没有使用过,因为这是https连接,所以我不得不使用它。请让我知道获取这些数据的最佳方式。
如果你想获得'$ content'和'$ response'把它们放在一起作为阵列'$ =结合阵列()第一件事; $ combine ['content'] = $ contente; $ combine ['info'] = $ info;'你可以根据需要循环条件。 –
[用PHP解析JSON文件]可能的副本(http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – miken32
可能的重复[如何从JSON中用PHP提取数据?] (http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) –