当前位置: 代码迷 >> PHP >> curl 远程登录 以及远程发贴有关问题
  详细解决方案

curl 远程登录 以及远程发贴有关问题

热度:69   发布时间:2016-04-29 01:23:29.0
curl 远程登录 以及远程发贴问题
http://www.czhi-fi.com/bbsxp/ 论坛 是个bbsxp论坛系统的 是5.15版本。
我用snoopy可以远程登录并发贴。而用curl却无法实现远程登录,更不要说远程发贴了。
我很想用curl实现模拟登录,恳请高手赐教!!拜谢!!


说明:
登录地址:http://www.czhi-fi.com/bbsxp/login.asp
用户名 :mytest
密 码 :111111

首先把我成功用snoopy远程登录并发贴的代码公开:
PHP code
$url = "http://www.czhi-fi.com/bbsxp/";  //论坛地址$url1 = "http://www.czhi-fi.com/bbsxp/default.asp";  //源地址$url2 = "http://www.czhi-fi.com/bbsxp/login.asp";  //登录提交判断页$posts = array();$posts['menu'] = 'add';       $posts['url'] = $url1 ; $posts['username'] = 'mytest'; $posts['userpass'] = '111111'; include("Snoopy.class.php");   $snoopy = new Snoopy;   $snoopy->submit($url2,$posts); //模拟浏览器提交并取得返回结果  /*下面是发贴*/$url3 = "http://www.czhi-fi.com/bbsxp/newtopic.asp";  //发贴提交页面。$topic = '经典笑话:离婚';  //标题$content = '丈大:“孩子他娘。咱们离婚好吗?”妻子:“咱们从来下吵不闹,为什么要离婚?”丈夫:“我的意思是,离婚以后我再和你结婚。”妻子:“啊、你今天发神经疯了还是怎么着?”丈夫:“没有啊!你看,这几年年轻人结婚,把我的腰包都掏空了,如果我们不再结一次婚,这些践怎么捞得回来哟?';$posts = array();$posts['forumid'] = '16';      //板块id号  这个是灌水板块     $posts['topic'] = $topic ;  //标题$posts['content'] = $content ;  //内容$snoopy->submit($url3,$posts); //模拟浏览器提交并取得返回结果  //print_r($snoopy->results);$content = $snoopy->results ;//http://www.czhi-fi.com/bbsxp/ShowPost.asp?id=3148//ShowForum.asp?forumid=16if(preg_match("/\"(\d+)\",\"[^\r\n\"]*?\",\"[^\r\n\"]*?\",\"mytest\"/is",$content, $matches)){  if(!empty($matches[1])){    $out = $url . "ShowPost.asp?id=".$matches[1] ;  }else{    $out = "没有采集到刚发贴的地址,<a href=\"".$url."ShowForum.asp?forumid=16\" target=\"_blank\">查看此板块内有没有新发贴</a>";  }}else{  $out = "发贴可能失败了,<a href=\"".$url."ShowForum.asp?forumid=16\" target=\"_blank\">查看此板块内有没有新发贴</a>";}echo "<a href=\"$out\" target=\"_blank\">".$out."</a>" ;

*************************************
 下面是我的curl登录代码,这个代码不能实现远程登录,请高人指点!
PHP code
$login_url = "http://www.czhi-fi.com/bbsxp/login.asp";//登录页地址$source_url = "http://www.czhi-fi.com/bbsxp/default.asp";//源页地址$post_fields = array();//以下两项固定值$post_fields['menu'] = 'add';$post_fields['url'] = 'http://www.czhi-fi.com/bbsxp/Default.asp';//用户名和密码,必须填写$post_fields['username'] = "mytest";$post_fields['userpass'] = "111111";$ch = curl_init($login_url);curl_setopt($ch, CURLOPT_REFERER, $source_url);curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 允许自动跳转$contents = curl_exec($ch);curl_close($ch);echo $contents ;



我很想用curl实现模拟登录,恳请高手赐教!!拜谢!!


------解决方案--------------------
cookie得获取(http 返回头中的set-Cookie项)并反馈过去(http请求头中的cookie项)。

PHP code
$login_url = "http://www.czhi-fi.com/bbsxp/login.asp";//登录页地址$source_url = "http://www.czhi-fi.com/bbsxp/default.asp";//源页地址$post_fields = array();//以下两项固定值/*$post_fields['menu'] = 'add';$post_fields['url'] = 'http://www.czhi-fi.com/bbsxp/Default.asp';//用户名和密码,必须填写$post_fields['username'] = "mytest";$post_fields['userpass'] = "111111";*/$cookie="cookie.txt";$ch = curl_init($login_url);curl_setopt($ch, CURLOPT_REFERER, $source_url);curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=mytest&userpass=111111&menu=add&url=http://www.czhi-fi.com/bbsxp/Default.asp');curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 允许自动跳转$contents = curl_exec($ch);curl_close($ch);echo $contents;