Jump to content

PHP Simple HTML DOM Parser - Compile error in PHP 5.2 when used as object


saijin_nxtoyou

Recommended Posts

I'm using PHP 5.2 Server and Simple HTML DOM 1.5. This script scrape or extract data from a football site, its fully working on PHP 5.9 Server but I need to know how I can fix it for PHP 5.2 server. Can someone give me a hint on how can I fix the error? Thanks in advance.

 

My PHP 5.2 Server script output shows:

++++++++++++++++

Object id #599 Object id #604 Object id #609 Object id #614 Object id #619

Object id #627 Object id #632 Object id #637 Object id #642 Object id #647

Object id #655 Object id #660 Object id #665 Object id #670 Object id #675

Object id #683 Object id #688 Object id #693 Object id #698 Object id #703

Object id #711 Object id #716 Object id #721 Object id #726 Object id #731

++++++++++++++++

 

while PHP 5.9 Server says

++++++++++++++++

Rk Player Team POS OPPONENT

1 Aaron Rodgers GB QB at CAR

2 Tom Brady NE QB vs. SD

3 Matt Schaub HOU QB at MIA

4 Michael Vick PHI QB at ATL

++++++++++++++++

 

I did applied the bug solution listed on https://sourceforge.net/tracker/index.php?func=detail&aid=3107230&group_id=218559&atid=1044037 but it is still not working. It says:

++++++++++++++++

Details:

 

I get compiler errors in PHP 5.2 when using this as an object.

 

The offending lines are 609 and 940, which both contain this construct:

 

if ($this->size>0) $this->char = $this->doc[0];

 

This tries to get the first character of $this->doc, but PHP 5.2 sees it as trying to access it as an array. It's easily fixed by this:

 

if ($this->size>0) $this->char = substr($this->doc, 0, 1);

 

Or you could probably use chr(ord($this->doc)) as well. Either way solves the compile error without changing functionality.

++++++++++++++++

 

Here are my codes:

 

<?php
# don't forget the library
include('simple_html_dom.php');

# this is the global array we fill with article information
$articles = array();
$source = 'http://www.athlonsports.com/columns/winning-game-plan/fantasy-football-qb-rankings';
# passing in the first page to parse, it will crawl to the end
# on its own
getArticles($source);      


function getArticles($page) {
global $articles, $descriptions;

$html = new simple_html_dom();
$html->load_file($page);

//$items = $html->find('div[class=preview]');
$items = $html->find('tbody tr');

foreach($items as $post) {
    # remember comments count as nodes
   /*$articles[] = array($post->children(3)->outertext,
                        $post->children(6)->first_child()->outertext);*/
    $articles[] = array($post->children(0), $post->children(1), $post->children(2), $post->children(3), $post->children(4));
}

# lets see if there's a next page
if($next = $html->find('a[class=nextpostslink]', 0)) {
    $URL = $next->href;
    echo "going on to $URL <<<\n";
    # memory leak clean up
   $html->clear();
    unset($html);

    getArticles($URL);
}
}

?>


<html>
<head>
</head>
<body>
<?
echo "Source: " . $source;
?>
<table cellpadding="5" cellspacing="0" border="0">
<?php
    foreach($articles as $item) {
        echo "<tr>";
        echo "<td>" . $item[0] . "</td><td>" . $item[1] . "</td><td>" . $item[2] . "</td>";
        echo "<td>" . $item[3] . "</td><td>" . $item[4] . "</td>";
        echo "<tr>";
    }
?>
</table>


</body>
</html>

Link to comment
Share on other sites

Sorry.. Here is the correct info:

 

I'm using PHP 5.1.6 Server and Simple HTML DOM 1.5. This script scrape or extract data from a football site, its fully working on PHP 5.2.17 Server. I need to know how I can fix it for PHP 5.1.6 server. Can someone give me a hint on how can I fix the error? Thanks in advance.

 

My PHP 5.1.6 Server script output shows:

++++++++++++++++

Object id #599 Object id #604 Object id #609 Object id #614 Object id #619

Object id #627 Object id #632 Object id #637 Object id #642 Object id #647

Object id #655 Object id #660 Object id #665 Object id #670 Object id #675

Object id #683 Object id #688 Object id #693 Object id #698 Object id #703

Object id #711 Object id #716 Object id #721 Object id #726 Object id #731

++++++++++++++++

 

PHP 5.2.17 Server says

++++++++++++++++

Rk Player Team POS OPPONENT

1 Aaron Rodgers GB QB at CAR

2 Tom Brady NE QB vs. SD

3 Matt Schaub HOU QB at MIA

4 Michael Vick PHI QB at ATL

++++++++++++++++

 

I did applied the bug solution listed on https://sourceforge.net/tracker/index.php?func=detail&aid=3107230&group_id=218559&atid=1044037 but it is still not working. It says:

++++++++++++++++

Details:

 

I get compiler errors in PHP 5.2 when using this as an object.

 

The offending lines are 609 and 940, which both contain this construct:

 

if ($this->size>0) $this->char = $this->doc[0];

 

This tries to get the first character of $this->doc, but PHP 5.2 sees it as trying to access it as an array. It's easily fixed by this:

 

if ($this->size>0) $this->char = substr($this->doc, 0, 1);

 

Or you could probably use chr(ord($this->doc)) as well. Either way solves the compile error without changing functionality.

++++++++++++++++

 

Here are my codes:

 

<?php
# don't forget the library
include('simple_html_dom.php');

# this is the global array we fill with article information
$articles = array();
$source = 'http://www.athlonsports.com/columns/winning-game-plan/fantasy-football-qb-rankings';
# passing in the first page to parse, it will crawl to the end
# on its own
getArticles($source);      


function getArticles($page) {
global $articles, $descriptions;

$html = new simple_html_dom();
$html->load_file($page);

//$items = $html->find('div[class=preview]');
$items = $html->find('tbody tr');

foreach($items as $post) {
    # remember comments count as nodes
   /*$articles[] = array($post->children(3)->outertext,
                        $post->children(6)->first_child()->outertext);*/
    $articles[] = array($post->children(0), $post->children(1), $post->children(2), $post->children(3), $post->children(4));
}

# lets see if there's a next page
if($next = $html->find('a[class=nextpostslink]', 0)) {
    $URL = $next->href;
    echo "going on to $URL <<<\n";
    # memory leak clean up
   $html->clear();
    unset($html);

    getArticles($URL);
}
}

?>


<html>
<head>
</head>
<body>
<?
echo "Source: " . $source;
?>
<table cellpadding="5" cellspacing="0" border="0">
<?php
    foreach($articles as $item) {
        echo "<tr>";
        echo "<td>" . $item[0] . "</td><td>" . $item[1] . "</td><td>" . $item[2] . "</td>";
        echo "<td>" . $item[3] . "</td><td>" . $item[4] . "</td>";
        echo "<tr>";
    }
?>
</table>


</body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.