PHP Classes

Problem with array result

Recommend this page to a friend!

      SimpleXML for PHP4  >  All threads  >  Problem with array result  >  (Un) Subscribe thread alerts  
Subject:Problem with array result
Summary:Problem with array result from youtube feeds
Messages:9
Author:adam8822
Date:2008-05-03 10:51:45
Update:2008-10-05 05:26:12
 

  1. Problem with array result   Reply   Report abuse  
Picture of adam8822 adam8822 - 2008-05-03 10:51:46
Hi

I have a problem with this simpleXML class


Original feed looks like this:

Array
(
[id] => http://gdata.youtube.com/feeds/api/videos/5wKsrur81_I/related
[updated] => 2008-05-03T10:16:39.182Z
[category] => Array
(
[@attributes] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://gdata.youtube.com/schemas/2007#video
)

)

[title] => Videos related to 'b-twist attempt'
[logo] => http://www.youtube.com/img/pic_youtubelogo_123x63.gif
[link] => Array

but in SimpleXML for PHP4 this looks like:

Array
(
[id] => http://gdata.youtube.com/feeds/api/videos/5wKsrur81_I/related
[updated] => 2008-05-03T10:49:41.861Z
[category] => Array
(
[@attributes] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://gdata.youtube.com/schemas/2007#video
)

)

[title] => Array
(
[@content] => Videos related to \'b-twist attempt\'
[@attributes] => Array
(
[type] => text
)

)

[logo] => http://www.youtube.com/img/pic_youtubelogo_123x63.gif
[link] => Array


Why title is Array in simplexml for php4??

  2. Re: Problem with array result   Reply   Report abuse  
Picture of Taha Paksu Taha Paksu - 2008-05-03 11:09:54 - In reply to message 1 from adam8822
Because the simplexml extension for PHP5 overrides some variables when parsing the XML file. In the original context, as you see here :

<title type="text">Videos related to 'b-twist attempt'</title>

it has an attribute in its tag. And my script does not overrides this.

Maybe this is not the best for some users, but when you expect exact things, this script is better.

  3. Re: Problem with array result   Reply   Report abuse  
Picture of adam8822 adam8822 - 2008-05-03 14:24:32 - In reply to message 1 from adam8822
How can I fix this ?

  4. Re: Problem with array result   Reply   Report abuse  
Picture of Taha Paksu Taha Paksu - 2008-05-03 15:33:48 - In reply to message 3 from adam8822
If you didn't use the "title" node a lot, you can change your code with $item->title->content() then you can access the title value. If it doesnt work, try $item->title->content()->scalar.

  5. Re: Problem with array result   Reply   Report abuse  
Picture of adam8822 adam8822 - 2008-05-03 15:59:41 - In reply to message 1 from adam8822
in my test file I have:

<?php
$file = "http://gdata.youtube.com/feeds/api/videos/5wKsrur81_I/related";
$sxml = new simplexml;
$data = $sxml->xml_load_file($file);
echo $item->title->content();
echo '<pre>';
print_r($data);
?>

but I see "Call to a member function on a non-object"


  6. Re: Problem with array result   Reply   Report abuse  
Picture of Taha Paksu Taha Paksu - 2008-05-03 18:01:10 - In reply to message 5 from adam8822
echo $item->title->content();

this is not correct. And I think you don't know what you are doing. Please first learn how to use objects and arrays.

  7. Re: Problem with array result   Reply   Report abuse  
Picture of Antonius Aji Antonius Aji - 2008-10-04 05:39:14 - In reply to message 3 from adam8822
use this:

$item -> title -> {'@content'}

to get the data of 'title' array.

rgds,
a.a

  8. Re: Problem with array result   Reply   Report abuse  
Picture of Antonius Aji Antonius Aji - 2008-10-04 05:48:28 - In reply to message 5 from adam8822
Use this bro!


<?php
$file = "http://gdata.youtube.com/feeds/api/videos/5wKsrur81_I/related";
$sxml = new simplexml;
$data = $sxml->xml_load_file($file);
echo "<br>Related Item to: ".$data -> title -> {'@content'};
echo '<pre>';
print_r($data);
?>

  9. Re: Problem with array result   Reply   Report abuse  
Picture of Antonius Aji Antonius Aji - 2008-10-05 05:26:12 - In reply to message 8 from Antonius Aji
The following example, use RSS from Picasaweb Google API:


=====


<?
require_once "simplexml.class.php";

//if you want to use it as a function;

if(!function_exists("simplexml_load_file")){
function simplexml_load_file($file){
$sx = new simplexml;
return $sx->xml_load_file($file);
}
}

//or directly:

$file = "http://picasaweb.google.com/data/feed/api/user/ahaji2002/album/top?kind=photo";
$sxml = new simplexml;
$data = $sxml->xml_load_file($file);
echo "<pre>";
//print_r($data);
echo "<br>=================Album Properties====================";
echo "<br>Last update: ".$data->updated;
echo "<br>Album Title: ".$data->title->{'@content'};
echo "<br>Album SubTitle: ".$data->subtitle->{'@content'};;
echo "<br>Album Icon: ".$data->icon;
echo "<br>Album XML Link: ".$data->link[0]->{'@attributes'}->href;
echo "<br>Album HTML Link: ".$data->link[1]->{'@attributes'}->href;
echo "<br>Album SWF Link: ".$data->link[2]->{'@attributes'}->href;
echo "<br>Album Author Name: ".$data->author->name;
echo "<br>Album Author URL: ".$data->author->uri;
echo "<br>Album Search Result: ".$data->{'openSearch:totalResults'};
echo "<br>Album ID: ".$data->{'gphoto:id'};
echo "<br>Album Name: ".$data->{'gphoto:name'};
echo "<br>Album Photo Number: ".$data->{'gphoto:numphotos'};
echo "<br>Album Name: ".$data->{'gphoto:name'};
echo "<br>Album User: ".$data->{'gphoto:user'};
echo "<br>Album Nickname: ".$data->{'gphoto:nickname'};
echo "<br>Album Comment Enable: ".$data->{'gphoto:commentingEnabled'};
echo "<br>Album Comment Count: ".$data->{'gphoto:commentCount'};
echo "<br>Album Allow Print: ".$data->{'gphoto:allowPrints'};
echo "<br>Album Allow Download: ".$data->{'gphoto:allowDownloads'};
echo "<br><br>==============Item Properties============";
$no_image = $data->{'openSearch:totalResults'};
for ($i = 0; $i < $no_image; $i++){
echo "<br>Item No. ".$i;
echo "<br>Item ID: ".$data->entry[$i]->id;
echo "<br>Item Published Date: ".$data->entry[$i]->published;
echo "<br>Item Updated Date: ".$data->entry[$i]->updated;
echo "<br>Item Title: ".$data->entry[$i]->title->{'@content'};
echo "<br>Item Summary: ".$data->entry[$i]->summary->{'@content'};
echo "<br>Item Content Type: ".$data->entry[$i]->content->{'@attributes'}->type;
echo "<br>Item Content: ".$data->entry[$i]->content->{'@attributes'}->src;
echo "<br>Item Link(".$data->entry[$i]->link[0]->{'@attributes'}->type."): ".$data->entry[$i]->link[0]->{'@attributes'}->href;
echo "<br>Item Link(".$data->entry[$i]->link[1]->{'@attributes'}->type."): ".$data->entry[$i]->link[1]->{'@attributes'}->href;
echo "<br>Item Link(".$data->entry[$i]->link[2]->{'@attributes'}->type."): ".$data->entry[$i]->link[2]->{'@attributes'}->href;
echo "<br>Item PhotoID: ".$data->entry[$i]->{'gphoto:id'};
echo "<br>Item AlbumID: ".$data->entry[$i]->{'gphoto:albumid'};
echo "<br>Item Height: ".$data->entry[$i]->{'gphoto:height'}." pixels";
echo "<br>Item Width: ".$data->entry[$i]->{'gphoto:width'}." pixels";
echo "<br>Item Size: ".$data->entry[$i]->{'gphoto:size'}." bytes";
echo "<br>Item Commenting Enabled ?: ".$data->entry[$i]->{'gphoto:commentingEnabled'};
echo "<br>Item Commenting Count: ".$data->entry[$i]->{'gphoto:commentCount'};
echo "<br>";
}
?>