Php simulates the user's automatic posting of articles in the QQ space ,. Php simulates the user's automatic posting of articles in the QQ space. this example describes how php simulates the user's automatic posting of articles in the QQ space. Share it with you for your reference. For specific analysis, php simulates the method by which users can automatically Post articles in the QQ space,
The example in this article describes how php simulated users can automatically Post articles in the QQ space. Share it with you for your reference. The specific analysis is as follows:
Here is a simple program that uses php to simulate login and then send an article to the QQ space. if you need it, you can refer to it or give me some suggestions for improvement. the code is as follows:
The code is as follows:
<? Php
// Simulate the get post request function
/*
Function description:
Function: The request method can be get, post, cookie that can be sent, and saved cookiefile.
Parameter: $ url ----- request url $ referer --- source url $ postdata ---------- used for post request data, ''is a get request
$ Cookie --------- sent cookie $ cookiefile ----- saved cookiefile
Return value: return 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 the source address
$ Header. = "Cookie:". $ cookie; // sets the cookie. the default value is null.
// Request method get post, with $ postdata null --- get, non-null ---- post
If ($ postdata = '') $ method = 'get ';
Else $ method = 'post ';
// Define the array used to create a stream
$ Opts = array ();
$ Opts ['http'] = array ('method' => $ method, 'header' => $ header, 'content' => $ postdata );
// Generate a stream
$ Context = stream_context_create ($ opts );
// Send a request to obtain the source code
$ Yuanma = file_get_contents ($ url, false, $ context );
// Whether to save the cookie to the file. $ When cookiefile is not empty
If ($ cookiefile! = ''){
Echo 'The cookie needs to be saved
';
// Determine whether the saved file exists and does not exist
If (! File_exists ($ cookiefile )){
File_put_contents ($ cookiefile ,'');
}
// Obtain and save the cookie
$ Response = implode ("rn", $ http_response_header );
// Use the regular expression to match the cookie
$ Zengze = "/Set-Cookie :(.*?) Rn /";
Preg_match_all ($ zengze, $ response, $ cookie_arr );
// Match exists and save
If (! Emptyempty ($ cookie_arr [1]) {
$ Cookiestr = implode (';', $ cookie_arr [1]);
File_put_contents ($ cookiefile, $ cookiestr );
Echo 'successfully saved the cookie
';
}
Else echo 'does not match cookie
';
} // End if ($ cookiefile! = '')
// Return the source code
Return $ yuanma;
} // End function request ($ url, $ referer, $ postdata, $ cookie, $ cookiefile)
// Obtain 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;
}
// Obtain the current file name
$ Nowurl = GetCurUrl ();
// Echo $ nowurl;
// Form output, no submission
If (! Isset ($ _ POST ['QQ']) {
Echo '';
Die ();
}
/*
Description of submitted parameters:
$ _ POST ['QQ'] --- User qq
$ _ POST ['G _ tk '] -- this parameter is critical. to obtain this parameter, you need to capture the g_tk = 123456789,
Road POST http://b1.qzone.qq.com/cgi-bin/blognew/blog_add? G_tk = 123456789 in 123456789
$ _ POST ['title'] --- article title, which cannot be blank
$ _ POST ['content'] --- content of the article, which cannot be blank
*/
Header ('content-Type: text/html; charset = gb2312 ');
Set_time_limit (0 );
// Ob_end_clean ();
// Ob_start ();
// Obtain the cookie file. it does not exist and exits the program.
$ Cookiefile = dirname (_ FILE _). '\ qq_cookie.txt ';
If (! File_exists ($ cookiefile )){
Echo 'qq_cookie.txt does not exist. it is automatically created. please fill in the cookie for packet capture
';
File_put_contents ($ cookiefile ,'');
Die ('Program exited ');
}
// Exist, read cookie
Else {
$ Cookie = file_get_contents ($ cookiefile); // logon cookie
// $ Cookie = urlencode ($ cookie );
}
// Echo 'cookie: '. $ cookie .'
';
// Make up important information such as posting pages and post data
// QQ number
If (emptyempty ($ _ POST ['QQ']) | preg_match ('/[^ 0-9]/is', $ _ POST ['QQ']) die ('qq number is incorrect, it must be number ');
Else $ qq =$ _ POST ['QQ']; // qq number
If (emptyempty ($ _ POST ['G _ tk ']) | preg_match ('/[^ 0-9]/is ', $ _ POST ['G _ tk ']) die ('post's important parameter g_tk is invalid and must be a number. use the value of the captured Package ');
$ G_tk = $ _ POST ['G _ tk '];
$ Title = emptyempty ($ _ POST ['title'])? Die ('title cannot be blank '): $ _ POST ['title']; // Article title
$ Content = emptyempty ($ _ POST ['content'])? Die ('content not Null'): $ _ POST ['content']; // content
$ Category = 'personal diaries '; // category
$ Fabiao = 'http: // b1.qzone.qq.com/cgi-bin/blognew/blog_add? G_tk = '. $ g_tk; // post a processing page
$ Referer = 'http: // ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html'#//#page
$ R1 = 'http: // user.qzone.qq.com/'.{qq.'/infocenter'#//{ page
$ Postdata = 'uin = '. $ qq. '& category = '. urlencode ($ category ). '& title = '. urlencode ($ title ). '& content = '. urlencode ($ content ). '& html = '. urlencode ('
'. $ Content .'
Failed % 3 Fsource % 3D1% 3 Chttp % 3A % 2F % 2Fuser.qzone.qq.com % 2F '. $ qq.' % 2Fmain '; // post data
// $ Postdata = urlencode ($ postdata );
// Echo $ postdata;
// Send a request to obtain the source code
$ Yuanma = request ($ fabiao, $ r1, $ postdata, $ cookie ,'');
If (strpos ($ yuanma, 'post succeeded ') echo $ title.' Post successful
';
Else echo 'posting failed: Right-click to view the source code and you will see the specific error '. $ yuanma;
?>
Php script:The scripts and PHP files are in the same directory.
Description of submitted parameters:
$ _ POST ['QQ'] --- User qq
$ _ POST ['G _ tk '] -- this parameter is critical. to obtain this parameter, you need to capture the g_tk = 123456789, post http://b1.qzone.qq.com/cgi-bin/blognew/blog_add called after the POST address submitted at the time of posting? G_tk = 123456789 in 123456789
$ _ POST ['title'] --- article title, which cannot be blank
$ _ POST ['content'] --- content of the article, which cannot be blank
I hope this article will help you with php programming.
Example: This article describes how php simulates a user's automatic posting of an article in the QQ space. Share it with you for your reference. The specific analysis is as follows...