This article describes the PHP simulation user automatically in the QQ space to publish the article method. Share to everyone for your reference. The specific analysis is as follows:
We here is a simple use of PHP to simulate login and then to the QQ space to send an article of a simple program, there is a need for friends can refer to, or improve can give me advice, the code is as follows:
Copy Code code as follows:
<?php
Simulate get POST request function
/*
Function Description:
Function: Request the way can get,post, can send the cookie, save the Cookiefile file
Parameters: $url-----Request URL $referer---Source URL $postdata The data used for post requests, ' for GET requests
$cookie---------sent cookies $cookiefile-----saved cookiefile files
Return value: Returns the obtained source code
*/
function request ($url, $referer = ', $postdata = ', $cookie = ', $cookiefile = ') {
Header settings
$header = ';
$header. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN";//Content request type
$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
$header. = "Referer:". $referer. " RN ";/Set Source Address
$header. = "Cookie:". $cookie; Set cookies, default null
Request method get post, via $postdata null---GET, non-empty----post
if ($postdata = = ") $method = ' get ';
else $method = ' POST ';
Defines an array to create a stream
$opts =array ();
$opts [' http ']=array (' method ' => $method, ' header ' => $header, ' content ' => $postdata);
Generate stream
$context =stream_context_create ($opts);
Send a request, get the source code
$yuanma =file_get_contents ($url, False, $context);
Whether you need to save cookies to a file, $cookiefile when not empty
if ($cookiefile!= ') {
Echo ' needs to save cookie<br> ';
To determine the existence of the saved file, there is no creation
if (!file_exists ($cookiefile)) {
File_put_contents ($cookiefile, ");
}
Get cookies, save them
$response =implode ("RN", $http _response_header);
Matching cookies with a regular match
$zengze = "/set-cookie: (. *?) Rn/";
Preg_match_all ($zengze, $response, $cookie _arr);
There is a match, save
if (!emptyempty ($cookie _arr[1])) {
$cookiestr =implode ('; ', $cookie _arr[1]);
File_put_contents ($cookiefile, $COOKIESTR);
Echo ' successfully save Cookie<br> ';
}
else echo ' does not match to cookie<br> ';
}//end if ($cookiefile!= ')
Back to Source
return $yuanma;
}//end function Request ($url, $referer, $postdata, $cookie, $cookiefile)
Get the current script URL
function Getcururl ()
{
if (!emptyempty ($_server["Request_uri"]))
{
$scriptName = $_server["Request_uri"];
$nowurl = $scriptName;
}
Else
{
$scriptName = $_server["Php_self"];
if (Emptyempty ($_server["query_string"]))
{
$nowurl = $scriptName;
}
Else
{
$nowurl = $scriptName. "?". $_server["Query_string"];
}
}
return $nowurl;
}
Get the current file name
$nowurl =getcururl ();
Echo $nowurl;
form output, when not submitted
if (!isset ($_post[' QQ ')) {
Echo ' <form method= ' post ' action= '. $nowurl. ' " >
QQ Number: <input type= "text" name= "QQ" ><br>
G_tk:<input type= "text" name= "G_TK" ><br>
Title: <input type= "text" name= "title" ><br>
Content: <input type= "text" name= "content" ><br>
<input type= "Submit" value= "Publish article" >
</form> ';
Die ();
}
/*
Submit parameter Description:
$_post[' QQ '---users QQ
$_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,
g_tk=123456789 in the road post http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789
$_post[' title ']---article title, not empty
$_post[' content ']---article contents, not empty
*/
Header (' content-type:text/html;charset=gb2312 ');
Set_time_limit (0);
Ob_end_clean ();
Ob_start ();
Get cookie file, no create, and exit program
$cookiefile =dirname (__file__). ' \qq_cookie.txt ';
if (!file_exists ($cookiefile)) {
Echo ' Qq_cookie.txt does not exist, automatically created, please fill in the cookie<br> ';
File_put_contents ($cookiefile, ");
Die (' program exit ');
}
exist, read cookies
else{
$cookie =file_get_contents ($cookiefile);//Login Cookie
$cookie =urlencode ($cookie);
}
Echo ' Cookie: '. $cookie. ' <br> ';
Important information that forms a publishing page, post data, etc.
QQ number
if (Emptyempty ($_post[' QQ ')) | | Preg_match ('/[^0-9]/is ', $_post[' qq ')) Die (' QQ number is wrong, must be number ');
else $qq =$_post[' QQ '];//QQ number
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 bag ');
$g _tk=$_post[' G_TK '];
$title =emptyempty ($_post[' title])? Die (' Caption Not empty '): $_post[' titles '];//article title
$content =emptyempty ($_post[' content ')? Die (' Contents not empty '): $_post[content];//Contents
$category = ' personal diary ';//Category
$fabiao = ' http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk= '. $g _tk;//Publishing Process page
$referer = ' http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html ';//source page
$r 1= ' http://user.qzone.qq.com/'. $qq. '/infocenter ';//list Access source page
$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
$postdata =urlencode ($postdata);
Echo $postdata;
Send a request, get the source code
$yuanma =request ($fabiao, $r 1, $postdata, $cookie, ");
if (Strpos ($yuanma, ' published successfully '), echo $title. ' Publish Success <br> ';
else Echo ' Publish failed: Right-click the source, you can see the specific error '. $yuanma;
?>
php Script:Note that the need to save the name is automatically recognized, I am named qq_fabiao.php, then set the submit address, cookie file qq_cookie.txt need to fill in the grab bag to get the space login cookie to authenticate through login, Qq_ Cookie.txt with PHP files in the same directory.
Submit parameter Description:
$_post[' QQ '---users QQ
$_post[' G_TK ']--This parameter is critical, to get this parameter, you need to grab the post address that is submitted after the publication of the G_tk=123456789,post http://b1.qzone.qq.com/cgi-bin/blognew/ The g_tk=123456789 in blog_add?g_tk=123456789.
$_post[' title ']---article title, not empty
$_post[' content ']---article contents, not empty
I hope this article will help you with your PHP program design.