PHP analog post submission data, a lot of useful, can be used for site collection, landing and so on
-
- Log in as a program login as an example
- function Bbslogin ($user _login, $password, $host, $port = "80") {
- Post data that needs to be submitted
- $ARGV = Array (' cookie ' = = Array (' user_login ' = = $user _login, ' password ' + $password, ' _wp_http_referer ' = '/ bbpress/', ' re ', ' = ', ' remember ' = true);
- foreach ($argv [' Cookie '] as $key = + $value) {
- $params [] = $key. '=' . $value;
- }
- $params = Implode (' & ', $params);
- $header = "post/bbpress/bb-login.php http/1.1\r\n";
- $header. = "Host: $host: $port \ r \ n";
- $header. = "content-type:application/x-www-form-urlencoded\r\n";
- $header. = "Content-length:". Strlen ($params). "\ r \ n";
- $header. = "connection:close\r\n\r\n";
- $header. = $params;
- $fp = Fsockopen ($host, $port);
- Fputs ($fp, $header);
- while (!feof ($fp)) {
- $str = fgets ($FP);
- Here is your own logic code, here is the main analog cookie, can be used to synchronize 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);
- }
- ?>
Copy Code
- Three ways to post data in PHP
- PHP has three ways to post data, namely curl, socket, file_get_contents:
- /**
- * Socket version
- * How to use:
- * $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 = up, $timeout = 30)
- {
- $socket = Fsockopen ($remote _server, $port, $errno, $errstr, $timeout);
- if (! $socket) Die ("$errstr ($errno)");
- Fwrite ($socket, "POST $remote _path http/1.0\r\n");
- Fwrite ($socket, "User-agent:socket example\r\n");
- Fwrite ($socket, "HOST: $remote _server\r\n");
- Fwrite ($socket, "content-type:application/x-www-form-urlencoded\r\n");
- Fwrite ($socket, "Content-length:".) (Strlen ($post _string) + 8). ' \ r \ n ');
- Fwrite ($socket, "accept:*/*\r\n");
- Fwrite ($socket, "\ r \ n");
- Fwrite ($socket, "mypost= $post _string\r\n");
- Fwrite ($socket, "\ r \ n");
- $header = "";
- while ($str = Trim (fgets ($socket, 4096))) {
- $header. = $str;
- }
- $data = "";
- while (!feof ($socket)) {
- $data. = Fgets ($socket, 4096);
- }
- return $data;
- }
- /**
- * Curl Version
- * How to use:
- * $post _string = "App=request&version=beta";
- * Request_by_curl (' http://facebook.cn/restServer.php ', $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
- * How to use:
- * $post _string = "App=request&version=beta";
- * Request_by_other (' http://facebook.cn/restServer.php ', $post _string);
- */
- function Request_by_other ($remote _server, $post _string)
- {
- $context = Array (
- ' http ' = = Array (
- ' Method ' = ' POST ',
- ' Header ' = ' content-type:application/x-www-form-urlencoded '.
- ' \ r \ n '. ' User-agent:jimmy\ ' s POST Example beta '.
- ' \ r \ n '. ' 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;
- }
- ?>
Copy Code |