【问题标题】:Curl is overwritting the content type I am setting for gcm callCurl 正在覆盖我为 gcm 调用设置的内容类型
【发布时间】:2015-05-27 10:02:46
【问题描述】:

简而言之:

我在卷曲时设置的内容类型被覆盖。相关代码如下:


详细

我正在使用 codeigniter 开发一个需要进行 gcm 调用的移动应用后端应用程序。 同样,我正在使用来自https://github.com/antongorodezkiy/codeigniter-gcm 的 codeigniter-gcm 库。虽然它在我的本地机器上运行良好,但放在我的服务器上时会返回错误。

在检查我提出的请求后,我发现在进行 curl 时设置的内容类型被覆盖。设置的内容类型是 application/json 但由于某种原因在服务器上发出请求时,它被覆盖到 text/html 中,这可以通过输出请求标头来证明。 这是使用的相关代码。

    $headers[] = 'Content-Type:application/json';
    $headers[] = 'Authorization:key='.$this->apiKey;

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $this->apiSendAddress);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $this->responseData = curl_exec($curl);
    $this->responseInfo = curl_getinfo($curl);

    curl_close($curl);

但是当我输出 $this->responseInfo 时请注意输出

[url] => https://android.googleapis.com/gcm/send
[content_type] => text/html; charset=UTF-8
[http_code] => 401
[header_size] => 395
[request_size] => 967
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.052485
[namelookup_time] => 2.7E-5
[connect_time] => 0.002536
[pretransfer_time] => 0.010841
[size_upload] => 786
[size_download] => 147
[speed_download] => 2800
[speed_upload] => 14975
[download_content_length] => -1
[upload_content_length] => 786
[starttransfer_time] => 0.05244
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 2404:6800:4005:80a::200a
[certinfo] => Array
    (
    )

为什么 curl 会改变 Content-Type?我怎样才能确保它没有改变? 任何帮助,将不胜感激。 谢谢!

编辑 我尝试了PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that 中提到的解决方案,但它也被覆盖了

【问题讨论】:

    标签: php codeigniter curl google-cloud-messaging


    【解决方案1】:

    这是两个不同的 Content-Type 标头。您在 request 中发送的第一个,在 response 中获得的第二个。因此,服务器通常以text/html 响应。如果您期望其他内容类型,请在您的请求中使用 Accept 标头。

    【讨论】:

    • 服务器响应通常是application/json。根据我阅读的内容,它还支持文本/纯文本。但即使我将 Content-Type 设置为该值并设置 Accept 标头,curl 也会将发送 Content-Type 更改为 text/html。
    • 如何查看请求标头?尝试this way(第二个答案)将请求标头写入文件。
    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2021-12-09
    • 2012-10-19
    相关资源
    最近更新 更多