Maison >développement back-end >tutoriel php >php模拟用户自动在qq空间发表文章的方法_PHP教程
我们这里是一个简单的利用php来模拟登录后再到QQ空间发送文章的一个简单的程序,有需要的朋友可以参考
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//模拟get post请求函数 http://www.lai18.com</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">/* </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">函数说明: </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">功能:请求方式可以get,post,可以发送的cookie,保存的cookiefile文件 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">参数:$url-----请求url $referer---来源url $postdata----------用于post请求的数据,''为get请求 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">$cookie---------发送的cookie $cookiefile-----保存的cookiefile文件 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">返回值:返回获取的源码 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">*/</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">function</span><span> request(</span><span class="vars">$url</span><span>,</span><span class="vars">$referer</span><span>=</span><span class="string">''</span><span>,</span><span class="vars">$postdata</span><span>=</span><span class="string">''</span><span>,</span><span class="vars">$cookie</span><span>=</span><span class="string">''</span><span>,</span><span class="vars">$cookiefile</span><span>=</span><span class="string">''</span><span>){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//header设置 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$header</span><span>=</span><span class="string">''</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$header</span><span>.=</span><span class="string">"Content-Type: application/x-www-form-urlencodedrn"</span><span>;</span><span class="comment">//内容请求类型 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$header</span><span>.=</span><span class="string">"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"</span><span>;</span><span class="comment">//浏览器字段 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$header</span><span>.=</span><span class="string">"Referer:"</span><span>.</span><span class="vars">$referer</span><span>.</span><span class="string">"rn"</span><span>;</span><span class="comment">//设置来源地址 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$header</span><span> .= </span><span class="string">"Cookie:"</span><span>.</span><span class="vars">$cookie</span><span> ; </span><span class="comment">//设置cookie,默认空 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//请求方法get post,通过$postdata空---get,非空----post </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$postdata</span><span>==</span><span class="string">''</span><span>)</span><span class="vars">$method</span><span>=</span><span class="string">'GET'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span><span class="vars">$method</span><span>=</span><span class="string">'POST'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//定义用于创建流的数组 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$opts</span><span>=</span><span class="keyword">array</span><span>(); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$opts</span><span>[</span><span class="string">'http'</span><span>]=</span><span class="keyword">array</span><span>(</span><span class="string">'method'</span><span>=></span><span class="vars">$method</span><span>,</span><span class="string">'header'</span><span>=></span><span class="vars">$header</span><span>,</span><span class="string">'content'</span><span>=></span><span class="vars">$postdata</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//生成流 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$context</span><span>=stream_context_create(</span><span class="vars">$opts</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//发送请求,获取源码 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$yuanma</span><span>=</span><span class="func">file_get_contents</span><span>(</span><span class="vars">$url</span><span>,false,</span><span class="vars">$context</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//是否需要保存cookie到文件,$cookiefile不空时 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$cookiefile</span><span>!=</span><span class="string">''</span><span>){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'需要保存cookie<br>'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//判断保存文件存在,不存在创建 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(!</span><span class="func">file_exists</span><span>(</span><span class="vars">$cookiefile</span><span>)){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">file_put_contents</span><span>(</span><span class="vars">$cookiefile</span><span>,</span><span class="string">''</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//获取cookie,保存起来 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$response</span><span>=implode(</span><span class="string">"rn"</span><span>,</span><span class="vars">$http_response_header</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//用正则匹配cookie </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$zengze</span><span>=</span><span class="string">"/Set-Cookie:(.*?)rn/"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>preg_match_all(<span class="vars">$zengze</span><span>,</span><span class="vars">$response</span><span>,</span><span class="vars">$cookie_arr</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//存在匹配,保存 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(!emptyempty(</span><span class="vars">$cookie_arr</span><span>[1])){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$cookiestr</span><span>=implode(</span><span class="string">';'</span><span>,</span><span class="vars">$cookie_arr</span><span>[1]); </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">file_put_contents</span><span>(</span><span class="vars">$cookiefile</span><span>,</span><span class="vars">$cookiestr</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'成功保存cookie<br>'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span><span class="func">echo</span><span> </span><span class="string">'没有匹配到cookie<br>'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>}<span class="comment">//end if($cookiefile!='') </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//返回源码 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$yuanma</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>}<span class="comment">//end function request($url,$referer,$postdata,$cookie,$cookiefile) </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//获得当前的脚本网址 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">function</span><span> GetCurUrl() </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(!emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">"REQUEST_URI"</span><span>])) </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$scriptName</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">"REQUEST_URI"</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$nowurl</span><span> = </span><span class="vars">$scriptName</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$scriptName</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">"PHP_SELF"</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">"QUERY_STRING"</span><span>])) </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$nowurl</span><span> = </span><span class="vars">$scriptName</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$nowurl</span><span> = </span><span class="vars">$scriptName</span><span>.</span><span class="string">"?"</span><span>.</span><span class="vars">$_SERVER</span><span>[</span><span class="string">"QUERY_STRING"</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$nowurl</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//获得当前文件名 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$nowurl</span><span>=GetCurUrl(); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//echo $nowurl; </span><span> </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//表单输出,没有提交时 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(!isset(</span><span class="vars">$_POST</span><span>[</span><span class="string">'qq'</span><span>])){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'<form method="post" action="'</span><span>.</span><span class="vars">$nowurl</span><span>.'"> </span></span></li><li><span> </span></li><li class="alt"><span>qq号码:<input type=<span class="string">"text"</span><span> name=</span><span class="string">"qq"</span><span>><br> </span></span></li><li><span> </span></li><li class="alt"><span>g_tk:<input type=<span class="string">"text"</span><span> name=</span><span class="string">"g_tk"</span><span>><br> </span></span></li><li><span> </span></li><li class="alt"><span>标题:<input type=<span class="string">"text"</span><span> name=</span><span class="string">"title"</span><span>><br> </span></span></li><li><span> </span></li><li class="alt"><span>内容:<input type=<span class="string">"text"</span><span> name=</span><span class="string">"content"</span><span>><br> </span></span></li><li><span> </span></li><li class="alt"><span><input type=<span class="string">"submit"</span><span> value=</span><span class="string">"发表文章"</span><span>> </span></span></li><li><span> </span></li><li class="alt"><span></form>'; </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">die</span><span>(); </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="comment">/* </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">提交参数说明: </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">$_POST['qq']---用户QQ </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">$_POST['g_tk']--这个参数很关键,获得这个参数,需要抓下发表时提交的post地址后面调用的g_tk=123456789, </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">路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> </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">$_POST['title']---文章标题,不得空 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">$_POST['content']---文章内容,不得空 </span> </span></li><li><span><span class="comment"> </span> </span></li><li class="alt"><span><span class="comment">*/</span><span> </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span>header(<span class="string">'Content-Type:text/html;charset=gb2312'</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>set_time_limit(0); </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//ob_end_clean(); </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//ob_start(); </span><span> </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//获取cookie文件,不存在创建,并退出程序 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$cookiefile</span><span>=dirname(</span><span class="keyword">__FILE__</span><span>).</span><span class="string">'\qq_cookie.txt'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(!</span><span class="func">file_exists</span><span>(</span><span class="vars">$cookiefile</span><span>)){ </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'qq_cookie.txt不存在,自动创建,请填写抓包的cookie<br>'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">file_put_contents</span><span>(</span><span class="vars">$cookiefile</span><span>,</span><span class="string">''</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">die</span><span>(</span><span class="string">'程序退出'</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//存在,读取cookie </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span>{ </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$cookie</span><span>=</span><span class="func">file_get_contents</span><span>(</span><span class="vars">$cookiefile</span><span>);</span><span class="comment">//登录cookie </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//$cookie=urlencode($cookie); </span><span> </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//echo 'cookie:'.$cookie.'<br>'; </span><span> </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="comment">//构成发表页,post数据等的重要信息 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//qq号码 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(emptyempty(</span><span class="vars">$_POST</span><span>[</span><span class="string">'qq'</span><span>])||preg_match(</span><span class="string">'/[^0-9]/is'</span><span>,</span><span class="vars">$_POST</span><span>[</span><span class="string">'qq'</span><span>]))</span><span class="keyword">die</span><span>(</span><span class="string">'qq号码有误,必须数字'</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span><span class="vars">$qq</span><span>=</span><span class="vars">$_POST</span><span>[</span><span class="string">'qq'</span><span>];</span><span class="comment">//qq号 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(emptyempty(</span><span class="vars">$_POST</span><span>[</span><span class="string">'g_tk'</span><span>])||preg_match(</span><span class="string">'/[^0-9]/is'</span><span>,</span><span class="vars">$_POST</span><span>[</span><span class="string">'g_tk'</span><span>]))</span><span class="keyword">die</span><span>(</span><span class="string">'post重要参数g_tk不合法,必须数字,请使用抓包的值'</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$g_tk</span><span>=</span><span class="vars">$_POST</span><span>[</span><span class="string">'g_tk'</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$title</span><span>=emptyempty(</span><span class="vars">$_POST</span><span>[</span><span class="string">'title'</span><span>])?</span><span class="keyword">die</span><span>(</span><span class="string">'标题不得空'</span><span>):</span><span class="vars">$_POST</span><span>[</span><span class="string">'title'</span><span>];</span><span class="comment">//文章标题 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$content</span><span>=emptyempty(</span><span class="vars">$_POST</span><span>[</span><span class="string">'content'</span><span>])?</span><span class="keyword">die</span><span>(</span><span class="string">'内容不得空'</span><span>):</span><span class="vars">$_POST</span><span>[</span><span class="string">'content'</span><span>];</span><span class="comment">//内容 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span><span class="vars">$category</span><span>=</span><span class="string">'个人日记'</span><span>;</span><span class="comment">//分类 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$fabiao</span><span>=</span><span class="string">'http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='</span><span>.</span><span class="vars">$g_tk</span><span>;//发表处理页 </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$referer</span><span>=</span><span class="string">'http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html'</span><span>;//来源页 </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$r1</span><span>=</span><span class="string">'http://user.qzone.qq.com/'</span><span>.</span><span class="vars">$qq</span><span>.</span><span class="string">'/infocenter'</span><span>;//列表访问来源页 </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$postdata</span><span>=</span><span class="string">'uin='</span><span>.</span><span class="vars">$qq</span><span>.</span><span class="string">'&category='</span><span>.urlencode(</span><span class="vars">$category</span><span>).</span><span class="string">'&title='</span><span>.urlencode(</span><span class="vars">$title</span><span>).</span><span class="string">'&content='</span><span>.urlencode(</span><span class="vars">$content</span><span>).</span><span class="string">'&html='</span><span>.urlencode(</span><span class="string">'<div class="blog_details_20110920">'</span><span>.</span><span class="vars">$content</span><span>.</span><span class="string">'</div>'</span><span>).</span><span class="string">'&tweetflag=0&cb_autograph=1&topflag=0&needfeed=0&g_tk='</span><span>.</span><span class="vars">$g_tk</span><span>.</span><span class="string">'&_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'</span><span>.</span><span class="vars">$qq</span><span>.</span><span class="string">'%2Fmain'</span><span>;</span><span class="comment">//post数据 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//$postdata=urlencode($postdata); </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//echo $postdata; </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//发送请求,获取源码 </span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$yuanma</span><span>=request(</span><span class="vars">$fabiao</span><span>,</span><span class="vars">$r1</span><span>,</span><span class="vars">$postdata</span><span>,</span><span class="vars">$cookie</span><span>,</span><span class="string">''</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="func">strpos</span><span>(</span><span class="vars">$yuanma</span><span>,</span><span class="string">'发表成功'</span><span>))</span><span class="func">echo</span><span> </span><span class="vars">$title</span><span>.</span><span class="string">' 发表成功<br>'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">else</span><span> </span><span class="func">echo</span><span> </span><span class="string">'发表失败:右键查看源码,可以看到具体错误'</span><span>.</span><span class="vars">$yuanma</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>?> </span></li></ol>