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

请教 php curl 返回 Empty reply from server 原因?

  •  
  •   hopestar86 · 2014-05-07 18:23:54 +08:00 · 11760 次点击
    这是一个创建于 3642 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function curlPost($url,$postdata=array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 600); //超时时间
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: */*',
    'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
    'Connection: Keep-Alive'));
    $data=curl_exec($ch);
    echo curl_error($ch);
    curl_close($ch);
    return $data;
    }

    $postdata=array(
    'site_key'=>'SOnmitFRjJ7h',
    'api_action'=>'siteverify',
    );

    echo curlPost('http://www.freerun3size.com/api_handler.php',$postdata);

    这个站我用html表单提交有正常返回,但是我用curl模拟提交curl就报Empty reply from server

    我朋友的站,程序本身没做限制,我程序放到空间curl模拟提交也可以正常返回,请教是这个空间做了什么限制了吗? 在加上什么模拟条件可以绕过限制,谢谢
    6 条回复    2014-05-08 21:35:37 +08:00
    mengskysama
        1
    mengskysama  
       2014-05-07 19:09:40 +08:00
    你确定这个web前端能正确处理post请求?这个http连接会被断开的。
    代码本身是没问题的把代码post改成get可以返回,html提交入口在哪。
    sandtears
        2
    sandtears  
       2014-05-07 20:34:25 +08:00
    你确定可以吗? 我在本地按照你写的构造 post 失败了
    alex321
        3
    alex321  
       2014-05-07 20:54:15 +08:00
    http_build_query
    hopestar86
        4
    hopestar86  
    OP
       2014-05-07 21:33:54 +08:00
    @sandtears

    <form action="http://www.freerun3size.com/api_handler.php" method="post"><br />
    site_key: <input type="text" name="site_key" value="SOnmitFRjJ7h" /><br />
    api_action: <input type="text" name="api_action" value="siteverify" /><br />
    <input type="submit" name="submit"/><br />
    </form>

    得到的结果
    {"data":"","status":0,"info":"SITEKEY_ERROR"}

    你本地form表单提交得到的也是空的吗?
    hopestar86
        5
    hopestar86  
    OP
       2014-05-07 21:35:46 +08:00
    @mengskysama
    form表单的html正常post提交没问题的,这个网站转义到别的空间curl和form表单都没问题,但是就是这个空间里curl提交就有问题,很奇怪,不知道是空间的什么限制
    sandtears
        6
    sandtears  
       2014-05-08 21:35:37 +08:00
    @hopestar86 应该是限制了机器提交,我用 python-urllib2 和命令行 curl 提交都失败了,这段 html 代码写成文件用 Chrome 打开提交就和你的效果一样了。。。

    你把 curl 加上 User-Agent 之类的东西试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2782 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 02:41 · PVG 10:41 · LAX 19:41 · JFK 22:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.