10

So i've spent a lot of time to write a script that do a certain task , When I was testing it on my local machine it work fine, But when I upload it to my hosting it give me this error

Fatal error: Cannot use object of type DOMNodeList as array

this is a sample of what the script do

$xml = new DOMDocument();
$xml->loadHTML($html);

$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@style='background: #aaaaaa']")->item(0);



$rows = $table->getElementsByTagName("tr");

foreach ($rows as $row) {
    if($row->getAttribute('align') === 'center') {
  $cells = $row -> getElementsByTagName('td');

  // I GET THE ERROR FROM THIS LINE
  $add = mysql_escape_string(utf8_decode($cells[0]->nodeValue));

  Some logic 

  }

Like I said it works fine on my local machine , And I get the error when I run it on my hosting

I used this code to get the loaded extensions because I thought that the problem might be from there

print_r(get_loaded_extensions());

And this is the result from my machine

Array
(
    [0] => Core
    [1] => bcmath
    [2] => calendar
    [3] => ctype
    [4] => date
    [5] => ereg
    [6] => filter
    [7] => ftp
    [8] => hash
    [9] => iconv
    [10] => json
    [11] => mcrypt
    [12] => SPL
    [13] => odbc
    [14] => pcre
    [15] => Reflection
    [16] => session
    [17] => standard
    [18] => mysqlnd
    [19] => tokenizer
    [20] => zip
    [21] => zlib
    [22] => libxml
    [23] => dom
    [24] => PDO
    [25] => bz2
    [26] => SimpleXML
    [27] => wddx
    [28] => xml
    [29] => xmlreader
    [30] => xmlwriter
    [31] => apache2handler
    [32] => openssl
    [33] => curl
    [34] => mbstring
    [35] => exif
    [36] => gd
    [37] => gettext
    [38] => intl
    [39] => mysql
    [40] => mysqli
    [41] => Phar
    [42] => pdo_mysql
    [43] => pdo_sqlite
    [44] => soap
    [45] => sockets
    [46] => sqlite3
    [47] => xmlrpc
    [48] => xsl
    [49] => mhash
)

and from my hosting

Array
(
    [0] => Core
    [1] => date
    [2] => ereg
    [3] => libxml
    [4] => openssl
    [5] => pcre
    [6] => sqlite3
    [7] => zlib
    [8] => bcmath
    [9] => bz2
    [10] => calendar
    [11] => ctype
    [12] => curl
    [13] => dom
    [14] => hash
    [15] => fileinfo
    [16] => filter
    [17] => ftp
    [18] => gd
    [19] => gettext
    [20] => SPL
    [21] => iconv
    [22] => session
    [23] => intl
    [24] => json
    [25] => mbstring
    [26] => mcrypt
    [27] => standard
    [28] => mysql
    [29] => mysqli
    [30] => pgsql
    [31] => mysqlnd
    [32] => Phar
    [33] => posix
    [34] => pspell
    [35] => Reflection
    [36] => imap
    [37] => SimpleXML
    [38] => soap
    [39] => sockets
    [40] => exif
    [41] => tidy
    [42] => tokenizer
    [43] => xml
    [44] => xmlreader
    [45] => xmlrpc
    [46] => xmlwriter
    [47] => xsl
    [48] => zip
    [49] => cgi-fcgi
    [50] => PDO
    [51] => pdo_sqlite
    [52] => pdo_mysql
    [53] => ionCube Loader
    [54] => Zend Guard Loader
)

I have no idea why I get the error

joko liptos
  • 175
  • 2
  • 8
  • Are you sure your local and remote machines are configured the same way? Libraries and everything? This is the first question when something works in a place and not in another. – Ed de Almeida Mar 07 '16 at 14:19
  • My local machine is windows and my hosting is linux, tell me what other configurations you want to know and I will post them her – joko liptos Mar 07 '16 at 14:23
  • 1
    This is really a problem. Some libraries will work differently in these two systems. If you want a good suggestion, install a linux virtual machine in your local machine and use it to develop. You'll make your life easier. – Ed de Almeida Mar 07 '16 at 14:29
  • It would be much easier if I re wrote the whole thing, The problem I can't figure out how to get the valuse of $cells – joko liptos Mar 07 '16 at 14:32
  • Web development in Windows is as a headache with an arm missing and a hand cutted. – Marcos Pérez Gude Mar 07 '16 at 14:32
  • 1
    You may say that again, @MarcosPérezGude! I already suffered a lot with Windows environments. At home I use just Linux, but some customers use windows and when I'm in their machines it is a terrible experience. – Ed de Almeida Mar 07 '16 at 14:36
  • @EddeAlmeida How can I know what is inside $cells so I can know what to extract, var_dump won't work – joko liptos Mar 07 '16 at 14:37
  • `var_dump($cells)` must work. – Marcos Pérez Gude Mar 07 '16 at 14:37
  • According to the answer @purpelninja posted, this returns a DOMNodeList. There must be a method in this object to see the contents of it. I'm going to do some research here. Give me a minute or two. – Ed de Almeida Mar 07 '16 at 14:39
  • See http://php.net/manual/en/class.domnodelist.php, where there is a good example of transversing a DOMNodeList object. You may use it with some changes. – Ed de Almeida Mar 07 '16 at 14:41
  • @EddeAlmeida I fiexed using your example ` $cells = $row -> getElementsByTagName('td'); $cells_array = array(); foreach ($cells as $node) { $cells_array[] = $node; }` Now I am not sure what to do, Should I accept the other answer or what ? – joko liptos Mar 07 '16 at 14:58
  • @purpleninja put us in the right track. It is completely fair if you accept his answer. – Ed de Almeida Mar 07 '16 at 15:01

2 Answers2

29

getElementsByTagName() returns a DOMNodeList, which implements ArrayAccess as of PHP 5.6.3. This is what allows you to access a node within via $cells[0].

In prior versions you'll need to use DOMNodeList's item() method to access a specific index, e.g. $cells->item(0).

user3942918
  • 24,679
  • 11
  • 53
  • 67
-4

Please read the documentation:

http://php.net/manual/en/domdocument.getelementsbytagname.php

This function returns a new instance of class DOMNodeList containing all the elements with a given local tag name.

It's an object and not an array which means you can not use $cells[0].

purpleninja
  • 376
  • 3
  • 11
  • 1
    Yes, I think the same, but why it works in local machine? – Marcos Pérez Gude Mar 07 '16 at 14:33
  • 1
    different php version, perhaps? – Meetai.com May 22 '16 at 01:36
  • 4
    Down-voted for the only-a-statement-but-no-solution context. That's just lazy. I'm down-voting the OP for being lazy and accepting this. If you do actually decide to update the answer I'll be happy to up-vote it if it works. – John Apr 11 '17 at 21:28
  • 1
    Please have a look at the second answer, which is correct, because this issue is caused by different PHP versions. – Marc Sep 19 '17 at 07:43