PHP读取网页文件内容的实现代码(fopen,curl等)
来源:互联网 发布:saa7134hl采集卡软件 编辑:程序博客网 时间:2024/05/16 12:55
转自:http://www.jb51.net/article/27448.htm
php小偷程序中经常需要获取远程网页的内容,下面是一些实现代码,需要的朋友可以惨况下。
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.jb51.net");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //转化编码方式为UTF8
print $html;
$htm = file("http://s.jb51.net");
$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\r\n" .
"Cookie: foo=bar\r\n"
)
);
$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.jb51.net");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //转化编码方式为UTF8
print $html;
$htm = file("http://s.jb51.net");
$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\r\n" .
"Cookie: foo=bar\r\n"
)
);
$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);
?>
0 0
- PHP读取网页文件内容的实现代码(fopen,curl等)
- 抓网页内容file_get_contents curl fopen
- PHP fopen读取url内容
- PHP读取Excel文件的实现代码
- PHP读取文件内容后清空文件的代码
- php fopen,file_get_contents,curl的区别
- PHP中fopen,file_get_contents,curl的区别
- php读取word\pdf等文档的内容,并将其保存到网页中
- 读取网页文件内容
- PHP代码——Curl实现网页代理proxy
- node.js实现逐行读取文件内容的代码
- 读取文件内容存到数组的具体代码实现
- 库函数fopen/fgetc/fputc实现两个文件内容的复制
- c#实现对MP4等文件属性信息的读取以及对txt文件内容的读取
- windows nginx使用file_get_contents、fopen、curl访问php文件造成阻塞的解决办法
- PHP 通过curl库函数获取网页内容
- libcurl获取网页内容的PHP代码
- php curl抓取远程页面内容的代码分享
- git基础使用方法整理
- jquery一些记录(老是搞忘函数用法,规则)
- hive 的一次使用命令
- HTTP网络协议(二)
- 栈和数组
- PHP读取网页文件内容的实现代码(fopen,curl等)
- Java 8/27
- mysql各版本编译安装
- 汇编指令状态寄存器访问指令(MRS,MSR)
- C语言 文件单词的检索与计数
- intents 和 intent过滤器(一)
- 了解mysql的源码目录及源文件
- Android中自定义控件的步骤
- 如何处理北京自考报名ie浏览器无法弹出易宝支付网页