3

here i try to fetch json data from following url

https://www.flickr.com/services/api/render?method=flickr.places.find&api_key=xxxxx&query=$search_data&format=json&nojsoncallback=1

contain following information which i want to get according above link

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

but here problem is that this link return null i'm using following code of php

$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);
mylesthe.dev
  • 9,565
  • 4
  • 23
  • 34
Hitesh Tank
  • 156
  • 1
  • 8

3 Answers3

2

Your call is wrong. The correct one would be

https://api.flickr.com/services/rest/?method=flickr.places.find&.....
Markus Müller
  • 2,611
  • 1
  • 17
  • 25
  • I have checked too. But how it explains why json_decode doesn't decodes json? I have checked json with validator and it's correct. – Roman Losev Feb 09 '15 at 08:58
  • @RomanLosev The OP didn't use the rest endpoint of the flicker API. He used the rendering endpoint, which syntax highlights the JSON via HTML. json_decode can not decode HTML. Thus it failed. – Markus Müller Feb 09 '15 at 09:00
  • yes but doesn't get information how to get perticular information like i want to fetch place_id how? – Hitesh Tank Feb 09 '15 at 09:03
  • See this SO question: http://stackoverflow.com/questions/263392/handling-data-in-a-php-json-object – Markus Müller Feb 09 '15 at 09:05
  • You're welcome. You might want to mark this answer as correct, so it is easier for other people to find that face a similiar problem – Markus Müller Feb 09 '15 at 09:24
2

I found it, its because you are calling /api/render method of API so it will give pretty printed json

just use this url instead

//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);
Saqueib
  • 3,484
  • 3
  • 33
  • 56
1

I tried a sample request from the Flickr API.

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

You need to add the following parameter format=json in your request URI. Also, the endpoint must be /services/rest.

psiyumm
  • 6,437
  • 3
  • 29
  • 50
  • yes that's fine but i can't scrap code of particular id information such as place_id how? – Hitesh Tank Feb 09 '15 at 09:05
  • If you have received the JSON now (use whatever URL you were using), you could just use `json_decode($response, true)`. This will convert the respone into the `PHP array` object. Then you can iterate over all the responses. – psiyumm Feb 09 '15 at 09:09