【发布时间】:2014-03-31 15:53:39
【问题描述】:
我正在寻找 URL 重定向到的位置,目前我正在使用 file_get_contents() 获取网页信息,但有些页面返回 301/302 标头状态,我想使用函数来查找 url页面重定向到;
function page_info($url) {
$fp = file_get_contents($url);
$title = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
$h1 = preg_match("/<h1>(.*)<\/h1>/siU", $fp, $h1_matches);
$h2 = preg_match("/<h2>(.*)<\/h2>/siU", $fp, $h2_matches);
$meta_desc = get_meta_tags($url);
$data = array("title"=>trim(preg_replace('/\s+/', ' ', $title_matches[1])), "metadesc" => $meta_desc['description'],"h1"=>trim(preg_replace('/\s+/', ' ', $h1_matches[1])), "h2"=>trim(preg_replace('/\s+/', ' ', $h2_matches[1])));
return $data;
}
有什么方法可以找出重定向的 url,以便我可以在正确的 URL 上运行 page_info() 函数?
【问题讨论】:
-
file_get_contents(),AFAIK 是不可能的。请改用 cURL。 -
用正则表达式解析 HTML? Yikes。也许有一个better way?
标签: php redirect curl file-get-contents