2012-07-23 90 views
0

我需要“计算”一个URL并重定向到它。我有以下代码:PHP:获取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) { 

     foreach($element->find('img') as $imagee) 
      $imgurl = $imagee->src; 
    } 

header("'Location:".$imgurl."'"); 

?> 

先在$ imgUrl的获得url后来重定向到URL ...但它不工作..

任何想法或建议?感谢

+0

你能保证图像的SRC属性将始终包含完整的URL吗?如果他们使用相对路径,您可能会遇到问题。另外,您对多个图像有什么期望?重定向到第一个? – ernie 2012-07-23 22:18:11

+0

要获得最后的图像,使用'$ imgurl = $ htmlCode-> find('div [class = frontPageImage] img') - > last_child() - > src;'。 – 2012-07-23 22:45:21

回答

3
header("'Location:".$imgurl."'"); 

大概应该是:

header("Location: " . $imgurl); 

我删除了单引号'因为你的HTTP标头看起来像:

'Location: http://site.com/image.jpg' 

这是因为单引号无效。

您也在循环并查找许多图片网址,但只能重定向到最后一个。

+0

谢谢drew010,问题是de引用!是的,我只想重定向最后一个。非常感谢! – user1364684 2012-07-23 22:28:20