使用get参数,Expect标头的CURL Post请求

时间:2014-12-28 14:18:04

标签: php post curl header expect

我想在网站上使用CURL登录,看起来像

  

http://www.example.com/login.php?return=

参数将与Post

一起发送
  

curl_setopt($ ch,CURLOPT_POST,TRUE);
$ data = array(“params”=>“param”          );
        curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);

CURL正在设置

  

期待:100-continue标题

我会得到一个

  

417 - 期望失败

作为回应。

所以它不起作用。当我尝试删除期望标题

  

curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Expect:'));

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);

2 个答案:

答案 0 :(得分:0)

您可以同时获取和发布

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/login.php?return=");

即是。

答案 1 :(得分:0)

curl不会发送Expect标题,除非发布数据超过一定大小,您的示例代码不会以任何一种方式发送Expect标题。如果您需要一些随机数据来使示例代码中生成Expect标头,请添加'data'=>str_repeat("\x00",1*1024),这将添加1 KB的空值,这将使您的示例代码发送Expect标头。就是说,我无法重现该问题,这是发送的POST请求而未删除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=------------------------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的身份关闭此问题

此外,如果您真的认为删除标头会更改您的请求类型,请随时在Discord / facebook上contact me,我想用自己的眼睛观察这种行为。