【发布时间】: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