PHP simulates the summary of post data submission methods, and phppost submits the summary _ PHP Tutorial

Source: Internet
Author: User
PHP simulates the summary of post data submission methods, and phppost submits the summary. PHP simulates post submission data method summary, phppost submission summary uses php to simulate post transfer. although not many values are used in daily life, they are often used in some occasions. The following PHP simulates the summary of post data submission methods, and phppost submits the summary.

Although php is used to simulate post-based value transfer in daily life, it is often used in some occasions. The following is a compilation of three php methods for simulating post value transfer: file_get_contents, curl, and socket.

First: 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);
 

Type 2: curl post simulation

 
  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: socket to simulate post

 
  ‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));var_dump($data);
 

The above three methods see the same content at the end, and both of them can get the post value. However, when using socket, you must pay attention to the complete header information when sending header information, for example, the content type and content length must be available, and the connection: close and post data must have a blank line. The content obtained through socket contains the header information, you have to deal with it to get the real content.

The following describes how to simulate a post request in php and call the interface.

/*** Simulate post url request * @ param string $ url * @ param string $ param */function request_post ($ url = '', $ param = '') {if (empty ($ url) | empty ($ param) {return false;} $ postUrl = $ url; $ curlPost = $ param; $ ch = curl_init (); // initialize curlcurl_setopt ($ ch, CURLOPT_URL, $ postUrl); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set headercurl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is required to be a string and output to the screen. curl_setopt ($ ch, CURLOPT_POST, 1); // The post submission method: curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost ); $ data = curl_exec ($ ch); // run curlcurl_close ($ ch); return $ data ;}

This is the method,

The following is a specific call case.

function testAction(){$url = 'http://mobile.jschina.com.cn/jschina/register.php';$post_data['appid'] = '10';$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';$post_data['member_name'] = 'zsjs123';$post_data['password'] = '123456';$post_data['email'] = 'zsjs123@126.com';$o = "";foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ;}$post_data = substr($o,0,-1);$res = $this->request_post($url, $post_data); print_r($res);} 

In this way, the request is submitted and the request results are obtained. Generally, the returned results are in json format.

The post is spliced.

It can also be transformed into the following method.

/*** Simulate post url request * @ param string $ url * @ param array $ post_data */function request_post ($ url = '', $ post_data = array ()) {if (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); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set headercurl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is required to be a string and output to the screen. curl_setopt ($ ch, CURLOPT_POST, 1); // The post submission method: curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost ); $ data = curl_exec ($ ch); // run curlcurl_close ($ ch); return $ data ;}

The splicing is encapsulated so that the call is more concise.

function testAction(){$url = 'http://mobile.jschina.com.cn/jschina/register.php';$post_data['appid'] = '10';$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:
  • Use the Curl, socket, and file_get_contents methods in php to submit data through POST.
  • Php retrieves xml data submitted through http post and parses xml
  • Php uses socket to simulate the sample code for submitting data through post or get in http
  • Example of php curl simulating post data submission
  • A simple example of PHP preventing repeated data submission by post
  • Summary of common methods for submitting post and post using ajax data in php
  • Php method for simulating post data submission

Ghost uses php to simulate post value transfer. although not many values are used in daily life, they are often used in some occasions. Below...

Related Article

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.