PHP simulates the way users automatically publish articles in QQ space

Source: Internet
Author: User
Tags array exit empty end header net return urlencode

We here is a simple use of PHP to simulate login and then to the QQ space to send articles of a simple program, there is a need for friends can refer to

 
 
  1. <?php
  2. Simulate get POST request function http://www.lai18.com
  3. /*
  4. Function Description:
  5. Function: Request the way can get,post, can send the cookie, save the Cookiefile file
  6. Parameters: $url-----Request URL $referer---Source URL $postdata The data used for post requests, ' for GET requests
  7. $cookie---------sent cookies $cookiefile-----saved cookiefile files
  8. Return value: Returns the obtained source code
  9. */
  10. function request ($url, $referer = ', $postdata = ', $cookie = ', $cookiefile = ') {
  11. Header settings
  12. $header = ';
  13. $header. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN";//Content request type
  14. $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;//Browser field
  15. $header. = "Referer:". $referer. " RN ";/Set Source Address
  16. $header. = "Cookie:". $cookie; Set cookies, default null
  17. Request method get post, via $postdata null---GET, non-empty----post
  18. if ($postdata = = ") $method = ' get ';
  19. else $method = ' POST ';
  20. Defines an array to create a stream
  21. $opts =array ();
  22. $opts [' http ']=array (' method ' => $method, ' header ' => $header, ' content ' => $postdata);
  23. Generate stream
  24. $context =stream_context_create ($opts);
  25. Send a request, get the source code
  26. $yuanma =file_get_contents ($url, False, $context);
  27. Whether you need to save cookies to a file, $cookiefile when not empty
  28. if ($cookiefile!= ') {
  29. Echo ' needs to save cookie<br> ';
  30. To determine the existence of the saved file, there is no creation
  31. if (!file_exists ($cookiefile)) {
  32. File_put_contents ($cookiefile, ");
  33. }
  34. Get cookies, save them
  35. $response =implode ("RN", $http _response_header);
  36. Matching cookies with a regular match
  37. $zengze = "/set-cookie: (. *?) Rn/";
  38. Preg_match_all ($zengze, $response, $cookie _arr);
  39. There is a match, save
  40. if (!emptyempty ($cookie _arr[1])) {
  41. $cookiestr =implode ('; ', $cookie _arr[1]);
  42. File_put_contents ($cookiefile, $COOKIESTR);
  43. Echo ' successfully save Cookie<br> ';
  44. }
  45. else echo ' does not match to cookie<br> ';
  46. }//end if ($cookiefile!= ')
  47. Back to Source
  48. return $yuanma;
  49. }//end function Request ($url, $referer, $postdata, $cookie, $cookiefile)
  50. Get the current script URL
  51. function Getcururl ()
  52. {
  53. if (!emptyempty ($_server["Request_uri"]))
  54. {
  55. $scriptName = $_server["Request_uri"];
  56. $nowurl = $scriptName;
  57. }
  58. Else
  59. {
  60. $scriptName = $_server["Php_self"];
  61. if (Emptyempty ($_server["query_string"]))
  62. {
  63. $nowurl = $scriptName;
  64. }
  65. Else
  66. {
  67. $nowurl = $scriptName. "?". $_server["Query_string"];
  68. }
  69. }
  70. return $nowurl;
  71. }
  72. Get the current file name
  73. $nowurl =getcururl ();
  74. Echo $nowurl;
  75. form output, when not submitted
  76. if (!isset ($_post[' QQ ')) {
  77. Echo ' <form method= ' post ' action= '. $nowurl. ' " >
  78. QQ Number: <input type= "text" name= "QQ" ><br>
  79. G_tk:<input type= "text" name= "G_TK" ><br>
  80. Title: <input type= "text" name= "title" ><br>
  81. Content: <input type= "text" name= "content" ><br>
  82. <input type= "Submit" value= "Publish article" >
  83. </form> ';
  84. Die ();
  85. }
  86. /*
  87. Submit parameter Description:
  88. $_post[' QQ '---users QQ
  89. $_post[' G_TK ']--This parameter is critical, to obtain this parameter, it is necessary to grasp the post address submitted at the time of publication after the Call of g_tk=123456789,
  90. Road post <a href= "http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789 g_tk=123456789" target= "_blank" The g_tk=123456789</a> in >http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789.
  91. $_post[' title ']---article title, not empty
  92. $_post[' content ']---article contents, not empty
  93. */
  94. Header (' content-type:text/html;charset=gb2312 ');
  95. Set_time_limit (0);
  96. Ob_end_clean ();
  97. Ob_start ();
  98. Get cookie file, no create, and exit program
  99. $cookiefile =dirname (__file__). ' \qq_cookie.txt ';
  100. if (!file_exists ($cookiefile)) {
  101. Echo ' Qq_cookie.txt does not exist, automatically created, please fill in the cookie<br> ';
  102. File_put_contents ($cookiefile, ");
  103. Die (' program exit ');
  104. }
  105. exist, read cookies
  106. else{
  107. $cookie =file_get_contents ($cookiefile);//Login Cookie
  108. $cookie =urlencode ($cookie);
  109. }
  110. Echo ' Cookie: '. $cookie. ' <br> ';
  111. Important information that forms a publishing page, post data, etc.
  112. QQ number
  113. if (Emptyempty ($_post[' QQ ')) Preg_match ('/[^0-9]/is ', $_post[' qq ')) Die (' QQ number is wrong, must number ');
  114. else $qq =$_post[' QQ '];//QQ number
  115. if (Emptyempty ($_post[' g_tk ')) preg_match ('/[^0-9]/is ', $_post[' G_TK '))) Die (' POST Important parameters g_tk illegal, must be numeric, please use the value of the Scratch packet ');
  116. $g _tk=$_post[' G_TK '];
  117. $title =emptyempty ($_post[' title])? Die (' Caption Not empty '): $_post[' titles '];//article title
  118. $content =emptyempty ($_post[' content ')? Die (' Contents not empty '): $_post[content];//Contents
  119. $category = ' personal diary ';//Category
  120. $fabiao = ' http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk= '. $g _tk;//Publishing Process page
  121. $referer = ' http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html ';//source page
  122. $r 1= ' http://user.qzone.qq.com/'. $qq. '/infocenter ';//list Access source page
  123. $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 data
  124. $postdata =urlencode ($postdata);
  125. Echo $postdata;
  126. Send a request, get the source code
  127. $yuanma =request ($fabiao, $r 1, $postdata, $cookie, ");
  128. if (Strpos ($yuanma, ' published successfully '), echo $title. ' Publish Success <br> ';
  129. else Echo ' Publish failed: Right-click the source, you can see the specific error '. $yuanma;
  130. ?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.