// Use a program to log on to a forum as an example. Function bbslogin ($ user_login, $ password, $ host, $ port = "80 "){ // The post data to be submitted $ Argv = array ('cookie '=> array ('user _ login' => $ user_login, 'Password' => $ password, '_ wp_http_referer' => '/bbpress/', 'Re' => '', 'Remember '=> true )); Foreach ($ argv ['cookies'] as $ key => $ value ){ $ Params [] = $ key. '='. $ value; } $ Params = implode ('&', $ params ); $ Header = "POST/bbpress/bb-login.php HTTP/1.1rn "; $ Header. = "Host: $ host: $ portrn "; $ Header. = "Content-Type: application/x-www-form-urlencodedrn "; $ Header. = "Content-Length:". strlen ($ params). "rn "; $ Header. = "Connection: Closernrn "; $ Header. = $ params; $ Fp = fsockopen ($ host, $ port ); Fputs ($ fp, $ header ); While (! Feof ($ fp )){ $ Str = fgets ($ fp ); // The following is your own logic code. Here we mainly simulate cookies for Synchronous login. If (! (Strpos ($ str, "Set-Cookie:") === false )){ $ Tmparray = explode ("", $ str ); $ Cookiearray = explode ("=", $ tmparray [1]); $ Cookiepaths = explode ("=", $ tmparray [6]); $ Cookiename = urldecode ($ cookiearray [0]); $ Cookievalue = urldecode (substr ($ cookiearray [1], 0, strlen ($ cookiearray [1])-1 )); $ Cookietime = time () + 3600*24*7; $ Cookiepath = urldecode (substr ($ cookiepaths [1], 0, strlen ($ cookiepaths [1])-1 )); Setcookie ($ cookiename, $ cookievalue, $ cookietime, $ cookiepath ); } } Fclose ($ fp ); } ?> // Three methods of php post Data // Php can post data in three ways: Curl, socket, and file_get_contents: /** * Socket version * Usage: * $ Post_string = "app = socket & version = beta "; * Request_by_socket ('Facebook. cn', '/restServer. php', $ post_string ); */ Function request_by_socket ($ remote_server, $ remote_path, $ post_string, $ port = 80, $ timeout = 30) { $ Socket = fsockopen ($ remote_server, $ port, $ errno, $ errstr, $ timeout ); If (! $ Socket) die ("$ errstr ($ errno )"); Fwrite ($ socket, "POST $ remote_path HTTP/1.0rn "); Fwrite ($ socket, "User-Agent: Socket Examplern "); Fwrite ($ socket, "HOST: $ remote_serverrn "); Fwrite ($ socket, "Content-type: application/x-www-form-urlencodedrn "); Fwrite ($ socket, "Content-length:". (strlen ($ post_string) + 8). 'rn '); Fwrite ($ socket, "Accept: */* rn "); Fwrite ($ socket, "rn "); Fwrite ($ socket, "mypost = $ post_stringrn "); Fwrite ($ socket, "rn "); $ Header = ""; While ($ str = trim (fgets ($ socket, 4096 ))){ $ Header. = $ str; } $ Data = ""; While (! Feof ($ socket )){ $ Data. = fgets ($ socket, 4096 ); } Return $ data; } /** * Curl version * Usage: * $ Post_string = "app = request & version = beta "; * Request_by_curl ('HTTP: // facebook.cn/restserver.php', then post_string ); */ Function request_by_curl ($ remote_server, $ post_string) { $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ remote_server ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, 'mypost = '. $ post_string ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); Curl_setopt ($ ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta "); $ Data = curl_exec ($ ch ); Curl_close ($ ch ); Return $ data; } /** * Other Versions * Usage: * $ Post_string = "app = request & version = beta "; * Request_by_other ('HTTP: // facebook.cn/restserver.php', then post_string ); */ Function request_by_other ($ remote_server, $ post_string) { $ Context = array ( 'Http' => array ( 'Method' => 'post ', 'Header' => 'content-type: application/x-www-form-urlencoded '. 'Rn '. 'user-Agent: Jimmy's POST Example beta '. 'Rn'. 'content-length: '. strlen ($ post_string) + 8, 'Content' => 'mypost = '. $ post_string) ); $ Stream_context = stream_context_create ($ context ); $ Data = file_get_contents ($ remote_server, false, $ stream_context ); Return $ data; } ?> |