PHP Curl,在 application/json 中请求数据返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24939326/
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
PHP Curl, Request data return in application/json
提问by StuBlackett
I am trying to get some data from an API and I am getting it back at the moment as XML.
我正在尝试从 API 获取一些数据,目前我正在将其作为 XML 获取。
I would prefer it as jSON and the API Doc's say that it is available in jSON aswell as XML.
我更喜欢它作为 JSON 并且 API Doc 说它在 JSON 和 XML 中都可用。
The Docs say...
文档说...
The API currently supports two types of response format: XML and JSON
You can specify the desired format using the HTTP Accept header in the request:
Accept: application/xml Accept: application/json
API 目前支持两种类型的响应格式:XML 和 JSON
您可以使用请求中的 HTTP Accept 标头指定所需的格式:
接受:应用程序/xml 接受:应用程序/json
So how do I generate the Accept Header of applixation/json in my PHP code?
那么如何在我的 PHP 代码中生成 applixation/json 的 Accept Header 呢?
My PHP code at the moment is :
我目前的 PHP 代码是:
header('Content-Type: application/json');
$endpoint = "http://api.api.com";
// Initiate curl
$ch = curl_init();
// Set The Response Format to Json
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$endpoint);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
The echo of result is just returning XML formatted data.
结果的回显只是返回 XML 格式的数据。
Thanks in advance.
提前致谢。
回答by
You should modify the code where you set the HTTP headers in your request. You weren't specifying the Accept
header
您应该修改在请求中设置 HTTP 标头的代码。你没有指定Accept
标题
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
This should sent the HTTP request to the API's URL with the information that you would like to get the response in the particular format.
这应该将 HTTP 请求发送到 API 的 URL,其中包含您希望以特定格式获取响应的信息。
Edit (in case this could be useful to someone):
编辑(以防这对某人有用):
- The
Accept
header is a request header and it specifies the acceptable type of the response's body - The
Content-Type
header is both a request and a response header and it specifies the format of the body of the request/response
- 所述
Accept
报头是请求报头,并将其指定响应身体的可接受的类型 - 的
Content-Type
报头既是请求和响应首部,并将其指定请求/响应的所述主体的所述格式
Example of the HTTP requests could look something like this (often, the request contains only the header part):
HTTP 请求的示例可能如下所示(通常,请求仅包含标头部分):
GET /index.html HTTP/1.1
Host: www.example.com
Accept: application/json
And the response could look something like that:
响应可能如下所示:
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 24
{
"hello": "world"
}