PHP:获取url后重定向
我需要“计算”一个URL并重定向到它。我有以下代码:PHP:获取url后重定向,php,redirect,simple-html-dom,Php,Redirect,Simple Html Dom,我需要“计算”一个URL并重定向到它。我有以下代码: <?php //Basic HTML parsing with PHP include("simple_html_dom.php"); $htmlCode = file_get_html('http://example.net/example.html'); foreach($htmlCode->find('div[class=frontPageImage]') as $element) {
<?php
//Basic HTML parsing with PHP
include("simple_html_dom.php");
$htmlCode = file_get_html('http://example.net/example.html');
foreach($htmlCode->find('div[class=frontPageImage]') as $element) {
foreach($element->find('img') as $imagee)
$imgurl = $imagee->src;
}
header("'Location:".$imgurl."'");
?>
应该是:
header("Location: " . $imgurl);
我删除了单引号”
,因为您的HTTP头看起来像:
'Location: http://site.com/image.jpg'
这是无效的,因为单引号
您还可以循环查找许多图像URL,但只重定向到最后一个
应该是:
header("Location: " . $imgurl);
我删除了单引号”
,因为您的HTTP头看起来像:
'Location: http://site.com/image.jpg'
这是无效的,因为单引号
您还可以循环查找许多图像URL,但只重定向到最后一个 你能保证图片的SRC属性总是包含完整的URL吗?如果他们使用相对路径,您可能会遇到问题。另外,您对多张图片的期望是什么?重定向到第一个映像?要获取最后一个映像,请使用$imgurl=$htmlCode->find('div[class=frontPageImage]img')->last\u child()->src
。您能保证图像的SRC属性始终包含完整的URL吗?如果他们使用相对路径,您可能会遇到问题。另外,您对多张图片的期望是什么?重定向到第一个映像?要获取最后一个映像,请使用$imgurl=$htmlCode->find('div[class=frontPageImage]img')->last\u child()->src代码>。谢谢drew010,问题是报价!是的,我只想要最后一个。谢谢!谢谢drew010,问题是报价!是的,我只想要最后一个。谢谢!