PHP three ways to send post requests

Source: Internet
Author: User
Tags curl

  This article mainly introduces three kinds of methods that PHP sends POST request, use curl, file_get_content, fsocket to realize post submission data respectively, need friend can refer to under

  Code as follows: Class request{      public static function post ($url, $post _data = ', $timeout = 5) {//curl & nbsp         $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) &nbsp ;           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) {//file_get_content                      $postdata = http_build_query (              $data         &N Bsp );                      $opts = Array (' http ' =>     &NBSP ;                   Array (              &NB Sp             ' method '  => ' POST ',               &N Bsp             ' header '  => ' content-type:application/x-www-form-urlencoded ',                             ' content ' => $postdata &nbsp ;                      )             &NBsp                  $context = Stream_context_create ($opts);             $result = file_get_contents ($url, False, $context);           return $result;        }         public static function Post3 ($host, $path, $query, $others = ') {//fs Ocket             $post = "Post $path http/1.1rnhost: $hostrn";           $post. = "content-type:application/x-www-form-";           $post. = "Urlencodedrn${others}";           $post. = "User-agent:mozilla 4.0rncontent-length:";           $post. =strlen ($query). Rnconnection:closernrn$query ";           $h =fsockopen ($host, 80);           fwrite ($h, $post);           for ($a =0, $r = ';! $a;) {          &NBsp       $b =fread ($h, 8192);                   $r. = $b;                   $a = (($b = = ")? 1:0);              ,           fclose ($h);           return $r;      }}  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.