PHP头条
热点:

php模拟用户自动在qq空间发表文章的方法,


我们这里是一个简单的利用php来模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考,或改进可以给我意见,代码如下:

 

 

[php] view plaincopy  
  1. <?php   
  2.   
  3. //模拟get post请求函数 http://www.lai18.com  
  4.   
  5. /*  
  6.  
  7. 函数说明:  
  8.  
  9. 功能:请求方式可以get,post,可以发送的cookie,保存的cookiefile文件  
  10.  
  11. 参数:$url-----请求url    $referer---来源url    $postdata----------用于post请求的数据,''为get请求  
  12.  
  13. $cookie---------发送的cookie     $cookiefile-----保存的cookiefile文件  
  14.  
  15. 返回值:返回获取的源码  
  16.  
  17. */   
  18.   
  19. function request($url,$referer='',$postdata='',$cookie='',$cookiefile=''){   
  20.   
  21. //header设置   
  22.   
  23. $header='';   
  24.   
  25. $header.="Content-Type: application/x-www-form-urlencodedrn";//内容请求类型   
  26.   
  27. $header.="User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)rn";//浏览器字段   
  28.   
  29. $header.="Referer:".$referer."rn";//设置来源地址   
  30.   
  31. $header .= "Cookie:".$cookie ; //设置cookie,默认空   
  32.   
  33. //请求方法get post,通过$postdata空---get,非空----post   
  34.   
  35. if($postdata=='')$method='GET';   
  36.   
  37. else $method='POST';   
  38.   
  39. //定义用于创建流的数组   
  40.   
  41. $opts=array();   
  42.   
  43. $opts['http']=array('method'=>$method,'header'=>$header,'content'=>$postdata);   
  44.   
  45. //生成流   
  46.   
  47. $context=stream_context_create($opts);   
  48.   
  49. //发送请求,获取源码   
  50.   
  51. $yuanma=file_get_contents($url,false,$context);   
  52.   
  53. //是否需要保存cookie到文件,$cookiefile不空时   
  54.   
  55. if($cookiefile!=''){   
  56.   
  57. echo '需要保存cookie<br>';   
  58.   
  59. //判断保存文件存在,不存在创建   
  60.   
  61. if(!file_exists($cookiefile)){   
  62.   
  63. file_put_contents($cookiefile,'');   
  64.   
  65. }   
  66.   
  67. //获取cookie,保存起来   
  68.   
  69. $response=implode("rn",$http_response_header);   
  70.   
  71. //用正则匹配cookie   
  72.   
  73. $zengze="/Set-Cookie:(.*?)rn/";   
  74.   
  75. preg_match_all($zengze,$response,$cookie_arr);   
  76.   
  77. //存在匹配,保存   
  78.   
  79. if(!emptyempty($cookie_arr[1])){   
  80.   
  81. $cookiestr=implode(';',$cookie_arr[1]);   
  82.   
  83. file_put_contents($cookiefile,$cookiestr);   
  84.   
  85. echo '成功保存cookie<br>';   
  86.   
  87. }   
  88.   
  89. else echo '没有匹配到cookie<br>';   
  90.   
  91. }//end if($cookiefile!='')   
  92.   
  93. //返回源码   
  94.   
  95. return $yuanma;   
  96.   
  97. }//end function request($url,$referer,$postdata,$cookie,$cookiefile)    
  98.   
  99. //获得当前的脚本网址    
  100.   
  101. function GetCurUrl()    
  102.   
  103. {    
  104.   
  105. if(!emptyempty($_SERVER["REQUEST_URI"]))    
  106.   
  107. {    
  108.   
  109. $scriptName = $_SERVER["REQUEST_URI"];    
  110.   
  111. $nowurl = $scriptName;    
  112.   
  113. }    
  114.   
  115. else    
  116.   
  117. {    
  118.   
  119. $scriptName = $_SERVER["PHP_SELF"];    
  120.   
  121. if(emptyempty($_SERVER["QUERY_STRING"]))    
  122.   
  123. {    
  124.   
  125. $nowurl = $scriptName;    
  126.   
  127. }    
  128.   
  129. else    
  130.   
  131. {    
  132.   
  133. $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];    
  134.   
  135. }    
  136.   
  137. }    
  138.   
  139. return $nowurl;    
  140.   
  141. }   
  142.   
  143.    
  144.   
  145. //获得当前文件名   
  146.   
  147. $nowurl=GetCurUrl();   
  148.   
  149. //echo $nowurl;   
  150.   
  151.    
  152.   
  153. //表单输出,没有提交时   
  154.   
  155. if(!isset($_POST['qq'])){   
  156.   
  157. echo '<form method="post" action="'.$nowurl.'">   
  158.   
  159. qq号码:<input type="text" name="qq"><br>   
  160.   
  161. g_tk:<input type="text" name="g_tk"><br>   
  162.   
  163. 标题:<input type="text" name="title"><br>   
  164.   
  165. 内容:<input type="text" name="content"><br>   
  166.   
  167. <input type="submit" value="发表文章">   
  168.   
  169. </form>';   
  170.   
  171. die();   
  172.   
  173. }   
  174.   
  175.    
  176.   
  177. /*  
  178.  
  179. 提交参数说明:  
  180.  
  181. $_POST['qq']---用户QQ  
  182.  
  183. $_POST['g_tk']--这个参数很关键,获得这个参数,需要抓下发表时提交的post地址后面调用的g_tk=123456789,  
  184.  
  185. 路POST <a href="http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789" target="_blank">http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789</a>  
  186.  
  187. $_POST['title']---文章标题,不得空  
  188.  
  189. $_POST['content']---文章内容,不得空  
  190.  
  191. */   
  192.   
  193.    
  194.   
  195. header('Content-Type:text/html;charset=gb2312');   
  196.   
  197. set_time_limit(0);   
  198.   
  199. //ob_end_clean();   
  200.   
  201. //ob_start();   
  202.   
  203.    
  204.   
  205. //获取cookie文件,不存在创建,并退出程序   
  206.   
  207. $cookiefile=dirname(__FILE__).'\qq_cookie.txt';   
  208.   
  209. if(!file_exists($cookiefile)){   
  210.   
  211. echo 'qq_cookie.txt不存在,自动创建,请填写抓包的cookie<br>';   
  212.   
  213. file_put_contents($cookiefile,'');   
  214.   
  215. die('程序退出');   
  216.   
  217. }   
  218.   
  219. //存在,读取cookie   
  220.   
  221. else{   
  222.   
  223. $cookie=file_get_contents($cookiefile);//登录cookie   
  224.   
  225. //$cookie=urlencode($cookie);   
  226.   
  227. }   
  228.   
  229. //echo 'cookie:'.$cookie.'<br>';   
  230.   
  231.    
  232.   
  233. //构成发表页,post数据等的重要信息   
  234.   
  235. //qq号码   
  236.   
  237. if(emptyempty($_POST['qq'])||preg_match('/[^0-9]/is',$_POST['qq']))die('qq号码有误,必须数字');   
  238.   
  239. else $qq=$_POST['qq'];//qq号   
  240.   
  241. if(emptyempty($_POST['g_tk'])||preg_match('/[^0-9]/is',$_POST['g_tk']))die('post重要参数g_tk不合法,必须数字,请使用抓包的值');   
  242.   
  243. $g_tk=$_POST['g_tk'];   
  244.   
  245.    
  246.   
  247. $title=emptyempty($_POST['title'])?die('标题不得空'):$_POST['title'];//文章标题   
  248.   
  249. $content=emptyempty($_POST['content'])?die('内容不得空'):$_POST['content'];//内容   
  250.   
  251.    
  252.   
  253. $category='个人日记';//分类   
  254.   
  255. $fabiao='http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='.$g_tk;//发表处理页   
  256.   
  257. $referer='http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html';//来源页   
  258.   
  259. $r1='http://user.qzone.qq.com/'.$qq.'/infocenter';//列表访问来源页   
  260.   
  261. $postdata='uin='.$qq.'&category='.urlencode($category).'&title='.urlencode($title).'&content='.urlencode($content).'&html='.urlencode('<div class="blog_details_20110920">'.$content.'</div>').'&tweetflag=0&cb_autograph=1&topflag=0&needfeed=0&g_tk='.$g_tk.'&_fp_refer=http%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%7Chttp%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%3Chttp%3A%2F%2Fuser.qzone.qq.com%2F'.$qq.'%2Fmain';//post数据   
  262.   
  263. //$postdata=urlencode($postdata);   
  264.   
  265. //echo $postdata;   
  266.   
  267. //发送请求,获取源码   
  268.   
  269. $yuanma=request($fabiao,$r1,$postdata,$cookie,'');   
  270.   
  271. if(strpos($yuanma,'发表成功'))echo $title.'  发表成功<br>';   
  272.   
  273. else echo '发表失败:右键查看源码,可以看到具体错误'.$yuanma;   
  274.   
  275. ?>  

 

 

代码仅供参考,成功率有待大家的验证

www.phpzy.comtrue/php/36242.htmlTechArticlephp模拟用户自动在qq空间发表文章的方法, 我们这里是一个简单的利用php来模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考,或改进可以给我意见,代码如下: [p...

相关文章

    暂无相关文章

PHP之友评论

今天推荐