PHP Analog Post Submission data method Rollup, phppost submission Rollup _php tutorial

Source: Internet
Author: User

PHP Analog Post Submission Data method Summary, Phppost submission Summary


Using PHP to simulate post values is not a lot of use in daily life, but it is often used in some situations. Under the help of the small people compiled for three kinds of PHP analog post transfer value method, file_get_contents, curl and socket.

The first type: file_get_contents to simulate post

 
  
   
   Array (' method ' = ' POST ', ' content ' = Http_build_query ($post),), $result = File_get_contents ($url, False, Stream_context_create ($options)); return $result;} $data = File_get_contents_post ("http://www.a.com/post/post.php", Array (' name ' = ' caiknife ', ' email ' = ') Caiknife#gmail.com '); Var_dump ($data);
 
  

Second type: Curl analog post

 
  
   
  True,curlopt_header =>false,curlopt_post =>true,curlopt_postfields = $post,); $ch = Curl_init ($url); curl_ Setopt_array ($ch, $options); $result = Curl_exec ($ch); Curl_close ($ch); return $result;} $data = Curl_post ("http://www.a.com/post/post.php", Array (' name ' = ' caiknife ', ' email ' = ' caiknife#gmail.com ') ); Var_dump ($data);
 
  

Third type: socket to simulate post

 
  POST {$urls [' path ']} http/1.1
  host:{$urls [' Host ']}
  content-type:application/x-www-form-urlencoded
  content-length:{$length}
  Connection:close
  {$post}
  Header;fwrite ($FP, $header), $result = ", while (!feof ($fp)) {$result. = fread ($FP, 512);} $result = Explode ("\r\n\r\n", $result, 2); return $result [1];} $data = Socket_post ("http://www.a.com/post/post.php", Array (' name ' = ' caiknife ', ' email ' = ' caiknife#gmail.com '); Var_dump ($data);
 

The above three methods to see the last content is the same, you can get the value of post, but when the socket is used, the header information must be sent to pay attention to the full information of the header, such as content type and content length must have, There is a blank line between the Connection:close and the post data, and so on, and the content obtained through the socket contains the header information, to be processed to get the real content.

Let's say the PHP analog post submission request, call the interface

This is the method,

The following is a specific invocation case.

This submits the request and gets the result of the request. The result of the general return is in JSON format.

The post here is stitched up.

can also be transformed into the following way.

/*** analog post for URL request * @param string $url * @param array $post _data*/function request_post ($url = ", $post _data = Array ()) {I F (Empty ($url) | | empty ($post _data)) {return false;} $o = ""; foreach ($post _data as $k = = $v) {$o. = "$k =". UrlEncode ($v). "&";} $post _data = substr ($o, 0,-1); $postUrl = $url; $curlPost = $post _data; $ch = Curl_init ();//Initialize Curlcurl_setopt ($ch, curlopt _url, $POSTURL);//crawl specified Web page curl_setopt ($ch, Curlopt_header, 0);//Set Headercurl_setopt ($ch, Curlopt_returntransfer, 1); /Required result is a string and output to the screen curl_setopt ($ch, Curlopt_post, 1);//post submission curl_setopt ($ch, Curlopt_postfields, $curlPost); $data = Curl_exec ($ch);//Run Curlcurl_close ($ch); return $data;}

Stitching is also encapsulated, so the call is more concise.

function Testaction () {$url = ' http://mobile.jschina.com.cn/jschina/register.php '; $post _data[' appid '] = ' ten '; $post _ data[' appkey ' = ' cmbohpffxvr03nipkkqxaaa1vf5no4nq '; $post _data[' member_name '] = ' zsjs124 '; $post _data[' password '] = ' 123456 '; $post _data[' email '] = ' zsjs124@126.com ';//$post _data = Array (); $res = $this->request_post ($url, $post _data ); Print_r ($res);}

Articles you may be interested in:

    • Three ways to post data in PHP using curl, socket, file_get_contents
    • PHP gets the XML data through the HTTP protocol post and parses the XML.
    • Example code in PHP that simulates post or get-submitted data in HTTP with a socket
    • PHP Curl analog post submission Data sample
    • Simple example of PHP preventing post repeating data submission
    • PHP uses AJAX data to submit post and post common methods summary
    • How PHP simulates post-commit data

http://www.bkjia.com/PHPjc/1100919.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100919.html techarticle PHP Analog Post submission Data method Summary, Phppost submission Rollup using PHP analog post values Although not many in daily life, but in some cases are often used. Below ...

  • 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.