繁体   English   中英

PHP Curl-知道内容类型

[英]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.

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