Simple JSON request in PHP

Master345 picture Master345 · Mar 29, 2012 · Viewed 63.3k times · Source

I have the following json

country_code({"latitude":"45.9390","longitude":"24.9811","zoom":6,"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}})

and i want just the country_code, how do i parse it?

I have this code

<?php
$json = "http://api.wipmania.com/jsonp?callback=jsonpCallback";
$jsonfile = file_get_contents($json);

var_dump(json_decode($jsonfile));
?>

and it returns NULL, why?

Thanks.

Answer

Solo Omsarashvili picture Solo Omsarashvili · Jun 13, 2013
<?php
$jsonurl = "http://api.wipmania.com/json";
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));
?>

You just need json not jsonp.
You can also try using json_decode($json, true) if you want to return the array.