php的curl真的是相當好用,網(wǎng)上一搜索相關(guān)文章都是關(guān)于curl模擬登陸的,很少人提供模擬discuz發(fā)貼的源碼。
本著共享的原則,我把自己測試成功的發(fā)帖代碼貼出來。不足的地方,希望大家指正。
03 |
$login_url = $discuz_url . 'logging.php?action=login' ; |
05 |
$post_fields = array (); |
07 |
$post_fields [ 'loginfield' ] = 'username' ; |
08 |
$post_fields [ 'loginsubmit' ] = 'true' ; |
10 |
$post_fields [ 'username' ] = 'tianxin' ; |
11 |
$post_fields [ 'password' ] = '111111' ; |
13 |
$post_fields [ 'questionid' ] = 0; |
14 |
$post_fields [ 'answer' ] = '' ; |
16 |
$post_fields [ 'seccodeverify' ] = '' ; |
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 ); |
24 |
preg_match( '/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i' , $contents , $matches ); |
25 |
if (! empty ( $matches )) { |
26 |
$formhash = $matches [1]; |
28 |
die ( 'Not found the forumhash.' ); |
32 |
$cookie_file = tempnam( './temp' , 'cookie' ); |
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 ); |
44 |
$send_url = $discuz_url . "post.php?action=newthread&fid=2" ; |
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 ); |
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]; |
58 |
die ( 'Not found the forumhash.' ); |
63 |
$post_data [ 'subject' ] = 'test2' ; |
65 |
$post_data [ 'message' ] = 'test2' ; |
66 |
$post_data [ 'topicsubmit' ] = "yes" ; |
67 |
$post_data [ 'extra' ] = '' ; |
69 |
$post_data [ 'tags' ] = 'test' ; |
71 |
$post_data [ 'formhash' ]= $formhash ; |
73 |
$ch = curl_init( $send_url ); |
74 |
curl_setopt( $ch , CURLOPT_REFERER, $send_url ); |
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 ); |
|