php通過curl類比登陸DZ論壇_PHP教程

來源:互聯網
上載者:User

php通過curl類比登陸DZ論壇


  本文章來給各位同學介紹一下關於Php CURL類比登陸論壇並採集資料執行個體,如果你對利用curl類比登入功能有興趣可進入參考。

  libcurl同時也支援HTTPS認證、HTTP POST、HTTP PUT、 FTP 上傳(這個也能通過PHP的FTP擴充完成)、HTTP 基於表單的上傳、代理、cookies和使用者名稱+密碼的認證。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

$discuz_url = 'http://www.jb51.net/';//論壇地址

$login_url = $discuz_url .'login.php?action=login';//登入頁地址

$post_fields = array();

//以下兩項不需要修改

$post_fields['loginfield'] = 'username';

$post_fields['loginsubmit'] = 'true';

//使用者名稱和密碼,必須填寫

$post_fields['username'] = 'tianxin';

$post_fields['password'] = '111111';

//安全提問

$post_fields['questionid'] = 0;

$post_fields['answer'] = '';

//@todo驗證碼

$post_fields['seccodeverify'] = '';

//擷取表單FORMHASH

$ch = curl_init($login_url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch);

curl_close($ch);

preg_match('//i', $contents, $matches);

if(!empty($matches)) {

$formhash = $matches[1];

} else {

die('Not found the forumhash.');

}

//POST資料,擷取COOKIE,cookie檔案放在網站的temp目錄下

$cookie_file = tempnam('./temp','cookie');

$ch = curl_init($login_url);

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_COOKIEJAR, $cookie_file);

curl_exec($ch);

curl_close($ch);

//取到了關鍵的cookie檔案就可以帶著cookie檔案去類比發帖,fid為論壇的欄目ID

$send_url = $discuz_url."post.php?action=newthread&fid=2";

$ch = curl_init($send_url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

$contents = curl_exec($ch);

curl_close($ch);

//這裡的hash碼和登陸視窗的hash碼的正則不太一樣,這裡的hidden多了一個id屬性

preg_match('//i', $contents, $matches);

if(!empty($matches)) {

$formhash = $matches[1];

} else {

die('Not found the forumhash.');

}

$post_data = array();

//文章標題

$post_data['subject'] = 'test2';

//文章內容

$post_data['message'] = 'test2';

$post_data['topicsubmit'] = "yes";

$post_data['extra'] = '';

//文章標籤

$post_data['tags'] = 'test';

//文章的hash碼,這個非常關鍵!假如缺少這個hash碼,discuz會警告你來路的頁面不正確

$post_data['formhash']=$formhash;

$ch = curl_init($send_url);

curl_setopt($ch, CURLOPT_REFERER, $send_url); //偽裝REFERER

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$contents = curl_exec($ch);

curl_close($ch);

//清理cookie檔案

unlink($cookie_file);

?>

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

http://www.bkjia.com/PHPjc/998364.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/998364.htmlTechArticlephp通過curl類比登陸DZ論壇 本文章來給各位同學介紹一下關於Php CURL類比登陸論壇並採集資料執行個體,如果你對利用curl類比登入功能有興趣可進...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.