Why Your JSON Parse Returns NULL and How to Fix It
When you attempt to decode a response from http://api.wipmania.com/jsonp?callback=jsonpCallback, receiving NULL is almost certainly due to a misunderstanding of the data format, not a bug in the PHP json_decode() function. The URL explicitly indicates that the API returns JSONP (JSON with Padding), not raw JSON.
In standard JSON, data is structured as objects or arrays enclosed in curly braces {} or square brackets []. However, JSONP wraps this payload inside a function call to facilitate execution in older browser environments where cross-origin resource sharing (CORS) was not yet supported. The response from the URL provided looks like this:
jsonpCallback({"latitude":"45.9390","longitude":"24.9811","zoom":6,"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}})
Notice the function name jsonpCallback preceding the actual data and the lack of surrounding quotes around that entire structure. When PHP's json_decode() encounters this string, it fails to identify valid JSON syntax because of the initial function call wrapper. Consequently, it returns NULL without raising an error by default.
To extract the country_code, you must strip the wrapping function name before attempting to decode. This can be achieved using regular expressions or simple string manipulation. A robust approach involves removing everything up to and including the opening parenthesis of the callback function.
Here is a practical implementation in PHP:
$jsonUrl = "http://api.wipmania.com/jsonp?callback=jsonpCallback";
$jsonString = file_get_contents($jsonUrl);
// Remove the JSONP wrapper (function name + parentheses)
$cleanJson = preg_replace('/^\w+\(/s', '', $jsonString);
$data = json_decode($cleanJson);
if ($data && isset($data->address->country_code)) {
echo "Country Code: " . $data->address->country_code;
} else {
echo "Failed to decode or data structure mismatch.";
}
By using preg_replace, we target the start of the string, matching any word characters followed by a parenthesis and stripping them. This leaves only the valid JSON object inside. Once cleaned, json_decode() successfully populates the $data variable with associative arrays (when using the default flags).
If you are working within a Laravel application or prefer typed properties, you might consider using the JsonpResponse class or custom middleware to handle this pattern automatically, though raw PHP remains effective for simple scripts. The key takeaway is recognizing the distinction between JSON and JSONP; treating them identically leads to silent failures where data structures vanish into NULL.
When dealing with third-party APIs, always inspect the response headers and URL parameters first. If a query parameter like callback=... is present, you are likely facing a legacy CORS workaround rather than standard JSON. Modern alternatives often expose raw JSON endpoints if you adjust the request method or omit the callback parameter, but for this specific API, the regex cleanup step is mandatory.
Finally, consider error handling. If the API returns an error message instead of data, your regex might not strip it cleanly, and json_decode will return NULL again. Always verify the decoded object exists before accessing nested keys like $data->address->country_code to avoid undefined property notices in production environments.