V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
eoo
V2EX  ›  PHP

CURL POST 请求 返回 Length Required

  •  
  •   eoo · 2015-09-15 23:35:01 +08:00 via Android · 11563 次点击
    这是一个创建于 3355 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我就纳闷了,头一次出现这问题,百度过了找不到相关问题的解决办法。

    携带 cookie GET 请求普通页面倒是没问题, 加了 POST 请求签到地址就返回

    Length Required

    HTTP Error 411. The request must be chunked or have a content length.


    我以为是缺少了 Content-Length 头

    就 strlen data 长度加了 header 提交 还是这样。

    怎么解决?
    4 条回复    2015-09-16 07:23:55 +08:00
    msg7086
        1
    msg7086  
       2015-09-16 00:53:33 +08:00
    Talk is cheap, show me the code.
    eoo
        2
    eoo  
    OP
       2015-09-16 01:04:29 +08:00 via Android
    @msg7086

    <?php
    header ("Content-type: text/html; charset=utf-8");

    ini_set ('display_errors','on');
    error_reporting (E_ALL );

    //限制内存占用
    ini_set ('memory_limit','10M');



    //普通 URL 地址
    $url="http://xin123.cc/bbs/book_list.aspx?action=new&siteid=1000";

    //签到地址
    $qd_url="http://xin123.cc/signin/Signin.aspx?Action=index&Mod=Signin&siteid=1000";


    //COOKIE
    $cookie = 'sidxin123=6C24EE628EF9AF160_8_68_37964_500100-2-0-0-0-600
    ';


    $data="content=月饼&FaceSelect=1.gif&g=签到";



    $ch = curl_init ();
    curl_setopt ($ch,CURLOPT_URL,$qd_url );
    curl_setopt ($ch,CURLOPT_HEADER,false );
    //curl_setopt ($ch,CURLOPT_TIMEOUT,2 );

    $header = array (
    "Content-length: ".strlen ($data )."
    );

    curl_setopt ($ch,CURLOPT_HTTPHEADER,$header );
    curl_setopt ($ch,CURLOPT_POST,true );
    curl_setopt ($ch,CURLOPT_POSTFIELDS,$data );
    curl_setopt ($ch,CURLOPT_COOKIE,$cookie );
    curl_setopt ($ch,CURLOPT_RETURNTRANSFER,false );
    curl_exec ($ch );
    curl_close ($ch );
    msg7086
        3
    msg7086  
       2015-09-16 03:04:17 +08:00
    <?php
    header ("Content-type: text/html; charset=utf-8");
    ini_set ('display_errors','on');
    error_reporting (E_ALL );
    ini_set ('memory_limit','10M');

    $url="http://xin123.cc/bbs/book_list.aspx?action=new&siteid=1000";
    $qd_url="http://xin123.cc/signin/Signin.aspx?Action=index&Mod=Signin&siteid=1000";
    $cookie = 'sidxin123=6C24EE628EF9AF160_8_68_37964_500100-2-0-0-0-600';
    $data="content=月饼&FaceSelect=1.gif&g=签到";

    $ch = curl_init ();
    curl_setopt ($ch,CURLOPT_URL,$qd_url );
    curl_setopt ($ch,CURLOPT_HEADER,true );

    curl_setopt ($ch,CURLOPT_POST,true );
    curl_setopt ($ch,CURLOPT_POSTFIELDS,$data );
    curl_setopt ($ch,CURLOPT_COOKIE,$cookie );
    curl_setopt ($ch,CURLOPT_RETURNTRANSFER,false );
    curl_exec ($ch );
    curl_close ($ch );


    以上代码测试得到
    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Length: 11900
    Content-Type: text/html; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 2.0.50727
    Set-Cookie: ASP.NET_SessionId=wk1vytudkv1azf55vjd5cbnj; path=/; HttpOnly
    Set-Cookie: GUID=409df51603005952; expires=Thu, 15-Sep-2016 19:00:59 GMT; path=/
    X-Powered-By: ASP.NET
    Date: Tue, 15 Sep 2015 19:00:59 GMT

    (内容略)

    于是问题在哪?
    eoo
        4
    eoo  
    OP
       2015-09-16 07:23:55 +08:00 via Android
    @msg7086 奇葩啊, 太奇葩了, 为什么会这么奇葩现在又可以了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1076 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 98ms · UTC 19:23 · PVG 03:23 · LAX 11:23 · JFK 14:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.