2012-06-20 1 views
0

У меня есть странная ошибка, связанная с DOM. Я пытаюсь перебрать все href внутри документа и при необходимости заменить его абсолютным путем. Проблема в том, что после использования $dom->setttribute(), getAttribute возвращает измененное значение. Тем не менее, если I saveHTML() или теги запроса снова используют getElementsByTagName и getAttribute, значения усекаются от http://example.com/path.php?ccc до http://example.com.Изменение атрибута с DOM в PHP не сохраняется правильно

Вот мой код:

<?php 
//include 'url_to_absolute.php'; 


function url_to_absolute($url, $href) { 
    return trim($url . $href); 
} 

$url = 'http://example.com'; 
//$url = $_GET["url"]; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$contents = curl_exec($ch); 
@curl_close(); 

$dom = new DOMDocument(); 
$dom->loadHTML($contents); 


//change the urls to absolute 
$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor) 
{ 
    $href = $anchor->getAttribute('href'); 
    $abs = url_to_absolute($url, $href); 
    $anchor->removeAttribute('href'); 
    $anchor->setAttribute('href', $abs); 

    //changed 
    $newhref = $anchor->getAttribute('href'); 
    echo "newhref = " . $newhref; //shows http://example.com/.... (good) 
} 


$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor) 
{ 
    echo "new2 = " . $anchor->getAttribute('href'); //returns http://example.com only 
} 

//print output 
echo @$dom->saveHTML(); 
?> 
+1

Не могли бы вы опубликовать функцию вашего url_to_absolute() пожалуйста ? И в вашем фрагменте переменная $ url не существует. – pp19dd

+0

Моя функция url_to_absolute Я получил отсюда: http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url#Code –

+0

my $ url переменная была установлена ​​ранее как $ url = $ _GET ['url'] –

ответ

0

Попробуйте эти локон варианты + curl_init ($ URL):

<?php 
//include 'url_to_absolute.php'; 
function url_to_absolute($url, $href){ 
    return trim($url . $href); 
} 

$url = 'http://example.com'; 
//$url = $_GET["url"]; 
$ch = curl_init($url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE); 
$contents = curl_exec($ch); 
curl_close(); 

$dom = new DOMDocument(); 
$dom->loadHTML($contents); 
//$dom->saveHTMLFile('dom_doc_test.html'); 


//change the urls to absolute 
$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor) 
{ 
    $href = $anchor->getAttribute('href'); 
    $abs = url_to_absolute($url, $href); 
    $anchor->removeAttribute('href'); 
    $anchor->setAttribute('href', $abs); 

    //changed 
    $newhref = $anchor->getAttribute('href') . '<br />'; 
    echo "newhref = " . $newhref; //shows http://example.com/.... (good) 
} 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor) 
{ 
    echo "new2 = " . $anchor->getAttribute('href') . '<br />'; //returns http://example.com only 
} 

//print output 
echo @$dom->saveHTML(); 
?> 
0

Это должно быть ошибка в функции url_to_absolute. Мой простой url_to_absolute является:

function url_to_absolute($url, $href){ 
    return trim($url . $href); 
} 

$url = 'http://example.com'; 

$dom = new DOMDocument(); 
$dom->loadHTML('<html><body><a href="/path.html?q=hello&a=bye"></a><a href="/path2.html?before=34&after=44"></a></body></html>'); 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor){ 
    $href = $anchor->getAttribute('href'); 
    echo "href = " . $href . '<br />'; 
} 

echo '<br />'; 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor){ 
    $href = $anchor->getAttribute('href'); 
    $abs = url_to_absolute($url, $href); 
    $anchor->removeAttribute('href'); 
    $anchor->setAttribute('href', $abs); 

    $newhref = $anchor->getAttribute('href'); 
    echo "newhref = " . $newhref . '<br />'; 
} 

echo '<br />'; 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor){ 
    echo "new2 = " . $anchor->getAttribute('href') . '<br />'; 
} 

и результат:

href = /path.html?q=hello&a=bye 
href = /path2.html?before=34&after=44 

newhref = http://example.com/path.html?q=hello&a=bye 
newhref = http://example.com/path2.html?before=34&after=44 

new2 = http://example.com/path.html?q=hello&a=bye 
new2 = http://example.com/path2.html?before=34&after=44 
+0

Я заменил свой код с вашей простой функцией url_to_absolute. Я все еще получаю ту же проблему. Я отредактировал свой фрагмент, чтобы показать, как выглядит мой код –