0

我有一个涉及 DOM 的奇怪错误。我正在尝试遍历文档中的每个 href 并在必要时将其替换为绝对路径。问题是,在我使用后$dom->setttribute()getAttribute返回更改后的值。然而,如果我saveHTML()再次使用 getElementsByTagName 和 getAttribute 查询标签,这些值就会从http://example.com/path.php?ccc截断为http://example.com

这是我的代码:

<?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();
?>
4

2 回答 2

0

试试这些 curl 选项 + 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();
?>
于 2012-06-21T09:05:32.540 回答
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
于 2012-06-20T19:55:43.050 回答