PHP Curl,在 application/json 中请求数据返回 [英] PHP Curl, Request data return in application/json

查看:42
本文介绍了PHP Curl,在 application/json 中请求数据返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 API 获取一些数据,目前我正在将其作为 XML 获取.

I am trying to get some data from an API and I am getting it back at the moment as XML.

我更喜欢它作为 jSON 并且 API 文档说它可以在 json 和 XML 中使用.

I would prefer it as jSON and the API Doc's say that it is available in jSON aswell as XML.

文档说...

API 目前支持两种响应格式:XML 和 JSON

The API currently supports two types of response format: XML and JSON

您可以使用请求中的 HTTP Accept 标头指定所需的格式:

You can specify the desired format using the HTTP Accept header in the request:

接受:application/xml接受:申请/json

Accept: application/xml Accept: application/json

那么如何在我的 PHP 代码中生成 applixation/json 的 Accept Header?

So how do I generate the Accept Header of applixation/json in my PHP code?

我目前的 PHP 代码是:

My PHP code at the moment is :

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;

结果的回显只是返回 XML 格式的数据.

The echo of result is just returning XML formatted data.

提前致谢.

推荐答案

您应该修改在请求中设置 HTTP 标头的代码.您没有指定 Accept 标头

You should modify the code where you set the HTTP headers in your request. You weren't specifying the Accept header

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));

这应该将 HTTP 请求发送到 API 的 URL,其中包含您希望以特定格式获取响应的信息.

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.

编辑(以防这对某人有用):

Edit (in case this could be useful to someone):

  • Accept 标头是一个请求标头,它指定了响应正文的可接受类型
  • Content-Type 标头既是请求标头又是响应标头,它指定了请求/响应正文的格式
  • 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

HTTP 请求的示例可能如下所示(通常,请求仅包含标头部分):

Example of the HTTP requests could look something like this (often, the request contains only the header part):

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"
}

这篇关于PHP Curl,在 application/json 中请求数据返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆