Feb 6

用curl读远程地址 不指定

bkkkd , 18:49 , 开发应用 , 评论(0) , 引用(0) , 阅读(4298) , Via 本站原创 | |
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"));

发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]