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
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

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.