Obtener parte de una página web con PHP

I need a way to get only an element with a certain ID and display it with PHP. I am a PHP noob, so this has been very difficult so far. All the other questions similar to this were a bit too complicated, so I was wondering if someone could explain it to me.

To be more specific on what I want, I am making a spell search for a minecraft server. Our website is http://pvpzone.org/ and wiki is at http://pvpzone.wikispaces.com/. Each spell has a page on the wiki, like the one for "Vanish" is pvpzone.wikispaces.com/Vanish. The idea of the spell search would be an easier way to look for a spell, you type in the spell name and get the results. The div 'wiki wikiPage' has the spell data in it. I want to get just that div and display it. Sadly I can’t connect to any form of database with spells, it is hosted by Wikispaces and they don’t allow that.

I hope this has been clear, ask me for more details if you like. Here is what I have so far:

<?php
if(isset($_POST['submit']))
{
    $spell=$_POST['spell'];
    $pvpwiki="http://pvpzone.wikispaces.com/";
    $site=$pvpwiki . $spell;
    $submit=true;
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Spell search</title>
    </head>
    <body>
        <form name="spellsearch" id="spellsearchform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="text" name="spell" value="<?php if(!isset($_POST['submit'])){echo("Vanish");}?>"></input>
            <input type="submit" value="Search" name="submit"></input>
        </form>
            <?php
                $doc = new DomDocument;
                $doc->validateOnParse = true;
                $doc->loadHtml(file_get_contents($site));
                var_dump($doc->getElementById('wiki wikiPage'));

                if($doc == false && $submit)
                {
                    echo("<br />" . "That is not a spell!");
                }
            ?>
    </body>
</html>

My problem right now is I am getting a parse error (Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID target_editor already defined in Entity, line: 212 in /home/content/d/e/x/dext0459/html/russellsayshi/phpspellsearch.php on line 24 NULL), I would really appreciate your help.

preguntado el 22 de septiembre de 12 a las 16:09

@Mahn: Good hint, but actually it turned out that this was not the issue. Anyway, in case that is the issue, you can get elements which have duplicate id attribute values with xpath. -

1 Respuestas

The error message you see is just a warning:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID target_editor already defined in Entity, line: 212 in /home/content/d/e/x/dext0459/html/russellsayshi/phpspellsearch.php on line 24 NULL

You can ignore those, they don't stop you. If you see them on your website, you have not configured it properly, you should log errors, not display them.

Anyway in that case for that library you can disable them this way, too:

libxml_use_internal_errors(true);

call it before loading the HTML. That HTML btw. didn't cause the error when I tried with that website.

The next mistake is that you are looking for a class not for an ID. Look for the ID instead:

$div = $doc->getElementById('content_view');

The whole code example:

function get_wiki_page_div($page)
{
    $url = sprintf('http://pvpzone.wikispaces.com/%s', urlencode($page));

    $doc = new DOMDocument();
    $doc->validateOnParse = true;
    libxml_use_internal_errors(true);

    $doc->loadHTMLFile($url);

    $div = $doc->getElementById('content_view');

    if (!$div) {
        return false;
    }

    return $doc->saveXML($div);
}

Uso:

<?php
$submit = isset($_POST['submit']);
if ($submit)
{
    $spell  = $_POST['spell'];
    $result = get_wiki_page_div($spell);
}
?>

...


<?php
if ($submit)
{
    echo $result ? $result : '<div>This is not a Spell!</div>';
}
?>

Respondido el 22 de Septiembre de 12 a las 17:09

Thank you so much, you are a lifesaver! This worked great, really appreciate it. - Russellsayshi

Never thought I'd see @hakra suggesting to ignore a warning. But I suppose it's acceptable in this case it's just DOMDocument being odd. One is left to wonder why did DOMDocument show the warning in the first place though... - mahn

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.