简体   繁体   English

PHP-cURL指定自定义内容类型

[英]PHP - cURL Specifying a custom Content-Type

I'm trying to do a cURL in PHP to an API that has a custom Content-Type response type option called "application/qhal+json". 我正在尝试在PHP中对具有自定义Content-Type响应类型选项(称为“ application / qhal + json”)的API进行cURL。 It returns a sort of JSON that's abstracted a bit for custom use. 它返回一种抽象的JSON,供自定义使用。

What I'm using: 我正在使用什么:

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CERTINFO, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: applcation/qhal+json'));

$result = curl_exec($ch);

However the response is always coming back as 'application/json' content type. 但是,响应始终以“ application / json”内容类型返回。 Is it not possible to specify a custom content-type? 无法指定自定义内容类型? Does anyone have any ideas on why the response isn't being returned in the correct format? 是否有人对为什么未以正确的格式返回响应有任何想法?

Please try the GET-request with an Accept Header: 请尝试使用带有接受标头的GET请求:

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


The Accept header is used by HTTP clients to tell the server what content types they'll accept. HTTP客户端使用Accept标头告诉服务器它们将接受哪些内容类型。 The server will then send back a response, which will include a Content-Type header telling the client what the content type of the returned content actually is. 然后,服务器将发回一个响应,该响应将包含一个Content-Type标头,该标头告诉客户端返回的内容的内容类型实际上是什么。

HTTP requests can also contain Content-Type headers. HTTP请求还可以包含Content-Type标头。 This header indicates which type of content is send to the server by a POST or PUT request. 此标头指示通过POST或PUT请求将哪种类型的内容发送到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM