开发者

php JSON decode with Twitter geo/search

How do I get coordinates from twitter geo/search? There are four coordinates in the JSON tree, but the result is array. How to? Thanks.

API URL : 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 tree

{
    "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...

update

<?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
}
?>


If you have JSON contents, you can use json_decode() function - it'll return nice array you can read.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜