PHP 6 Method code examples for sending get, POST requests

Source: Internet
Author: User

This article shows a code example of 6 ways that PHP sends a GET, POST request, using File_get_contents, fopen, Fsockopen, and curl to send get and post requests, with the following code: Method 1: Use file_get_contents to get the content in the Get mode:
1 <? PHP 2     $url= ' http://www.jb51.net/'; 3     $html file_get_contents ($url); 4     Echo $html ; 5 ?>
Method 2: Open the URL with fopen and get the content in get:
 1  <? php  2   $fp  =  ( $url , ' R '  3  stream_get_meta_data ($FP);  4  5   $result. = Fgets ($fp, 1024x768);  6   7  echo "url body: $result";  8  fclose ($FP);  9 ? 
Method 3: Use the file_get_contents function to get the URL by post
1<?PHP2     $data=Array(' foo ' = ' bar ');3     $data=Http_build_query($data);4  5     $opts=Array (6' HTTP '= = Array (7' Method ' = ' POST ',8' Header ' = ' content-type:application/x-www-form-urlencodedrn '. "Content-length:".strlen($data) . "RN",Ten' Content '= $data One         ) A     ); -   -     $context = stream_context_create ($opts); the$html = file_get_contents (' http://localhost/e/admin/test.html ',false,$context); -   -     Echo $html; -?>
Method 4: Open the URL with the Fsockopen function, get the complete data in Get mode, including header and Body,fsockopen need php.ini allow_url_fopen option to open
1<?PHP2     functionGet_url ($url,$cookie=false)3     {4$url=Parse_url($url);5         $query=$url[path]. "?".$url[query];6         Echo"Query:".$query;7         $fp=Fsockopen($url[Host],$url[Port]?$url[Port]:80,$errno,$errstr, 30);8         if(!$fp) {9             return false;Ten}Else { One             $request= "GET$queryHttp/1.1rn "; A             $request. = "Host:$url[Host]rn]; -             $request. = "Connection:Closern "; -             if($cookie)$request. = "Cookie:$cookien"; the $request.="RN"; -             fwrite($fp,$request); -              while([email protected]feof($fp)) { -                 $result.= @fgets($fp, 1024); +             } -             fclose($fp); +             return $result; A         } at     } -     //gets the HTML part of the URL, removing the header -     functionGeturlhtml ($url,$cookie=false) -     { -         $rowdata= Get_url ($url,$cookie); -         if($rowdata) in         { -             $body=Stristr($rowdata,"Rnrn"); to             $body=substr($body, 4,strlen($body)); +             return $body; -} the   *         return false; $     }Panax Notoginseng?>
Method 5: Use the Fsockopen function to open the URL, post to get the full data, including header and body
1<?PHP2     functionHttp_post ($URL,$data,$cookie,$referrer= "")3     {4  5         //parsing the given URL6         $URL _info=parse_url ($URL);7  8         //Building referrer9If$referrer= = "")//if not given use this script as referrerTen             $referrer= "111″; One   A         //making string from $data -         foreach($data  as $key=$value) -             $values[]=]$key= ".UrlEncode($value); the         $data _string=implode("&",$values); -   -         //Find out which port was needed–if not given use standard (=80) -if(!isset($URL _info["Port"])) +             $URL _info["Port"]=80; -   +         //Building Post-request: A         $request. = "POST".$URL _info["Path"]. " Http/1.1n "; at         $request. = "Host:".$URL _info["Host"]."n"; -         $request. = "Referer:$referern"; -         $request. = "content-type:application/x-www-form-Urlencodedn "; -         $request. = "Content-length:".strlen($data _string)."n"; -         $request. = "Connection:Closen "; -   in         $request. = "Cookie:$cookien"; -   to         $request.="n"; +         $request.=$data _string."n"; -   the         $fp=Fsockopen($URL _info["Host"],$URL _info["Port"]); *         fputs($fp,$request); $          while(!feof($fp)) {Panax Notoginseng              $result.=fgets($fp, 1024); -         } the         fclose($fp); +   A         return $result; the     } +?>
Method 6: Using the Curl Library, before using the Curl Library, you may need to see if php.ini has the curl extension turned on
1<?PHP2     $ch=curl_init ();3    $timeout= 5;4curl_setopt ($ch, Curlopt_url, ' http://www.jb51.net/');5curl_setopt ($ch, Curlopt_returntransfer, 1);6curl_setopt ($ch, Curlopt_connecttimeout,$timeout);7     $file _contents= Curl_exec ($ch);8Curl_close ($ch);9  Ten     Echo $file _contents; One?>

PHP 6 Method code examples for sending get, POST requests

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.