php教程|php手册
网页文件内容,fopen,curl,file_g
php教程-php手册
点击统计 源码,ubuntu内核调试环境,你们都用爬虫干嘛,php替换,大连seo整站lzw
php小偷程序中经常需要获取远程网页的内容,下面是一些实现代码,需要的朋友可以惨况下。
php 游戏网站源码,ubuntu中鼠标闪烁,电脑有两个tomcat吗,爬虫怎么找headers,php7.2跟7.3区别,宁波seo推荐lzw
c string类源码,vscode可以连接手机吗,ubuntu包目录,tomcat图片上传消失,爬虫猫眼网,php fopen w,青海关键词seo运营,加盟网站模板lzw
1.fopen实现代码:代码如下:
<?php
$handle = fopen (“/”, “rb”);
$contents = “”;
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
代码如下:
<?php
// 对 PHP 5 及更高版本
$handle = fopen(“/”, “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=””;
$contents=_url($pageURL);
?>
编码转换函数
代码如下:
$html = file_get_contents(“”);
$html = iconv( “Big5”, “UTF-8//IGNORE” , $html); //转化编码方式为UTF8
print $html;
$htm = file(“”);
$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
with additional headers shown above */
$fp = fopen(‘’, ‘r’, false, $context);
fpassthru($fp);
fclose($fp);
?>