Feb
6
function getHeader($content)
{
$array = preg_split("/(\r\n?)/", $content, 2);
if (!isset($array[1])) $array[1] = null;
return $array;
}
//$url 要读取的地址,$allowExts 如果有跳转,允许跳转后的扩展名
function read($url, $allowExts, $header = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP WORLD Spider - WWW.PHPWORLD.CN");
if ($header == true)
{
$m = getHeader(curl_exec($ch));
}
else
{
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
//截取内容
$header = $m[0];
$body = $m[1];
if (preg_match("/\nlocation:(.+)/", $header, $ar))
{
$url = trim($ar[1]);
$infos = @pathinfo(basename($url));
if (isset($infos["extension"]) && in_array(strtolower($infos["extension"]), $allowExts))
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, $header);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
}
return $body;
}
echo read("http://www.test.com/a.php", array("php"));
{
$array = preg_split("/(\r\n?)/", $content, 2);
if (!isset($array[1])) $array[1] = null;
return $array;
}
//$url 要读取的地址,$allowExts 如果有跳转,允许跳转后的扩展名
function read($url, $allowExts, $header = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP WORLD Spider - WWW.PHPWORLD.CN");
if ($header == true)
{
$m = getHeader(curl_exec($ch));
}
else
{
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
//截取内容
$header = $m[0];
$body = $m[1];
if (preg_match("/\nlocation:(.+)/", $header, $ar))
{
$url = trim($ar[1]);
$infos = @pathinfo(basename($url));
if (isset($infos["extension"]) && in_array(strtolower($infos["extension"]), $allowExts))
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, $header);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
}
return $body;
}
echo read("http://www.test.com/a.php", array("php"));