PHP 中的简单 JSON 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9930434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:56:19  来源:igfitidea点击:

Simple JSON request in PHP

phpfile-get-contentsjson

提问by Master345

I have the following json

我有以下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?

我只想要 country_code,我该如何解析它?

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?

它返回NULL,为什么?

Thanks.

谢谢。

回答by Solo Omsarashvili

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

你只需要json而不是jsonp。如果要返回数组,
也可以尝试使用 json_decode($json, true)

回答by JKirchartz

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

你请求 jsonp with http://api.wipmania.com/jsonp?callback=jsonpCallback,它返回一个包含 JSON 的函数,如:

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/jsonto return pure JSON like:

而不是 JSON 本身。将您的 URL 更改http://api.wipmania.com/json为返回纯 JSON,如:

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

请注意,第二段代码没有将 json 包装在jsonpCallback()函数中。

回答by Felipe Sabino

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

如果您的服务器实现JSONP,它将假定回调参数是一个JSONP信号,结果将类似于 JavaScript 函数,如

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

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

然后,json_decode将无法解析jsonpCallback("{yada: 'yada yada'}")为有效的 JSON 字符串

回答by Jasper

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.

您返回的是 JSONP,而不是 JSON。JSONP 用于 JavaScript 中的跨域请求。使用 PHP 时不需要使用它,因为您不受跨域策略的影响。

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):

由于您从file_get_contents()函数中获取字符串,因此您可以替换country_code(文本(这是响应的 JSONP 特定部分):

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

这有效,但 JKirchartz 的解决方案看起来更好,只需请求正确的数据而不是处理不正确的数据。

回答by grifos

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

如果country_code(您的 json 中包含右括号,请将其删除。这不是有效的 json 语法:json

回答by dutchflyboy

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

该网站不返回纯 JSON,而是包装 JSON。这意味着作为脚本包含在内,并将调用回调函数。如果要使用它,首先需要删除函数调用(直到第一个括号的部分和最后的括号)。

回答by NeoVance

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

显然在这种情况下,使用正确的 URL 访问 API 将返回纯 JSON。

"http://api.wipmania.com/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.

很多人都在提供正在使用的 API 的替代方案,而不是回答 OP 的问题,所以这里有一个解决方案,供那些寻找在 PHP 中处理 jSONp 的方法的人使用。

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:

首先,API允许你指定一个回调方法,所以你既可以使用Jasper的获取jSON子串的方法,也可以给出json_decode的回调方法,并修改结果以调用eval使用。这是我对 Jasper 代码示例的替代,因为我不喜欢成为一个模仿者:

$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:

诚然,这看起来有点长,更不安全,而且不像 Jasper 的代码那样清晰易读:

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

var_dump(json_decode($jsonfile));
$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:

然后 JSON"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}告诉我们像这样访问 country_code:

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