范德萨发而为
全部博文(392)
分类: Python/Ruby
2012-10-10 17:04:10
01 |
02 | !extension_loaded('curl') && die('The curl extension is not loaded.'); |
03 |
04 | $discuz_url = '';//论坛地址 |
05 | $login_url = $discuz_url .'/do.php?ac=login';//登录页地址 |
06 | $get_url = $discuz_url .'/space.php?do=home'; //我的帖子 |
07 |
08 | $post_fields = array(); |
09 | //以下两项不需要修改 |
10 | $post_fields['loginfield'] = 'username'; |
11 | $post_fields['loginsubmit'] = 'true'; |
12 | //用户名和密码,必须填写 |
13 | $post_fields['username'] = '你的用户名'; |
14 | $post_fields['password'] = '你的密码'; |
15 | $post_fields['refer'] = 'space.php?do=home'; |
16 |
17 | //获取表单FORMHASH |
18 | $ch = curl_init($login_url); |
19 | curl_setopt($ch, CURLOPT_HEADER, 0); |
20 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
21 | $contents = curl_exec($ch); |
22 | curl_close($ch); |
23 | preg_match('//i', $contents, $matches); |
24 | if(!empty($matches)) { |
25 | $formhash = $matches[1]; |
26 | } else { |
27 | die('Not found the forumhash.'); |
28 | } |
29 | $post_fields['formhash']=$formhash; |
30 | //POST数据,获取COOKIE |
31 | $cookie_file = dirname(__FILE__) . '/cookie.txt'; |
32 | //$cookie_file = tempnam('/tmp'); |
33 | $ch = curl_init($login_url); |
34 | curl_setopt($ch, CURLOPT_HEADER, 0); |
35 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
36 | curl_setopt($ch, CURLOPT_POST, 1); |
37 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); |
38 | curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); |
39 | curl_exec($ch); |
40 | curl_close($ch); |
41 |
42 | //带着上面得到的COOKIE获取需要登录后才能查看的页面内容 |
43 | $ch = curl_init($get_url); |
44 | curl_setopt($ch, CURLOPT_HEADER, 0); |
45 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); |
46 | curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); |
47 | $contents = curl_exec($ch); |
48 | curl_close($ch); |
49 |
50 | var_dump($contents); |
51 | ?> |