PHP 5 simplexml incompatability?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psm9640
Forum Newbie
Posts: 14
Joined: Tue May 29, 2007 3:59 pm

PHP 5 simplexml incompatability?

Post by psm9640 »

I have the following code working PERFECTLY in PHP 5.2.0 but get errors when dealing with the child elements for "media" and "bc" in PHP 5.1.4.

Code: Select all

$rss = simplexml_load_file('http://link.brightcove.com/services/link/bcpid960594405?action=mrss&v=2');



$a = 1; //set beginning of increment



foreach($rss->channel->item as $item) {



                echo '<p>',$a,' - title: ', $item->title, "<br />\n";

                echo 'link: ', $item->link, "<br />\n";

                echo 'description: ', $item->description, "<br />\n";

                echo 'pubDate: ', $item->pubDate, "<br />\n";



$e = 0; //set beginning of increment				

foreach($item->children('media', true) as $media) {







        foreach ($media->attributes() as $key => $value) {

       

        echo $media->getName(), " {$key}: {$value}<br>";

		$value2[$e] = $value;

		echo "$value2[$e]<br>";



$e++;

}



}

$i = 0; //set beginning of increment



foreach($item->children('bc', true) as $bc) {

        //echo 'bc: ', $bc->getName(), ": ",$bc, "<br />\n";



//assign unique array variables via loop

		$bc2[$i] = $bc;

		echo "$bc2[$i]<br>";

		$i++;

}

}

Here's the error:

Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /home/content/f/a/f/faftp/html/video/import.php on line 16

Warning: Invalid argument supplied for foreach() in /home/content/f/a/f/faftp/html/video/import.php on line 16

Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /home/content/f/a/t/faftp/html/video/import.php on line 32

Warning: Invalid argument supplied for foreach() in /home/content/f/a/f/faftp/html/video/import.php on line 32
Is this simply an incompatibility with the versions or does anyone have an idea as to tweaking the code to make it friendly for both? Thanks in advance!
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

http://lt.php.net/manual/en/function.si ... ildren.php

Second parameter in SimpleXMLElement->children() function was added in PHP 5.20, so the function in PHP 5.14 doesn't expect second parameter and brings up an error. And those second and last errors follow the first and third errors.
psm9640
Forum Newbie
Posts: 14
Joined: Tue May 29, 2007 3:59 pm

Post by psm9640 »

Well, that makes sense. If anyone has any suggestions as to how to work around this without re-writing from scratch, it would be appreciated greatly.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Well, you could check for PHP version inside the script. If it's lower than 5.2 then use only one parameter in SimpleXMLElement->children() function. That would get rid of those errors.
psm9640
Forum Newbie
Posts: 14
Joined: Tue May 29, 2007 3:59 pm

Post by psm9640 »

Well, unfortunately eliminating a parameter completely screws the entire series of nested loops for the elements. I apologize, my PHP skill is moderate at best.

So I changed the following from my original script:

Code: Select all

foreach($item->children('media', true) as $media) {
foreach($item->children('bc', true) as $bc) {
to

Code: Select all

foreach($item->children('media') as $media) {
foreach($item->children('bc') as $bc) {
and as I expected I have errors all over the place. I again apologize. I just need a little illustration to get me in the right direction...
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

You may try to bypass these warnings by putting @ before $item->children('media', true) as $media. It would look like this:

Code: Select all

foreach(@$item->children('media', true) as $media) {
foreach(@$item->children('bc', true) as $bc) {
Now script should not show any errors, but I'm not sure if script will continue after getting an error.
psm9640
Forum Newbie
Posts: 14
Joined: Tue May 29, 2007 3:59 pm

Post by psm9640 »

Yes, well it does cut down on the errors I but still can't get it to work. PLEASE, someone, if you can just point me in the right direction, I will be forever grateful. Arukomp has helped me narrow down the fact that the SimpleXMLElement->children() function does not allow for a second parameter prior to PHP 5.2.0, hence this bit of code...

Code: Select all

foreach($item->children('media', true) as $media) {
...will not work. So I guess what I need answered: is there a relatively simple workaround or do I need to go back to the beginning? I need a script that can work and handle obnoxious things like namespaces and attributes in 5.1.4. Thanks as always! (here's the full code again:)

Code: Select all

$rss = simplexml_load_file('http://link.brightcove.com/services/link/bcpid960594405?action=mrss&v=2');



$a = 1; //set beginning of increment



foreach($rss->channel->item as $item) {



                echo '<p>',$a,' - title: ', $item->title, "<br />\n";

                echo 'link: ', $item->link, "<br />\n";

                echo 'description: ', $item->description, "<br />\n";

                echo 'pubDate: ', $item->pubDate, "<br />\n";



$e = 0; //set beginning of increment				

foreach($item->children('media', true) as $media) {







        foreach ($media->attributes() as $key => $value) {

       

        echo $media->getName(), " {$key}: {$value}<br>";

		$value2[$e] = $value;

		echo "$value2[$e]<br>";



$e++;

}



}

$i = 0; //set beginning of increment



foreach($item->children('bc', true) as $bc) {

        //echo 'bc: ', $bc->getName(), ": ",$bc, "<br />\n";



//assign unique array variabls via loop

		$bc2[$i] = $bc;

		echo "$bc2[$i]<br>";

		$i++;

}



}
psm9640
Forum Newbie
Posts: 14
Joined: Tue May 29, 2007 3:59 pm

Post by psm9640 »

Well, I found my best remedy for the situation was to fall back on xpath:

Code: Select all

$rss = simplexml_load_file('http://link.brightcove.com/services/link/bcpid958801320?action=mrss&v=2');

$a = 1; //set beginning of increment

foreach($rss->channel->item as $item) {

                echo '<p><b>',$a,' - Title: ', $item->title, "</b><br />\n";
                echo "Link: <a href=\"$item->link\" target=\"_new\">$item->link</a><br>";
                echo 'Description: ', $item->description, "<br />\n";
                echo 'pubDate: ', $item->pubDate, "<br />\n";
                

foreach ($item->xpath('media:content') as $mediaLg) {
echo "<p>Large Thumb=><a href=\"$mediaLg[url]\" target=\"_new\">",$mediaLg['url'],'</a>'; }
foreach ($item->xpath('media:thumbnail') as $mediaSm) {
echo "<br>Small Thumb=><a href=\"$mediaSm[url]\" target=\"_new\">",$mediaSm['url'],'</a>'; }

foreach($item->xpath('bc:playerid') as $key => $playerid) {
echo '<p>bc:playerid=>',$playerid; }
foreach($item->xpath('bc:lineupid') as $key => $lineupid) {
echo '<br>bc:lineupid=>',$lineupid; }
foreach($item->xpath('bc:titleid') as $key => $titleid) {
echo '<br>bc:titleid=>',$titleid; }
}
Post Reply