PHP读取网页文件内容的实现代码(fopen,curl等)
作者:bea
1.fopen实现代码: 代码如下: <?php $handle = fopen ("http://www.example.com/", "rb"); $contents = ""; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); ?> 代码如下: <?php // 对 PHP 5 及更高版本 $handle = fo
1.fopen实现代码:
代码如下:
<?php
$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
代码如下:
<?php
// 对 PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
2.curl实现代码:
代码如下:
<?php
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);
?>
编码转换函数
代码如下:
$html = file_get_contents("http://s.");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //转化编码方式为UTF8
print $html;
$htm = file("http://s.");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);
}
print_r($h);
另一种打开网页的方法
代码如下:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en
" .
"Cookie: foo=bar
"
)
);
$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);
?>
有用 | 无用
代码如下:
<?php
$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
代码如下:
<?php
// 对 PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
2.curl实现代码:
代码如下:
<?php
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);
?>
编码转换函数
代码如下:
$html = file_get_contents("http://s.");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //转化编码方式为UTF8
print $html;
$htm = file("http://s.");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);
}
print_r($h);
另一种打开网页的方法
代码如下:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en
" .
"Cookie: foo=bar
"
)
);
$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);
?>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php设计模式 FlyWeight (享元模式)
- php设计模式 Mediator (中介者模式)
- php设计模式 Prototype (原型模式)代码
- PHP如何解决网站大流量与高并发的问题
- session在PHP大型web应用中的使用
- php URL跳转代码 减少外链
- php session安全问题分析
- 使用PHP实现二分查找算法代码分享
- PHP求最大子序列和的算法实现
- php中转义mysql语句的实现代码
- 把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。
- php中对2个数组相加的函数
- php判断输入不超过mysql的varchar字段的长度范围
- PHP array操作10个小技巧分享
- php 缩略图实现函数代码
- php中随机显示图片的函数代码
- 使用php shell命令合并图片的代码
- php whois查询API制作方法
- PHP字符编码问题之GB2312 VS UTF-8解决方法