我正在尝试使用Simple Html Dom从某些网站获取链接,(file_get_content)
问题是其中一些链接使用重定向到实际帖子,我的脚本一直跟随它到帖子,但在我链接到该帖子的网站上,我不希望 php 回显文件"process.php?id=121",但我希望它返回真正的实际网址,如"domain.com/redirected-to-here.html"
脚本看起来像
$html = file_get_html('www.domain.com/post/this-is-a-post.html');
foreach($html->find('div#post a',0) as $linktopost){
echo $linktopost->href;
}
但这返回类似的东西
www.domain.com/redirect.php?id=10
所以问题实际上是,如何在重定向后使用简单的 html dom 解析器返回 url?
提前谢谢。
我会使用 cURL 并使用正则表达式解析位置标头。
$ch = curl_init('www.domain.com/post/this-is-a-post.html');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$header_and_html = curl_exec($ch);
preg_match(...);