Several ways that PHP sends a GET, POST request

Source: Internet
Author: User
Tags array to string urlencode vars

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

[PHP]View Plaincopyprint?
    1. <?php
    2. $url =' http://www.domain.com/';
    3. $html = file_get_contents ($url);
    4. echo $html;
    5. ?>
<?php$url= ' http://www.domain.com/'; $html = file_get_contents ($url); Echo $html; >

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

[PHP]View Plaincopyprint?
    1. <?php
    2. $fp = fopen ($url, ' R ');
    3. Return request flow information (array: Request status, block, return value is empty, return value HTTP first)
<?PHP$FP = fopen ($url, ' r ');//Return request flow information (array: request state, block, return value is empty, return value HTTP first)
[PHP]View Plaincopyprint?
    1. Stream_get_meta_data ($fp);
Stream_get_meta_data ($FP);
[PHP]View Plaincopyprint?
    1. while (! Feof ($fp)) {
    2. $result. = fgets ($fp, 1024);
    3. }
    4. echo "URL body: $result";
    5. Fclose ($fp);
    6. ?>
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]View Plaincopyprint?
    1. <?php
    2. $data = Array (' foo ' = ' bar ');
<?php$data = Array (' foo ' = ' bar ');
[PHP]View Plaincopyprint?
  1. Generate Url-encode Request string, convert array to string
  2. $data = Http_build_query ($data);
  3. $opts = Array (
  4. <span style="WHITE-SPACE:PRE;" > </span>' http ' = = Array (
  5. <span style="WHITE-SPACE:PRE;" > </span>' method ' = ' POST ',
  6. <span style="WHITE-SPACE:PRE;"  > </span>' header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
  7. <span style="WHITE-SPACE:PRE;" > </span>"content-length:". strlen ($data). "\ r \ n",
  8. <span style="WHITE-SPACE:PRE;" > </span>' content ' = $data
  9. <span style="WHITE-SPACE:PRE;" > </span>)
  10. );
After generating the Url-encode request string, convert the array to a string $data = Http_build_query ($data); $opts = Array (the ' http ' = = Array ('method ' = > ' POST ',' header ' = ' content-type:application/x-www-form-urlencoded\r\n '." Content-length: ". Strlen ($data). "\ r \ n",' content ' = $data));     
[PHP]View Plaincopyprint?
    1. Generate a handle file for the request
    2. $context = Stream_context_create ($opts);
    3. $html = file_get_contents (' http://localhost/e/admin/test.html ', false, $context);
    4. echo $html;
    5. ?>
The handle file for the build request $context = Stream_context_create ($opts); $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

[PHP]View Plaincopyprint?
  1. <?php
  2. function Get_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;
  10. } Else {
  11. $request = "GET $query http/1.1\r\n";
  12. $request. = "Host: $url [host]\r\n";
  13. $request. = "connection:close\r\n";
  14. if ($cookie) $request. ="Cookie: $cookie \ n";
  15. $request. ="\ r \ n";
  16. Fwrite ($fp,$request);
  17. while ()) {
  18. $result. = @fgets ($fp, 1024);
  19. }
  20. Fclose ($fp);
  21. return $result;
  22. }
  23. }
  24. Gets the HTML part of the URL, removing the header
  25. function geturlhtml ($url,$cookie =false)
  26. {
  27. $rowdata = Get_url ($url,$cookie);
  28. if ($rowdata)
  29. {
  30. $body = stristr ($rowdata,"\r\n\r\n");
  31. $body =substr ($body, 4,strlen ($body));
  32. return $body;
  33. }
  34. return false;
  35. }
  36. ?>
<?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.1\r\n"; $request. = "Host: $url [host]\r\n"; $request. = "connection:close\r\n"; if ($coo Kie) $request. = "Cookie:   $cookie \ n"; $request. = "\ r \ n"; fwrite ($fp, $request); while ()) {$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, "\r\n\r\n"), $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

[PHP]View Plaincopyprint?
  1. <?php
  2. function Http_post ($URL,$data,$cookie, $referrer ="")
  3. {
  4. //parsing the given URL
  5. $URL _info=parse_url ($URL);
  6. //Building referrer
  7. if ($referrer = ="") //If not given with this script as referrer
  8. $referrer ="111";
  9. //making string from $data
  10. foreach ($data as $key =$value)
  11. $values []="$key =". UrlEncode ($value);
  12. $data _string=implode ("&",$values);
  13. //Find out which port was needed-if not given use standard (=80)
  14. if (!isset ($URL _info["Port"))
  15. $URL _info["Port"]=80;
  16. //Building post-request:
  17. $request. ="POST".  $URL _info["path"].  Http/1.1\n ";
  18. $request. ="Host:". $URL _info["host"]."  \ n ";
  19. $request. ="Referer: $referer \ n";
  20. $request. ="content-type:application/x-www-form-urlencoded\n";
  21. $request. ="Content-length:". strlen ($data _string)."  \ n ";
  22. $request. ="connection:close\n";
  23. $request. ="Cookie: $cookie \ n";
  24. $request. ="\ n";
  25. $request. =$data _string."  \ n ";
  26. $fp = fsockopen ($URL _info["host"],$URL _info["Port"]);
  27. Fputs ($fp, $request);
  28. while (! Feof ($fp)) {
  29. $result. = fgets ($fp, 1024);
  30. }
  31. Fclose ($fp);
  32. return $result;
  33. }
  34. ?>
<?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 $dataforeach ($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 post-request: $request. = "POST". $URL _info["path"]. " Http/1.1\n "; $request. =" Host: ". $URL _info[" host "]." \ n "; $request. =" Referer: $referer \ n "; $request. =" content-type:application/x-www-form-urlencoded\n "; $request. =" Content-length: ". strlen ($data _string)."    \ n "; $request. =" connection:close\n ";    $request. = "Cookie: $cookie \ n"; $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]View Plaincopyprint?
    1. <?PHP&NBSP;&NBSP;
    2. $ch  = curl_init ();   
    3. $timeout  = 5;  
    4. curl_setopt  ( $ch,  curlopt_url,  ' http://www.domain.com/' );   
    5. curl_setopt  ( $ch,  curlopt_ returntransfer, 1);   
    6. curl_setopt  ( $ch,  curlopt_ Connecttimeout,  $timeout)   
    7. $file _contents = curl_exec ( $ch);   
    8. curl_close ( $ch);   
    9. echo  $file _contents;  
    10. ? >  

Several ways PHP sends a GET, POST request

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.