PHP implementation code for reading web page file content (fopen, curl, etc.)_PHP tutorial

WBOY
Release: 2016-07-21 15:27:55
Original
909 people have browsed it

1.fopen implementation code:

Copy code The code is as follows:

$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$ contents .= fread($handle, 8192);
}
fclose($handle);
?>

Copy code The code is as follows:

// For PHP 5 and above
$handle = fopen("http://www.example .com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

2 .curl implementation code:
Copy code The code is as follows:

function _url( $Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$pageURL="http://www.baidu.com";
$contents= _url($pageURL);
?>

Encoding conversion function
Copy code The code is as follows:

$html = file_get_contents("http://s.jb51.net");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); / /Convert encoding to UTF8
print $html;
$htm = file("http://s.jb51.net");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);
}
print_r($h);

Another way to open a web page
Copy the code The code is as follows:

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language : enrn" .
"Cookie: foo=barrn"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.baidu.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323665.htmlTechArticle1.fopen implementation code: Copy the code as follows: ?php $handle = fopen ("http://www .example.com/", "rb"); $contents = ""; while (!feof($handle)) { $contents .= fread($handle, 8192);...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!