Three common uses of PHP analog post requests

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.