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
API目前支持两种类型的响应格式:XML和JSON
You can specify the desired format using the HTTP Accept header in the request:
您可以使用请求中的HTTP Accept标头指定所需的格式:
Accept: application/xml Accept: application/json
接受:application / xml Accept:application / 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.
提前致谢。
1 个解决方案
#1
17
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 - Accept标头是一个请求标头,它指定了响应主体的可接受类型
- The
Content-Type
header is both a request and a response header and it specifies the format of the body of the request/response - 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"
}
#1
17
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 - Accept标头是一个请求标头,它指定了响应主体的可接受类型
- The
Content-Type
header is both a request and a response header and it specifies the format of the body of the request/response - 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"
}