Jump to content

Cannot seem to get PHP Dom working


deadliver

Recommended Posts

I am pulling back data from a site using curl.

I wanted to simply use dom to some information from the table within.

 

I know the curl is giving the information, but the bit of code to find the tag names

and return info is not.

 

Question is this..could this have anything to do with actual php.ini settings?

 

I have not programmed anything php in 7 years so I am head scratching.

 

 

/** LOAD SETTINGS **/

$url="xxx";   

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  $string = curl_exec($ch);
  curl_close($ch);

// get DOM from URL or file
$doc = new DOMDocument(); 
$doc = str_get_html($string);
$elements = $doc->getElementsByTagName('td'); 
echo $elements->item(0)->nodeValue;   

 

The echo fo the td information returns nothing whatsoever.

 

 

Link to comment
https://forums.phpfreaks.com/topic/268449-cannot-seem-to-get-php-dom-working/
Share on other sites

So can you post the code you have right now, then? What I assume you have seems right: make sure your php.ini is right for a development environment with

error_reporting = -1
display_errors = on

and make sure you're getting some sort of error message that we can work with.

You do realize that you're overwriting the DOM object here, right?

$doc = str_get_html($string)

 

That is, unless str_get_html () returns a new DOM object, or you're using loadHTML () correctly (which involves more than just replacing the function name).

In any case, enable error messages and you'll be told exactly why and where it fails.

I apologize, I the server is hosted so I can't just open my php.ini etc.  I have to wait for answers.  Here is what I have found is my issue, and I am not sure why.

 

SCRIPT 1: Works

<?
$doc = new DOMDocument();
$doc->loadHTML("<table><tr><td>Table Cell1</td></tr><tr><td>Table Cell2</td></tr></table>");
echo $doc->saveHTML();
?>

 

 

 

SCRIPT 2: Fatal error: Call to undefined function loadhtml() in /home/hulldown/public_html/phpsig/test.php on line 12

 

<?
$curlstr = "<table><tr><td>Table Cell1</td></tr><tr><td>Table Cell2</td></tr></table>";


$doc = new DOMDocument(); 
$doc = loadHTML($curlstr);
echo $doc->saveHTML();
?>

 

The only difference is passing a string variable instead of an actual string to loadHTML().

 

I feel so stupid because it does not make sense to me what is going on.

 

 

The only difference is passing a string variable instead of an actual string to loadHTML().

No, there's another difference:

Fatal error: Call to undefined function loadhtml() in /home/hulldown/public_html/phpsig/test.php on line 12

You're calling loadhtml(). Lowercase. Function names are case-sensitive.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.