我想在网站上使用 CURL 登录,看起来像
参数将通过Post发送
curl_setopt($ch, CURLOPT_POST, TRUE);
$data = array ("params" => "param" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
CURL 正在设置
期望:100-继续标题
我会得到一个
417 - 期望失败
作为回应。
所以它不起作用。当我尝试删除 Expect 标头时
curl_setopt($ch, CURLOPT_HTTPHEADER, array('期望:'));
CURL 正在发送 GET 请求,而不是 POST 请求。我究竟做错了什么?
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/login.php?return=");
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.com/login.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
$data = array (
"param1" => $username,
"param2" => $password
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec ($ch);
curl_close($this->ch);
您可以同时获取和发布
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/login.php?return=");
那就是得到。
curl 不会发送 Expect 标头,除非发布数据超过一定大小,您的示例代码无论如何都不会发送 Expect 标头。如果您需要一些随机数据来在示例代码中生成 Expect 标头,请添加
'data'=>str_repeat("\x00",1*1024)
,这将添加 1 KB 的空值,这将使您的示例代码发送 Expect 标头。也就是说,我无法重现该问题,这是在未删除 Expect 的情况下发送的 POST 请求:
POST / HTTP/1.1
Host: 127.0.0.1:9999
User-Agent: wut
Accept: */*
Referer: http://www.example.com/login.php
Content-Length: 1369
Content-Type: multipart/form-data; boundary=------------------------35f10eb804cdb5c6
Expect: 100-continue
--------------------------35f10eb804cdb5c6
Content-Disposition: form-data; name="param1"
username
--------------------------35f10eb804cdb5c6
Content-Disposition: form-data; name="param2"
password
--------------------------35f10eb804cdb5c6
Content-Disposition: form-data; name="data"
--------------------------35f10eb804cdb5c6--
如果我删除 Expect 标头,这是新请求:
POST / HTTP/1.1
Host: 127.0.0.1:9999
User-Agent: wut
Accept: */*
Referer: http://www.example.com/login.php
Content-Length: 1369
Content-Type: multipart/form-data; boundary=------------------------0a1590cb9de6d248
--------------------------0a1590cb9de6d248
Content-Disposition: form-data; name="param1"
username
--------------------------0a1590cb9de6d248
Content-Disposition: form-data; name="param2"
password
--------------------------0a1590cb9de6d248
Content-Disposition: form-data; name="data"
--------------------------0a1590cb9de6d248--
如您所见,
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
没有将请求类型从POST更改为GET,因此我投票结束此问题,因为can not reproduce