如何使用GET请求获取JSON

时间:2014-11-24 10:24:46

标签: php json

需要代码演示HTTP GET请求以获取一些JSON,解码它,并迭代顶级对象,假设格式为:

[{},{},{},...]

1 个答案:

答案 0 :(得分:0)

$url='http://api.example.com/';
$url.='?id='.urlencode($id);//append id as get
$url.='category='.urlencode($category);//append category as get
$response=file_get_contents($url);
//suppose api response is as ['name','created_at','category','id','other_data'];

$data=json_decode($response);

foreach ($data as $key => $value) {
    //do your stuff
}