1: Get the content with file_get_contents
Copy Code code 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: Open the URL with fopen and get the content
Copy Code code 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);
?>
example code 3: Using the File_get_contents function to get the URL by post
Copy Code code 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: Open the URL with the Fsockopen function to get the complete data, including header and body
Copy Code code 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;
}
//Get the HTML portion of the URL, remove 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: Open the URL with the Fsockopen function to get the complete data in post, including header and body
Copy Code code 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 the port is needed-if to 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: Before using the Curl library, you might want to check the php.ini to see if the curl extension has been turned on before using the Curl Library
Copy Code code 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 the Curl Library:
Curl Official website http://curl.haxx.se/
Curl is a transfer file tool using URL syntax that supports FTP, FTPS, HTTP htpps SCP SFTP TFTP TELNET DICT file and LDAP. Curl supports SSL certificates, HTTP POST, HTTP put, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password certificates, file transfer recovery, HTTP proxy channels, and a number of other useful tips
Copy Code code 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 put PHP allow_url_fopen option is off, just can't use file_get_contents directly to get the content of remote Web pages. That is, you can use another function curl.
Here are the different ways to file_get_contents and curl two functions
Example of using the File_get_contents function:
Copy Code code as follows:
< PHP
$file _contents = file_get_contents (' http://www.ccvita.com/');
echo $file _contents;
?>
To use an example of a curl function:
Copy Code code as follows:
< PHP
$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;
?>
using the Function_exists function to determine whether PHP supports a function can easily write the following function
Copy Code code 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, this function is still open to question, if your host service provider to the file_get_contents and curl are closed, the above function will appear errors.