I’m using DOMDocument to retrieve several bits of text from a webpage and place them into an array. The same code works on another server, yet doesn’t on mine. I get Trying to get property of non-object for each iteration of the while loop and the array remains empty at the end.
$html = file_get_contents("http://sugarkettle.site44.com/catering.html");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$meatPrices = array();
function fillArrayFromDOM($array,$type) {
global $doc;
$i = 0;
$label = 1;
$array = array();
while ($label <= 15):
$array[$i] = $doc->getElementById($type.$label)->textContent;
$i++;
$label++;
endwhile;
return $array;
}
fillArrayFromDOM($meatPrices,"meat");
echo var_dump($meatPrices);
Here’s a link to it working:
http://www.evintr.com/willtest.php
He’s running a GoDaddy server and I have a local WAMP (2.2) server. Any configuration options I can provide that might explain why this is happening? Or does the problem have nothing to do with my server config?
Any help much appreciated. Thanks in advance!
Update 1 – 11/16/12
On my server, I’ve tested $meatPrices[1] = $doc->getElementById('meat1')->textContent; and it works. For whatever reason, inside the while loop the same expression (except with variables in the getElementById parameters) tosses an error: Trying to get property of non-object.
Update 2 – 11/17/12
My WAMP server is running PHP version 5.3.13.
My friend’s server is running PHP version 5.3.6.
Try with adding
allow_url_fopen=onin your PHP configuration file (php.ini). Save it and restart Apache; it should work…EDIT:
Check also if you have extension
php_openssl.dllenabled (extension=php_openssl.dllin yourphp.inifile). Again, restart of Apache would be required.EDIT:
It depends on PHP version you have but here are two potential solutions:
fillArrayFromDOM($meatPrices,"meat");with$meatPrices = fillArrayFromDOM($meatPrices,"meat"); You can alsochange your function to remove necessary $meatPrices parameter).
or
function fillArrayFromDOM($array,$type) {withfunction fillArrayFromDOM(&$array,$type) { //note new character &; it will keep reference to $array variable so it could be changeable;you can also remove line:$array = array();Both should work; I am in rush have no time to wait on your comment response. Let us know what you get…