PHP 6 ways to send get, POST requests concise summary _php instances

Source: Internet
Author: User
This article mainly introduces the PHP send get, POST request 6 ways concise summary, respectively, use file_get_contents, fopen, Fsockopen, curl to send get and post requests, the need for friends can refer to

Method 1: Use file_get_contents to get the content in the Get mode:

<?php$url= ' http://www.jb51.net/'; $html = file_get_contents ($url); Echo $html; >

Method 2: Open the URL with fopen and get the content in get:

<?PHP$FP = fopen ($url, ' R '); Stream_get_meta_data ($FP); while (!feof ($fp)) {$result. = fgets ($fp, 1024);} echo "URL body: $result"; fclose ($FP);? >

Method 3: Use the file_get_contents function to get the URL by post

<?php$data = Array (' foo ' = ' bar '), $data = Http_build_query ($data); $opts = Array (' http ' = = Array (' method ' => ; ' POST ', ' header ' = ' content-type:application/x-www-form-urlencodedrn '. " Content-length: ". Strlen ($data). "RN", ' content ' = $data)); $context = Stream_context_create ($opts); $html = file_get_contents (' http://www.php.cn/', 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

<?phpfunction Get_url ($url, $cookie =false) {$url = Parse_url ($url); $query = $url [path]. "?". $url [query];echo "Query:". $query; $fp = Fsockopen ($url [host], $url [port]? $url [port]:80, $errno, $errstr,); if (! $fp) { return false;} else {$request = "GET $query http/1.1rn"; $request. = "Host: $url [Host]rn"; $request. = "Connection:closern"; if ($cookie) $r Equest.= "Cookie:  $cookien"; $request. = "RN"; fwrite ($fp, $request); while (! @feof ($fp)) {$result. = @fgets ($FP, 1024 );} Fclose ($FP); return $result;}} Gets the HTML portion of the URL, removing headerfunction geturlhtml ($url, $cookie =false) {$rowdata = Get_url ($url, $cookie), or if ($rowdata) {$ Body= stristr ($rowdata, "rnrn"), $body =substr ($body, 4,strlen ($body)); return $body;} return false;}? >

Method 5: Use the Fsockopen function to open the URL, post to get the full data, including header and body

<?phpfunction http_post ($URL, $data, $cookie, $referrer = "") {//parsing the given url$url_info=parse_url ($URL);// Building Referrerif ($referrer = = "")//if not given with this script as referrer$referrer= "111″;//making string from $dataf Oreach ($data as $key = + $value) $values []= "$key =". UrlEncode ($value); $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 PO St-request: $request. = "POST". $URL _info["path"]. " http/1.1n "; $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 "; $request. =" Cookie: $cookien "; $request. =" n "; $request. = $data _string." n "; $fp = Fsockopen ($URL _info[" host "], $URL _info[" Port "]), fputs ($fp, $request), while (!feof ($fp)) {$result. = fgets ($fp , 1024);} Fclose ($FP); return $result;}? >

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

<?php$ch = Curl_init (); $timeout = 5;curl_setopt ($ch, Curlopt_url, ' http://www.php.cn/'); curl_setopt ($ch, Curlopt_ Returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ CH); echo $file _contents;? >
  • 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.