Simple JSON request in PHP

2020-06-09 10:50发布

问题:

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.

回答1:

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



回答2:

you're requesting jsonp with http://api.wipmania.com/jsonp?callback=jsonpCallback, which returns a function containing JSON like:

jsonpCallback({"latitude":"44.9718","longitude":"-113.3405","zoom":3,"address":{"city":"-","country":"United States","country_code":"US","region":"-"}})

and not JSON itself. change your URL to http://api.wipmania.com/json to return pure JSON like:

{"latitude":"44.9718","longitude":"-113.3405","zoom":3,"address":{"city":"-","country":"United States","country_code":"US","region":"-"}}

notice the second chunk of code doesn't wrap the json in the jsonpCallback() function.



回答3:

The website doesn't return pure JSON, but wrapped JSON. This is meant to be included as a script and will call a callback function. If you want to use it, you first need to remove the function call (the part until the first paranthesis and the paranthesis at the end).



回答4:

If your server implements JSONP, it will assume the callback parameter to be a JSONP signal and the result will be similar to a JavaScript function, like

jsonpCallback("{yada: 'yada yada'}")

And then, json_decode won't be able to parse jsonpCallback("{yada: 'yada yada'}") as a valid JSON string



回答5:

If country_code( along with closing parenthesis are include in your json, remove them. This is not a valid json syntax: json



回答6:

You are being returned JSONP, not JSON. JSONP is for cross-domain-requests in JavaScript. You don't need to use it when using PHP because you aren't affected by cross-domain-policies.

Since you are getting a string from the file_get_contents() function you can do a replacement of the country_code( text (this is the JSONP specific part of the response):

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

var_dump(json_decode($jsonfile));
?>

Note

This works but JKirchartz's solution looks better, just request the correct data rather than messing around with the incorrect data.



回答7:

Obviously in this situation, using the correct URL to access the API will return pure jSON.

"http://api.wipmania.com/json"

A lot of people are providing an alternative to the API in use, rather than answering the OP's question, so here is a solution for those looking for a way of handling jSONp in PHP.

First, the API allows you to specify a callback method, so you can either use Jasper's method of getting the jSON sub string, or you can give a callback method of json_decode, and modify the result to use with a call to eval. This is my alternative to Jasper's code example since I don't like to be a copy cat:

$json = "http://api.wipmania.com/jsonp?callback=json_decode";
$jsonfile eval(str_replace("(", "('", str_replace(")", "')", file_get_contents($json)))));

var_dump($jsonfile);

Admittedly this seems a little longer, more insecure, and not as clear to read as Jasper's code:

$json = "http://api.wipmania.com/jsonp?callback=jsonpCallback";
$jsonfile = substr(file_get_contents($json)), 13, -1);

var_dump(json_decode($jsonfile));

Then the jSON "address":{"city":"-","country":"Romania","country_code":"RO","region":"-"} tells us to access the country_code like so:

$jsonfile->{'address'}->{'country_code'};