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 of the small compiled to everyone compiled three kinds of PHP analog post transfer value method, file_get_contents, curl and socket, need to refer to the friend
The first type: file_get_contents to simulate post
<PHPfunctionFile_get_contents_post ($url,$post){$options=Array(' http '=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
<PHPfunctionCurl_post ($url,$post){$options=Array(Curlopt_returntransfer=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
<PHPfunctionSocket_post ($url,$post){$urls=Parse_url($url);if(!isset($urls[' Port ']) {$urls[' Port ']=80;}$fp=Fsockopen($urls[' Host '],$urls[' Port '],$errno,$errstr);if(!$fp){Echo"$errno,$errstr";Exit();}$post=Http_build_query($post);$length=strlen($post);$header=<<<HEADER<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>post {$urls[' Path ']} Http/1.1<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>host:{$urls[' Host ']}<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>content-type:application/x-www-form-urlencoded<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>content-length:{$length}<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>connection:Close<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>{$post}<spanclass= "Apple-tab-span" style= "White-space:pre" ></span>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
/** * URL Request for analog post * @param string $url * @param string $param*/functionRequest_post ($url= ",$param= ' ') {if(Empty($url) ||Empty($param)) {return false;}$POSTURL=$url;$curlPost=$param;$ch= Curl_init ();//Initialize Curlcurl_setopt ($ch, Curlopt_url,$POSTURL);//crawl specified Web pagecurl_setopt ($ch, Curlopt_header, 0);//Set Headercurl_setopt ($ch, Curlopt_returntransfer, 1);//request result As string and output to screencurl_setopt ($ch, Curlopt_post, 1);//Post Submission Methodcurl_setopt ($ch, Curlopt_postfields,$curlPost);$data= Curl_exec ($ch);//Run CurlCurl_close ($ch);return $data;}
This is the method,
The following is a specific invocation case.
functiontestaction () {$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 '] = ' [email protected] ';$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);}
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.
/** * URL Request for analog post * @param string $url * @param array $post _data*/functionRequest_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);//crawl specified Web pagecurl_setopt ($ch, Curlopt_header, 0);//Set Headercurl_setopt ($ch, Curlopt_returntransfer, 1);//request result As string and output to screencurl_setopt ($ch, Curlopt_post, 1);//Post Submission Methodcurl_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.
functiontestaction () {$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 '] = ' [email protected] ';//$post _data = Array ();$res=$this->request_post ($url,$post _data); Print_r($res);}
PHP Analog Post Submission Data method summary