Method 1: Use file_get_contents to get the content in the Get mode
[PHP]View Plaincopyprint?
- <?php
- $url =' http://www.domain.com/';
- $html = file_get_contents ($url);
- echo $html;
- ?>
<?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?
- <?php
- $fp = fopen ($url, ' R ');
- 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?
- Stream_get_meta_data ($fp);
Stream_get_meta_data ($FP);
[PHP]View Plaincopyprint?
- while (! Feof ($fp)) {
- $result. = fgets ($fp, 1024);
- }
- echo "URL body: $result";
- Fclose ($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]View Plaincopyprint?
- <?php
- $data = Array (' foo ' = ' bar ');
<?php$data = Array (' foo ' = ' bar ');
[PHP]View Plaincopyprint?
- Generate Url-encode Request string, convert array to string
- $data = Http_build_query ($data);
- $opts = Array (
- <span style="WHITE-SPACE:PRE;" > </span>' http ' = = Array (
- <span style="WHITE-SPACE:PRE;" > </span>' method ' = ' POST ',
- <span style="WHITE-SPACE:PRE;" > </span>' header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
- <span style="WHITE-SPACE:PRE;" > </span>"content-length:". strlen ($data). "\ r \ n",
- <span style="WHITE-SPACE:PRE;" > </span>' content ' = $data
- <span style="WHITE-SPACE:PRE;" > </span>)
- );
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?
- Generate a handle file for the request
- $context = Stream_context_create ($opts);
- $html = file_get_contents (' http://localhost/e/admin/test.html ', false, $context);
- echo $html;
- ?>
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?
- <?php
- function 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, 30);
- 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 ($cookie) $request. ="Cookie: $cookie \ n";
- $request. ="\ r \ n";
- Fwrite ($fp,$request);
- while ()) {
- $result. = @fgets ($fp, 1024);
- }
- Fclose ($fp);
- return $result;
- }
- }
- Gets the HTML part of the URL, removing the header
- function geturlhtml ($url,$cookie =false)
- {
- $rowdata = Get_url ($url,$cookie);
- if ($rowdata)
- {
- $body = stristr ($rowdata,"\r\n\r\n");
- $body =substr ($body, 4,strlen ($body));
- return $body;
- }
- return false;
- }
- ?>
<?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?
- <?php
- function Http_post ($URL,$data,$cookie, $referrer ="")
- {
- //parsing the given URL
- $URL _info=parse_url ($URL);
- //Building referrer
- if ($referrer = ="") //If not given with this script as referrer
- $referrer ="111";
- //making string from $data
- foreach ($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;
- }
- ?>
<?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?
- <?PHP&NBSP;&NBSP;
- $ch = curl_init ();
- $timeout = 5;
- curl_setopt ( $ch, curlopt_url, ' http://www.domain.com/' );
- curl_setopt ( $ch, curlopt_ returntransfer, 1);
- curl_setopt ( $ch, curlopt_ Connecttimeout, $timeout)
- $file _contents = curl_exec ( $ch);
- curl_close ( $ch);
- echo $file _contents;
- ? >
Several ways PHP sends a GET, POST request