使用php从URL获取json数据

时间:2015-02-09 08:39:17

标签: javascript php jquery arrays json

这里我尝试从以下url

获取json数据
  

https://www.flickr.com/services/api/render?method=flickr.places.find&api_key=xxxxx&query= $ search_data&安培;格式= JSON&安培; nojsoncallback = 1

包含我想根据上述链接获得的以下信息

{
    "places": {
        "place": [
            {
                "place_id": "Dm5SiT1TULMEIMYN",
                "woeid": "2295402",
                "latitude": 23.03,
                "longitude": 72.591,
                "place_url": "/India/Gujarat/Ahmedabad",
                "place_type": "locality",
                "place_type_id": 7,
                "timezone": "Asia/Kolkata",
                "_content": "Ahmedabad, Gujarat, India",
                "woe_name": "Ahmedabad"
            },
            {
                "place_id": "mz1zSK1YUrJTfvUrOA",
                "woeid": "90883450",
                "latitude": 23.027,
                "longitude": 72.57,
                "place_url": "/India/Gujarat",
                "place_type": "locality",
                "place_type_id": 7,
                "timezone": "Asia/Kolkata",
                "_content": "Ahmedabad, India",
                "woe_name": "Ahmedabad"
            }
        ],
        "query": "Ahmedabad, Gujarat, India",
        "total": 2
    },
    "stat": "ok"
}

但是这里的问题是这个链接使用以下php代码返回null

$json_array = file_get_contents("https://www.flickr.com/services/api/render?method=flickr.places.find&api_key=xxxxx&query=Ahmedabad,%20Gujarat,%20India&format=json&nojsoncallback=1");
$json_array = iconv('UTF-16', 'UTF-8', $json_array);
$json_data=json_decode($json_array,true);
print_r($json_data);

3 个答案:

答案 0 :(得分:2)

你的电话是错误的。正确的是

https://api.flickr.com/services/rest/?method=flickr.places.find&.....

答案 1 :(得分:2)

我找到了它,因为你正在调用API的/api/render方法,所以它会给pretty printed json

只需使用此网址

//https://www.flickr.com/services/rest?method=flickr.places.find&api_key=c5336c8cc248142bbda940c1f771bfd5&query=Ahmedabad,%20Gujarat,%20India&format=json&nojsoncallback=1

$json_array = file_get_contents("https://www.flickr.com/services/rest?method=flickr.places.find&api_key=c5336c8cc248142bbda940c1f771bfd5&query=Ahmedabad,%20Gujarat,%20India&format=json&nojsoncallback=1");

$json_data=json_decode($json_array,true);
print_r($json_data);

答案 2 :(得分:1)

我尝试了Flickr API的示例请求。

https://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&foo=bar&api_key=cddc3ae537ed443aafa20cf2c30086df

您需要在请求URI中添加以下参数format=json。此外,端点必须为/services/rest