Using PHP to simulate post values is not a lot of use in daily life, but it is often used in some situations. The following script to the small people compiled 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);}