php 模拟用户抓取网页内容程序
<?php
function _link($url) {
$contents = @file_get_contents("$url");
if ($contents == "Forbidden" || $contents == "") {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, "$url");
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);
}
if (empty($contents)) {
exit('<font color=red>cant locaion.</font>');
}
preg_match_all("/charset=(.*?)>/is", $contents, $cod);
if (!empty($cod[1][0])) {
if (preg_match("/utf\-8/i", $cod[1][0])) {
$contents = iconv("UTF-8", "gbk//TRANSLIT", $contents);
}
}
return $contents;
}文章网址:http://www.phprm.com/code/2a623d9e94566736e1cfc27e52b2e4d5.html
随意转载^^但请附上教程地址。