日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

!!!!! php 使用curl模擬登錄discuz以及模擬發(fā)帖 | SimonLeung's Blo...

 看見就非常 2012-09-26

php的curl真的是相當好用,網(wǎng)上一搜索相關(guān)文章都是關(guān)于curl模擬登陸的,很少人提供模擬discuz發(fā)貼的源碼。

本著共享的原則,我把自己測試成功的發(fā)帖代碼貼出來。不足的地方,希望大家指正。

 
01 <div><?php
02 $discuz_url ';//論壇地址
03 $login_url $discuz_url .'logging.php?action=login';//登錄頁地址
04  
05 $post_fields array();
06 //以下兩項不需要修改
07 $post_fields['loginfield'] = 'username';
08 $post_fields['loginsubmit'] = 'true';
09 //用戶名和密碼,必須填寫
10 $post_fields['username'] = 'tianxin';
11 $post_fields['password'] = '111111';
12 //安全提問
13 $post_fields['questionid'] = 0;
14 $post_fields['answer'] = '';
15 //@todo驗證碼
16 $post_fields['seccodeverify'] = '';
17  
18 //獲取表單FORMHASH
19 $ch = curl_init($login_url);
20 curl_setopt($ch, CURLOPT_HEADER, 0);
21 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
22 $contents = curl_exec($ch);
23 curl_close($ch);
24 preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i'$contents$matches);
25 if(!empty($matches)) {
26  $formhash $matches[1];
27 else {
28  die('Not found the forumhash.');
29 }
30  
31 //POST數(shù)據(jù),獲取COOKIE,cookie文件放在網(wǎng)站的temp目錄下
32 $cookie_file = tempnam('./temp','cookie');
33  
34 $ch = curl_init($login_url);
35 curl_setopt($ch, CURLOPT_HEADER, 0);
36 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
37 curl_setopt($ch, CURLOPT_POST, 1);
38 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
39 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
40 curl_exec($ch);
41 curl_close($ch);
42  
43 //取到了關(guān)鍵的cookie文件就可以帶著cookie文件去模擬發(fā)帖,fid為論壇的欄目ID
44 $send_url $discuz_url."post.php?action=newthread&fid=2";
45  
46 $ch = curl_init($send_url);
47 curl_setopt($ch, CURLOPT_HEADER, 0);
48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
50 $contents = curl_exec($ch);
51 curl_close($ch);
52  
53 //這里的hash碼和登陸窗口的hash碼的正則不太一樣,這里的hidden多了一個id屬性
54 preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i'$contents$matches);
55 if(!empty($matches)) {
56  $formhash $matches[1];
57 else {
58  die('Not found the forumhash.');
59 }
60  
61 $post_data array();
62 //帖子標題
63 $post_data['subject'] = 'test2';
64 //帖子內(nèi)容
65 $post_data['message'] = 'test2';
66 $post_data['topicsubmit'] = "yes";
67 $post_data['extra'] = '';
68 //帖子標簽
69 $post_data['tags'] = 'test';
70 //帖子的hash碼,這個非常關(guān)鍵!假如缺少這個hash碼,discuz會警告你來路的頁面不正確
71 $post_data['formhash']=$formhash;
72  
73 $ch = curl_init($send_url);
74 curl_setopt($ch, CURLOPT_REFERER, $send_url);       //偽裝REFERER
75 curl_setopt($ch, CURLOPT_HEADER, 0);
76 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
77 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
78 curl_setopt($ch, CURLOPT_POST, 1);
79 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
80 $contents = curl_exec($ch);
81 curl_close($ch);
82  
83 //清理cookie文件
84 unlink($cookie_file);
85  
86 ?></div>

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約