search
HomeBackend DevelopmentPHP Tutorialphp模拟用户自动在qq空间发表文章的步骤

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

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

 

 

[php] view plaincopy

 

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

 

 

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools