1: Get content using file_get_contents in get Mode
CopyCode The Code is as follows: <? PHP
$ Url = 'HTTP: // www.baidu.com /';
$ Html = file_get_contents ($ URL );
// Print_r ($ http_response_header );
EC ($ html );
Printhr ();
Printarr ($ http_response_header );
Printhr ();
?>
Example code 2: Use fopen to open a URL and get the content Copy codeThe Code is as follows: <?
$ Fp = fopen ($ URL, 'R ');
Printarr (stream_get_meta_data ($ FP ));
Printhr ();
While (! Feof ($ FP )){
$ Result. = fgets ($ FP, 1024 );
}
Echo "url body: $ result ";
Printhr ();
Fclose ($ FP );
?>
Sample Code 3: Use the file_get_contents function to obtain the URL in post mode. Copy codeThe Code is as follows: <? PHP
$ DATA = array ('foo' => 'bar ');
$ DATA = http_build_query ($ data );
$ Opts = array (
'Http' => array (
'Method' => 'post ',
'Header' => "Content-Type: Application/X-WWW-form-urlencoded ".
"Content-Length:". strlen ($ data )."",
'Content' => $ data
),
);
$ Context = stream_context_create ($ opts );
$ Html = file_get_contents ('HTTP: // localhost/e/admin/test.html ', false, $ context );
Echo $ HTML;
?>
Example code 4: Use the fsockopen function to open a URL and get complete data, including header and body Copy code The Code is as follows: <?
Function get_url ($ URL, $ cookie = false ){
$ Url = parse_url ($ URL );
$ Query = $ URL [path]. "? ". $ URL [query];
EC ("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 ";
$ Request. = "Host: $ URL [host]";
$ Request. = "connection: Close ";
If ($ cookie) $ request. = "Cookie: $ cookie \ n ";
$ Request. = "";
Fwrite ($ FP, $ request );
While (! @ Feof ($ FP )){
$ Result. = @ fgets ($ FP, 1024 );
}
Fclose ($ FP );
Return $ result;
}
}
// Obtain the HTML part of the URL and remove the header
Function geturlhtml ($ URL, $ cookie = false ){
$ Rowdata = get_url ($ URL, $ cookie );
If ($ rowdata)
{
$ Body = stristr ($ rowdata ,"");
$ Body = substr ($ body, 4, strlen ($ body ));
Return $ body;
}
Return false;
}
?>
Example code 5: Use the fsockopen function to open the URL and obtain the complete data in post mode, including the header and body Copy code The Code is as follows: <?
Function http_post ($ URL, $ data, $ cookie, $ referrer = ""){
// Parsing the given URL
$ Url_info = parse_url ($ URL );
// Building referrer
If ($ referrer = "") // if not given use 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 is 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;
}
Printhr ();
?>
Example code 6: Use the curl library. before using the curl library, you may need to check PHP. ini to see if the curl extension has been enabled.
Copy codeThe Code is as follows: <?
$ CH = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, curlopt_url, 'HTTP: // www.baidu.com /');
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_connecttimeout, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ file_contents;
?>
About curl Library:
Http://curl.haxx.se/
Curl is a File Transfer tool using URL syntax. It supports FTP, ftps, HTTP htpps scp sftp tftp Telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based upload, proxy, Cookie, user + password proof, file transfer recovery, HTTP Proxy channel and a large number of other useful techniquesCopy codeThe Code is as follows: <?
Function printarr (array $ ARR)
{
Echo "<br> row field count:". Count ($ ARR). "<br> ";
Foreach ($ arr as $ key => $ value)
{
Echo "$ key = $ value <br> ";
}
}
?>
7.
Some host service providers disable PHP's allow_url_fopen option, that is, they cannot directly use file_get_contents to obtain the content of the remote web page. That is, you can use another function curl.
Below are two different expressions of the same function of file_get_contents and curl:
Example of using the file_get_contents function:Copy codeThe Code is as follows: <? PHP
$ File_contents = file_get_contents ('HTTP: // www.ccvita.com /');
Echo $ file_contents;
?>
example of using the curl function: copy Code the code is as follows: $ CH = curl_init ();
$ timeout = 5;
curl_setopt ($ ch, curlopt_url, 'HTTP: // www.ccvita.com ');
curl_setopt ($ ch, curlopt_returntransfer, 1);
curl_setopt ($ ch, curlopt_connecttimeout, $ timeout);
$ file_contents = curl_exec ($ ch );
curl_close ($ ch);
echo $ file_contents;
?>
Use the function_exists function to determine whether PHP supports a function. You can easily write the following function. Copy codeThe Code is as follows: <? PHP
Function vita_get_url_content ($ URL ){
If (function_exists ('file _ get_contents ')){
$ File_contents = file_get_contents ($ URL );
} Else {
$ CH = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, curlopt_url, $ URL );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_connecttimeout, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
}
Return $ file_contents;
}
?>
In fact, the above function remains to be discussed. If your host service provider closes file_get_contents and curl, the above function will encounter an error.