This article mainly introduces the PHP analog POST request Three common uses, the interest friend's reference, hoped to be helpful to everybody.
The example in this paper describes how PHP implements a simulated post request, as follows:
Class request{public static function post ($url, $post _data = ", $timeout = 5) {//curl $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, 1); if ($post _data! = ") {curl_setopt ($ch, Curlopt_postfields, $post _data); } curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); curl_setopt ($ch, Curlopt_header, false); $file _contents = curl_exec ($ch); Curl_close ($ch); return $file _contents; public static function Post2 ($url, $data =array ()) {//file_get_content $postdata = http_build_query ($data); $opts = Array (' http ' = = Array (' method ' = ' + ' POST ', ' header ' = ' + ' content-type:application/x-www- form-urlencoded ', ' content ' = $postdata)); $context = Stream_context_create ($opts); $result = File_get_contents ($url, False, $context); return $result; public static function Post3 ($host, $path, $query, $others = ") {//fsocket $post =" Post $path http/1.1\r\nhost: $host \ r \ n "; $post. = "content-type:application/x-www-form-"; $post. = "Urlencoded\r\n${others}"; $post. = "User-agent:mozilla 4.0\r\ncontent-length:"; $post. =strlen ($query). " \r\nconnection:close\r\n\r\n$query "; $h =fsockopen ($host, 80); Fwrite ($h, $post); For ($a =0, $r = ";! $a;) {$b =fread ($h, 8192); $r. = $b; $a = (($b = = ")? 1:0); } fclose ($h); return $r; }} $url = ' http://******/con/inter.php '; $data =request::p ost ($url, Array (' API ' = ' tag_list ')); $data 2=request:: Post2 ($url, Array (' API ' = ' tag_list '); Echo $data;
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP prevents websites from being attacked by prohibiting frequent IP access
How to count the number of people online through PHP
Php+mysql+jquery implementation of Query and list box selection