2012-07-23 87 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