[英]PHP Curl - Knowing the Content-type
下面是一个没有CURLOPT_HTTPHEADER
的示例,它可以正常工作。
<?php
$URL = "http://someurl.info";
$data = 'x_test_request=0&x_version=3.1&x_delim_data=true&x_relay_response=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 1800);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
?>
Curl是否会自动检测我们发布的数据类型并自动计算字符串长度? 还是应该总是同时指定Both?
还是很大程度上取决于目的地的规则和期望?
为了安全起见,我通常会添加标头信息,如下所示:
<?php
$contentlen = strlen(trim($data));
$headersARR = array(
"Content-type: application/x-www-form-urlencoded",
"Content-length: ".$contentlen,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersARR);
?>
我阅读了http://us1.php.net/curl_setopt ,但没有注意到默认内容类型参数的任何细节。
如果您没有指定任何有关Content-type
而是使用CURLOPT_POSTFIELDS
则标头始终为Content-type: application/x-www-form-urlencoded
默认为Content-type: application/x-www-form-urlencoded
。 它还会自动设置Content-length:
:。
因此,当我们通过POST
发布JSON
内容时,我们需要通过HTTPHEADER
选项覆盖content-type
。 它类似于XML
或任何其他特定类型的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.