PHP Analog Post submission data

Source: Internet
Author: User
Tags explode bbpress

PHP analog post submission data, a lot of useful,

<?PHP//PHP Post data three ways//PHP has three ways to post data, respectively, curl, socket, file_get_contents:/** * Socket version * method of use: * $post _string = "App=socket&version=beta"; * Request_by_socket (' facebook.cn ', '/ Restserver.php ', $post _string); */functionRequest_by_socket ($remote _server,$remote _path,$post _string,$port= 80,$timeout= 30){    $socket=Fsockopen($remote _server,$port,$errno,$errstr,$timeout); if(!$socket) die("$errstr($errno)"); fwrite($socket, "POST$remote _pathHttp/1.0\r\n "); fwrite($socket, "User-agent:socket example\r\n"); fwrite($socket, "HOST:$remote _server\ r \ n "); fwrite($socket, "content-type:application/x-www-form-urlencoded\r\n"); fwrite($socket, "Content-length:". (strlen($post _string) + 8). ' \ r \ n '); fwrite($socket, "accept:*/*\r\n"); fwrite($socket, "\ r \ n"); fwrite($socket, "mypost=$post _string\ r \ n "); fwrite($socket, "\ r \ n"); $header= "";  while($str=Trim(fgets($socket, 4096))) {        $header.=$str; }     $data= "";  while(!feof($socket)) {        $data.=fgets($socket, 4096); }     return $data;} /** * Curl version * How to use: * $post _string = "App=request&version=beta"; * Request_by_curl (' Http://facebook.cn/restServer . php ', $post _string); */functionRequest_by_curl ($remote _server,$post _string){    $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$remote _server); curl_setopt ($ch, Curlopt_postfields,$post _string); curl_setopt ($ch, Curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) "); $data= Curl_exec ($ch); Curl_close ($ch); return $data;} /** Other versions * How to use: * $post _string = "App=request&version=beta"; * Request_by_other (' http://facebook.cn/ Restserver.php ', $post _string); */functionRequest_by_other ($remote _server,$post _string){    $context=Array(        ' http ' =Array(            ' Method ' = ' POST ', ' header ' = ' content-type:application/x-www-form-urlencoded '. ' \ r \ n '. ' user-agent:mozilla/4.0 (compatible; MSIE 5.01;                        Windows NT 5.0) '. ' \ r \ n '. ' Content-length: '.strlen($post _string+ 8, ' content ' = ' mypost= '.$post _string)        ); $stream _context=stream_context_create($context); $data=file_get_contents($remote _server,false,$stream _context); return $data;} ?>

Alternatively, you can directly simulate the post form submission without receiving data.

<?PHP$query _url=Array(' username ' = ' username ', ' password ' = ' password ');Echo' <form action= ' http://lin.test.com "method=" post "name=" Buttonform "id=" Buttonform ">";foreach($query _url  as $key=$val){Echo' <input type = ' hidden ' name= '.$key. ' "value =" '.$val.‘" > ';}Echo' </form> ';Echo' <script>document. getElementById ("Buttonform"). Submit ();</script> ';

<?PHP//Log in as a program login as an examplefunctionBbslogin ($user _login,$password,$host,$port= "80") {    //post data that needs to be submitted    $argv=Array(' Cookies ' =Array(' user_login ' =$user _login, ' password ' =$password, ' _wp_http_referer ' = '/bbpress/', ' re ' = ', ' Remember 'true)); foreach($argv[' Cookie '] as $key=$value) {        $params[] =$key. ' = '.$value; }    $params=implode(' & ',$params); $header= "Post/bbpress/bb-login.php http/1.1\r\n"; $header. = "Host:$host:$port\ r \ n "; $header. = "content-type:application/x-www-form-urlencoded\r\n"; $header. = "Content-length:".strlen($params) . "\ r \ n"; $header. = "connection:close\r\n\r\n"; $header.=$params; $fp=Fsockopen($host,$port); fputs($fp,$header);  while(!feof($fp)) {        $str=fgets($fp); //here is your own logic code, here is the main analog cookie, can be used to synchronize login        if(! (Strpos($str, "Set-cookie:") = = =false)) {            $tmparray=Explode(" ",$str); $cookiearray=Explode("=",$tmparray[1]); $cookiepaths=Explode("=",$tmparray[6]); $cookiename=UrlDecode($cookiearray[0]); $cookievalue=UrlDecode(substr($cookiearray[1], 0,strlen($cookiearray[1])-1)); $cookietime= Time() + 3600 * 24 * 7; $cookiepath=UrlDecode(substr($cookiepaths[1], 0,strlen($cookiepaths[1])-1)); Setcookie($cookiename,$cookievalue,$cookietime,$cookiepath); }    }    fclose($fp);}?>

PHP Analog Post submission data

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.