应公司老板要求,用curl做了一个模拟登陆网站(和讯博客)并提交数据的程序,
登陆部分可正常使用,但在发布博客文章的时候遇到了一点困难,自己实在搞不清楚问题在哪里。
我用curl的post方法提交数据的时候,发布失败,返回内容为空,返回头信息如下:HTTP/1.1 200 OK Server: nginx/0.8.15 Date: Thu, 29 Oct 2009 08:38:18 GMT Content-Type: text/html; 
charset=gb2312 Connection: close X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private 
Content-Length: 252 
用ie浏览器发布博客文章,借助一些抓包工具的时候返回内容正常,可以正常发布,返回头信息如下:
HTTP/1.1 200 OK
Connection:keep-alive
Content-Location:http://post.blog.hexun.com/xml/redirect.htm?404;http://post.blog.hexun.com:80/myw1796/postarticlesubmit.asp
Accept-Ranges:bytes
Content-Length:2329
Content-Type:text/html
Date:Thu, 29 Oct 2009 08:41:58 GMT
ETag:"4193b1e5ef4ca1:314"
Last-Modified:Wed, 15 Jul 2009 01:59:33 GMT
Server:nginx/0.8.15
X-Powered-By:ASP.NET
我用cur提交的时候返回的:Connection: close是什么意思啊?自己上学的时候对http协议没学好,是不是这个原因?
接下来我应当如何测试呢?

解决方案 »

  1.   

    附上我的curl的主要函数,我的源代码页面采用的是utf-8编码,里面采用了一些转换编码自己定义函数, function post($url,$refer=false,$data,$code,$jump=1)//post请求
    {
    if(strtolower($code) != "utf-8")
    {
    $data = iconv("utf-8","{$code}//IGNORE",$data);
    }
    $ch = curl_init(); // 启动一个CURL会话
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);// 模拟用户使用的浏览器 
    curl_setopt($ch,CURLOPT_AUTOREFERER,$refer ? $refer : 1); // 自动设置Referer  
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$jump); // 使用自动跳转
    curl_setopt($ch,CURLOPT_POST,1); // 发送一个常规的Post请求 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data); // Post提交的数据包
    curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookie); // 存放Cookie信息的文件名称
    curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookie); // 读取Cookie
      curl_setopt($ch,CURLOPT_TIMEOUT,30); // 设置超时限制防止死循环
    curl_setopt($ch,CURLOPT_HEADER,1); //不示返回的Header区域内容
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//设定返回的数据是否自动显示
    $info = curl_exec($ch);
    $info = $this->safeEncoding($info);
    curl_close($ch);
    return $info;
    }
      

  2.   

    同样,想通过post方式进行模拟登陆Photobucket.com,并上传一张图片, curl.exe photobucket.com -H "Content-type: application/x-www-form-urlencoded;" --data-ascii -d "loginForm%5Bredir%5D=&loginForm%5BredirectAction%5D=&loginForm%5BredirectParams%5D=&loginForm%5BreturnUrl%5D=&action=login&loginForm%5Busernameemail%5D=chinesealexander&loginForm%5Bpassword%5D=abcd" > photo5.html 以上操作不对,不知道应该怎么处理. 有人曾经处理过吗?