ホームページ  >  記事  >  バックエンド開発  >  CURL リモート ログインとリモート投稿に関連する問題

CURL リモート ログインとリモート投稿に関連する問題

WBOY
WBOYオリジナル
2016-06-13 13:33:061042ブラウズ

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 highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$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=16
if(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 highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$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 コード
$login_url = "http://www.czhi-fi.com/bbsxp/login.asp";//ログインページのアドレス
$source_url = "http://www.czhi-fi.com/bbsxp/default.asp";//ソース ページのアドレス
$post_fields = 配列();
//以下の2つの固定値
/*$post_fields['メニュー'] = '追加';
$post_fields['url'] = 'http://www.czhi-fi.com/bbsxp/Default.asp';

//ユーザー名とパスワードを入力する必要があります
$post_fields['ユーザー名'] = "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);
カール_クローズ($ch);
エコー $content; <div class="clear"></div>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。