Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Does this mean that DOM is not working ?

    Hi,

    I am new to using the DOMDocument() function set
    and am practicing on website that I know.

    this is my script, but I am not getting any output.

    I alştered the script to show the contents of $dom
    and I find that it is empty ??

    PHP Code:
    $target_url "http://www.expert-world.com/";
    $userAgent 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

    echo 
    "<br>Starting<br>Target_url: $target_url";

    // make the cURL request to $target_url

    $ch curl_init();
    curl_setopt($chCURLOPT_USERAGENT$userAgent);
    curl_setopt($chCURLOPT_URL,$target_url);
    curl_setopt($chCURLOPT_FAILONERRORtrue);
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);
    curl_setopt($chCURLOPT_TIMEOUT10);
    $pagecurl_exec($ch);
    if (!
    $page) {
        echo 
    "<br />cURL error number:" .curl_errno($ch);
        echo 
    "<br />cURL error:" curl_error($ch);
        exit;
    }

    //echo "<br>Page: $page";   THIS echoed the page fine when uncommented.

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    @
    $dom->loadHTML($page);

    echo 
    "<br>Dom: $dom";

    // grab all the on the page
    $xpath = new DOMXPath($dom);

    echo 
    "<br>Xpath: $xpath";

    ?> 
    The result:

    Starting
    Target_url: http://www.expert-world.com/

    Shouldn't I be seeing something more?

    Does this mean that my php5.2 is not equipped with DOMDocument() ?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    $dom is an object, it's not a string. You can't just echo $dom and see what's in it, you have to use the saveHTML() method of the DOMDocument object.

    http://us3.php.net/manual/en/domdocument.savehtml.php

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK,

    I have added the extra line:

    PHP Code:
    $target_url "http://www.support-focus.com/";
    $userAgent 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

    echo 
    "<br>Starting<br>Target_url: $target_url";



    // make the cURL request to $target_url
    $ch curl_init();
    curl_setopt($chCURLOPT_USERAGENT$userAgent);
    curl_setopt($chCURLOPT_URL,$target_url);
    curl_setopt($chCURLOPT_FAILONERRORtrue);
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);
    curl_setopt($chCURLOPT_TIMEOUT10);
    $pagecurl_exec($ch);
    if (!
    $page) {
        echo 
    "<br />cURL error number:" .curl_errno($ch);
        echo 
    "<br />cURL error:" curl_error($ch);
        exit;
    }

    //echo "<br>Page: $page";

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    @
    $dom->loadHTML($page);
    echo 
    "<br>Dom: $dom";



    // grab all the on the page
    $xpath = new DOMXPath($dom);

    echo 
    "<br>Xpath: $xpath"

    I also changed the url to: http://www.support-focus.com/

    Still I only get:

    Starting
    Target_url: http://www.support-focus.com/


    OPPS - SORRY IGNORE THIS.
    I WILL ADD SOME MORE CODE AFTER ı READ A BIT MORE
    Last edited by jeddi; 10-23-2009 at 06:06 PM.

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Ok, I have read some more and tried a few things but I
    can not get this working.

    I am trying to use the dom to get a list of links
    and this my code:


    PHP Code:
    <?php 
    /* 
    *  Get links
    */

    $target_url "http://www.support-focus.com/";
    $userAgent 'Googlebot/2.1 ([url]http://www.googlebot.com/bot.html)';[/url]

    echo 
    "<br>Starting<br>Target_url: $target_url";

    $ch curl_init();
    curl_setopt($chCURLOPT_USERAGENT$userAgent);
    curl_setopt($chCURLOPT_URL,$target_url);
    curl_setopt($chCURLOPT_FAILONERRORtrue);
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);
    curl_setopt($chCURLOPT_TIMEOUT10);
    $pagecurl_exec($ch);
    if (!
    $page) {
        echo 
    "<br />cURL error number:" .curl_errno($ch);
        echo 
    "<br />cURL error:" curl_error($ch);
        exit;
    }

    //echo "<br>Page: $page";

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    $doc->loadHTMLFile($page);

    echo 
    $doc->saveHTML();

    $params $doc->getElementsByTagName('a'); // Find  the a hrefs
    $k=0;
    foreach (
    $params as $param//go to each link1 by 1
    {
             echo 
    "Link Attribute :-> ".$params->item($k)->getAttribute('href')."<br>";   //get a

    $k++;   
             
    }
    ?>
    I have looked over it a couple of times and tried different things
    but I get no output from the screen

    (even the Starting<br>Target_url: does not display )

    Can anyone help me see what I have done wrong ?
    Thanks
    Last edited by jeddi; 10-23-2009 at 06:49 PM.

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK I am missing the DOM class

    Fatal error: Class 'DOMDocument' not found i

    I thought this was part of php 5.

    In fact the manual says so:
    There is no installation needed to use these functions; they are part of the PHP core.
    Last edited by jeddi; 10-23-2009 at 07:01 PM.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $dom = new DOMDocument();
    $doc->loadHTMLFile($page);

    echo 
    $doc->saveHTML(); 
    You managed to float from using $dom to using $doc. I suggest you stick with one or the other

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well spotted

    I fixed that, but alas my DOMdoc is still not working.

    I ran this command on my server:
    rpm -qa | grep php

    and got:

    php-mcrypt-5.1.6-15.el5.centos.1
    wbm-php-pear-1.5-1
    php-pdo-5.1.6-23.el5
    php-snmp-5.1.6-23.el5
    php-devel-5.1.6-23.el5
    php-gd-5.1.6-23.el5
    php-pear-1.4.9-4
    php-common-5.1.6-23.el5
    php-cli-5.1.6-23.el5
    php-mysql-5.1.6-23.el5
    php-pgsql-5.1.6-23.el5
    php-mbstring-5.1.6-23.el5
    php-odbc-5.1.6-23.el5
    php-imap-5.1.6-23.el5
    php-5.1.6-23.el5
    php-ldap-5.1.6-23.el5
    php-xmlrpc-5.1.6-23.el5

    The last one in the list looks like the one -
    so that means that it should be running ?

    Is there something it the php.ini file that I need to turn on ?

    You can see the result on this url:

    Curl - DOM test

    Any ideas ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •