最近要用到curl模拟登录并抓取页面信息,从网上看到了一段很不错的源代码。虽然我要抓取的目录不是discuz构架,不过毕竟有借鉴意义,先摘录过来再说。等有时间了再学习更改下。

 1<?php
 2$discuz_url = 'http://127.0.0.1/discuz/';//论坛地址
 3$login_url = $discuz_url .'logging.php?action=login';//登录页地址
 4$post_fields = array();
 5//以下两项不需要修改
 6$post_fields['loginfield'] = 'username';
 7$post_fields['loginsubmit'] = 'true';
 8//用户名和密码,必须填写
 9$post_fields['username'] = 'tianxin';
10$post_fields['password'] = '111111';
11//安全提问
12$post_fields['questionid'] = 0;
13$post_fields['answer'] = '';
14//@todo验证码
15$post_fields['seccodeverify'] = '';
16//获取表单FORMHASH
17$ch = curl_init($login_url);
18curl_setopt($ch, CURLOPT_HEADER, 0);
19curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20$contents = curl_exec($ch);
21curl_close($ch);
22preg_match('/<inputs*type="hidden"s*name="formhash"s*value="(.*?)"s*/>/i', $contents, $matches);
23if(!empty($matches)) {
24    $formhash = $matches[1];
25} else {
26    die('Not found the forumhash.');
27}
28//POST数据,获取COOKIE,cookie文件放在网站的temp目录下
29$cookie_file = tempnam('./temp','cookie');
30$ch = curl_init($login_url);
31curl_setopt($ch, CURLOPT_HEADER, 0);
32curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
33curl_setopt($ch, CURLOPT_POST, 1);
34curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
35curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
36curl_exec($ch);
37curl_close($ch);
38//取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
39$send_url = $discuz_url."post.php?action=newthread&fid=2";
40$ch = curl_init($send_url);
41curl_setopt($ch, CURLOPT_HEADER, 0);
42curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
43curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
44$contents = curl_exec($ch);
45curl_close($ch);
46//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
47preg_match('/<inputs*type="hidden"s*name="formhash"s*id="formhash"s*value="(.*?)"s*/>/i', $contents, $matches);
48if(!empty($matches)) {
49    $formhash = $matches[1];
50} else {
51    die('Not found the forumhash.');
52}
53$post_data = array();
54//帖子标题
55$post_data['subject'] = 'test2';
56//帖子内容
57$post_data['message'] = 'test2';
58$post_data['topicsubmit'] = "yes";
59$post_data['extra'] = '';
60//帖子标签
61$post_data['tags'] = 'test';
62//帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
63$post_data['formhash']=$formhash;
64$ch = curl_init($send_url);
65curl_setopt($ch, CURLOPT_REFERER, $send_url);       //伪装REFERER
66curl_setopt($ch, CURLOPT_HEADER, 0);
67curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
68curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
69curl_setopt($ch, CURLOPT_POST, 1);
70curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
71$contents = curl_exec($ch);
72curl_close($ch);
73//清理cookie文件
74unlink($cookie_file);
75?>