php JSON解码与Twitter地理/搜索

时间:2011-06-06 21:04:34

标签: php json

如何从twitter geo / search获取坐标? JSON树中有四个坐标,但结果是数组。如何?感谢。

API网址:https://api.twitter.com/1/geo/search.json?query=tokyo //での取得结果

json[result]['places']['0']['name']=Tokyo
json[result]['places']['0']['bounding_box']['type']=Polygon
json[result]['places']['0']['bounding_box']['coordinates'][0][0]=Array  //Array data.

JSON树

{
    "result": {
        "places": [
            {
                "name": "Tokyo",
                "bounding_box": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                138.942806388889,
                                24.222885
                            ],
                            [
                                142.248202222222,
                                24.222885
                            ],
                            [
                                142.248202222222,
                                35.8986897222222
                            ],
                            [
                                138.942806388889,
                                35.8986897222222
                            ]
                        ]
                    ]
                },
       ... // a long tree, something not important, hiden...

更新

<?php
header('Content-type:text/html; charset=utf-8');
$url = "https://api.twitter.com/1/geo/search.json?query=tokyo";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: api.twitter.com'));
$body = curl_exec($ch);
curl_close ($ch);
$data = json_decode($body, true);
foreach ($data['result']['places'] as $data) {
    echo $data['bounding_box']['coordinates']['0']['0'].'<br />'; //array
}
?>

1 个答案:

答案 0 :(得分:2)

如果你有JSON内容,你可以使用json_decode()函数 - 它将返回你可以读取的漂亮数组。

http://php.net/manual/en/function.json-decode.php