Jump to content

Tutorial - missing something maybe?


DavidPr
 Share

Recommended Posts

Yeah, you've got the right idea. You missed the first item part though:$x->item($i)->...I'm not sure about that DOMDocument error, frankly the error message is wrong. The constructor for DOMDocument does not expect at least 1 parameter, it has 2 parameters, and both of them are optional:http://www.php.net/manual/en/domdocument.construct.phpI guess you could do this though:new DOMDocument('1.0', 'iso-8859-1');Before doing that, I would say to fix the problems in the loop first, it doesn't really make sense that a problem in the loop farther down would affect the load higher up, but line 17 definitely doesn't have an error. You don't need to change the line before the for loop or the loop itself, PHP just seems to have some sort of issue with chaining the objects like that. The only lines that need to be changed are where it chains them. I'm not even sure why it doesn't like chaining, that should be a perfectly cromulent thing to do.

Link to comment
Share on other sites

Ok, I see what you are saying and made the change to://get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) { $tmp1 = $x->getElementsByTagName('title');$tmp2 = $tmp1->item($i);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_title = $tmp2->nodeValue; $tmp1 = $x->getElementsByTagName('link');$tmp2 = $tmp1->item($i);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_link = $tmp2->nodeValue; $tmp1 = $x->getElementsByTagName('description');$tmp2 = $tmp1->item($i);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_desc = $tmp2->nodeValue;and got this response:Warning: domdocument() expects at least 1 parameter, 0 given in /home/website/public_html/getrss.php on line 17Fatal error: Call to undefined function: load() in /home/website/public_html/getrss.php on line 18So i followed your advice and changed it to:$xmlDoc = new DOMDocument('1.0', 'iso-8859-1');$xmlDoc->load($xml);Now I am getting this:Warning: domdocument() expects parameter 2 to be long, string given in /home/website/public_html/getrss.php on line 17Fatal error: Call to undefined function: load() in /home/website/public_html/getrss.php on line 18I also tried:$xmlDoc = new DOMDocument('1.0', 'UTF-8');which came back as:Warning: domdocument() expects parameter 2 to be long, string given in /home/website/public_html/getrss.php on line 17Fatal error: Call to undefined function: load() in /home/website/public_html/getrss.php on line 18 :)

Link to comment
Share on other sites

You've got the item part in the wrong spot, that should be first. This was the original line:$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;so it should be like this:$tmp1 = $x->item($i);$tmp2 = $tmp1->getElementsByTagName('title');$tmp1 = $tmp2->item(0);$tmp2 = $tmp1->childNodes;$tmp1 = $tmp2->item(0);$item_title = $tmp1->nodeValue;there was also a missing item before childNodes.Those DOMDocument errors are confusing. It says parameter 2 should be a long, that's not correct from what I'm looking at, both parameters are definitely defined as string values. Ignore those errors for now and get the loop fixed, possibly those will go away. Like I said, the errors don't match what the PHP manual says, it's almost like it's using a different DOMDocument class than what's defined in the manual. It seemed to be working fine before we started making the changes though, so that's why I'm saying continue with the changes and see if they go away.

Link to comment
Share on other sites

Thank you so much for your help, I really appreciate it! Unfortunately the code is still giving me the DOM error.Heres the code:-----------------------------$xmlDoc = new DOMDocument('1.0', 'iso-8859-1');$xmlDoc->load($xml);//get elements from "<channel>"$tmp=$xmlDoc->getElementsByTagName('channel');$channel = $tmp->item(0);$tmp1 = $channel->getElementsByTagName('title');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$channel_title = $tmp2->nodeValue;$tmp1 = $channel->getElementsByTagName('link');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$channel_link = $tmp2->nodeValue;$tmp1 = $channel->getElementsByTagName('description');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$channel_desc = $tmp2->nodeValue;//output elements from "<channel>"echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");echo("<br />");echo($channel_desc . "</p>");//get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) {$tmp1 = $x->item($i);$tmp2 = $tmp1->getElementsByTagName('title');$tmp1 = $tmp2->item(0);$tmp2 = $tmp1->childNodes;$tmp1 = $tmp2->item(0);$item_title = $tmp1->nodeValue;$tmp1 = $x->item($i);$tmp2 = $tmp1->getElementsByTagName('link');$tmp1 = $tmp2->item(0);$tmp2 = $tmp1->childNodes;$tmp1 = $tmp2->item(0);$item_link = $tmp1->nodeValue;$tmp1 = $x->item($i);$tmp2 = $tmp1->getElementsByTagName('description');$tmp1 = $tmp2->item(0);$tmp2 = $tmp1->childNodes;$tmp1 = $tmp2->item(0);$item_desc = $tmp1->nodeValue;-------------------------------------------------------------Warning: domdocument() expects parameter 2 to be long, string given in /home/website/public_html/getrss.php on line 17Fatal error: Call to undefined function: load() in /home/website/public_html/getrss.php on line 18I am stumped, I thought the correction you supplied would have fixed it as well. Weird!

Link to comment
Share on other sites

Actually I guess this sort of makes sense, in a way. Before you were getting parse or syntax errors, when you get a syntax error the code doesn't even execute, PHP fails when it's trying to figure out the code structure before it even runs it. So we fixed the syntax errors and then it was actually able to execute, where it ran into the DOM error. So it makes sense that the DOM error didn't show up before, because it wasn't executing the code yet, but the error itself still doesn't make sense to me. When I said it was working fine before we made the changes that was wrong, it wasn't even executing the code yet.So.. The PHP manual says that the DOM classes are part of the PHP core, and I don't see any version information about it in the manual, so it doesn't sound like your server has the wrong version of the DOM classes. I guess checking the version wouldn't hurt though. Create a file with just this:<?php phpinfo(); ?>and run that. Scroll down to the section labeled "dom" and check the version information there, on the server I'm on here it says the API version is 20031129 and the libxml version is 2.6.26, the server here has PHP 5.2.0 though. Actually it looks like another issue. Check this page and scroll down to read some of the user comments:http://www.php.net/manual/en/domdocument.construct.phpIt looks like your server is using an older DOM extension that is replacing the built-in one. In fact, that might also be the reason why you were originally getting syntax errors, if PHP were to check that. Apparently you need to go into php.ini and disable the older extension so that is uses the built-in one that you're trying to use. You can find out where your php.ini is by looking at the phpinfo page. Near the top of the phpinfo output you'll see where it says Configuration File Path, and it will tell you what the path is to php.ini. Open the file that it's reporting, and scroll down to the section for extensions in php.ini. You should see a line that looks like this:extension=php_domxml.dllIn order to disable that, just add a semicolon before it:;extension=php_domxml.dllAfter doing that, you'll need to restart the web server for it to pick up the change.

Link to comment
Share on other sites

Ok, my server control panel said 5.2.9, I had already sent tech support a message. So i upped the info file for PHP, thinking I had already done this on this server. I work on a few different servers constantly for work.Well, turns out it was running PHP 4.x.xI had to literally click one button in my control panel to update the version to PHP 5.justsomeguy, Thank you so much for all of your help and trouble shooting! I really appreciate it and hopefully this will save someone else in the future!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...