Namespace question...

I’ve been on this for 8 hours and realize it’s probably something simple but… LOL

[php]

$cdate = date(‘D, d M Y’, time());
$url= $params->get(‘feed’);
// $url=(‘http://www.dallascowboys.com/feeds/podcasts/2531/audio’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
foreach ($xml->channel->item as $item) {
$namespaces = $item->getNamespaces(true);
$thumbnail = $item->children($namespaces[‘media’])->thumbnail;
$attr = $thumbnail->attributes();
$title= ($item->title);
$link= ($item->link);
$description= ($item->description);
$image = ($item->image);
$total= ($dc->content);
$time = $params->get(‘time’);
$isizeh = $params->get(‘isizeh’);
$isizew = $params->get(‘isizew’);
$adate = ($item->pubDate);
$newdate = date(“l, n-j-Y”, strtotime($adate));
if (strtotime($adate) >= strtotime("$time")) {
?>

<div class="box">
	<table class="tg">
	 <tr>
	 	<th class="tg-031e"><?= $img ?></th>
        <th class="tg-031e"><?= $newdate ?>
        <h3><?= $title ?></h3>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <?= $description ?>&nbsp;&nbsp;&nbsp;
        <a href="<?= $link ?>" class="hvr-shutter-in-vertical" target= "_blank">Listen Here</a>!
		</th>
	</tr>
   </table>
 </div>



<?php echo '
';
echo "URL = {$attr['url']}, width = {$attr['width']}, height = {$attr['height']}\n";
echo print_r($attr, true);
echo '
'; } } [/php] Here is the entire script and below is the xml I'm trying to get the info from: [code] <?xml version="1.0" encoding="utf-8" ?> Video

Recent videos

http://www.dallascowboys.com/ en Tue, 24 Feb 2015 15:10:07 -0600 Tue, 24 Feb 2015 15:16:25 -0600 Talkin' Cowboys: Contract Talk & Priorities In The Draft http://www.dallascowboys.com/video/2015/02/24/talkin-cowboys-contract-talk-priorities-draft Join Kelsey, Bryan, and David for their thoughts on the Combine and the continuing thoughts on free agency. 30ce5361-6a98-4e6b-b376-a15ee7d33719 Tue, 24 Feb 2015 15:10:07 -0600 Video Tue, 24 Feb 2015 15:10:07 -0600 416x234 640x360 640x360 [/code]

I can’t seem to catch the media:thumbnail object… this is what [from what I have above] it’s returning:

URL = , width = , height = SimpleXMLElement Object ( )

Which is pretty much blank… I’m trying to get the image from the xml but haven’t been able to quite capture it… is there something in the code that I’m doing wrong… well obviously yes but what am I doing wrong is a better question.

Thanks in advance!

John :wink:

Ok so I started looking for working examples out in the world and have found a couple…

[php]
$xml = simplexml_load_string($data);
foreach ($xml->channel->item as $item){

//Use that namespace
$namespaces = $item->getNameSpaces(true);
//Now we don’t have the URL hard-coded
$dc = $item->children($namespaces[‘media’]);
[/php]

It’s returning the name space using this:

[php]
var_dump($namespaces);
[/php]

and it returns this:

array(2) { ["dc"]=> string(32) "http://purl.org/dc/elements/1.1/" ["media"]=> string(29) "http://search.yahoo.com/mrss/" }

So I KNOW it’s reading the namespaces but after trying a few variations of this:

[php]
print_r ($dc->thumbnail->attributes()->url);
print_r ($dc->content->attributes()->url);
[/php]

It returns nothing…

If i just use this:
[php]
print_r ($dc->thumbnail);
print_r ($dc->content);
[/php]

It returns this:

SimpleXMLElement Object ( ) SimpleXMLElement Object ( )

Still nothing…

So I tried this:

[php]
print_r ($dc->thumbnail->attributes());
print_r ($dc->content->attributes());
[/php]

Returns the same… nothing. This is following every example out there… I wouldn’t normally even bother with this BUT I am trying to learn so I figured it’s in the xml I’m using so hey why not… NOW I’ve just totally frustrated myself for 2 days LOL

The loop I have is good… it’s basically an easy peasy loop… and in all the example pretty much the way I already had it… getting the namespaces is good… it’s getting the objects from within the namespaces that is not working…

Okay, is there anyone out there that can tell me what I’m doing wrong here?

Ok this has it returning 1 …

[php]
$image = ($img->group->content->thumbnail[0]->attributes());
[/php]

and using a foreach loop doesn’t work to return the rest…

Suggestions?

The xml you posted is invalid, not closing channel and rss tags

[hr]

If you google “SimpleXMLElement var_dump empty” you will get a more info on why this is.

[hr]

[php]$data = ’<?xml version="1.0" encoding="utf-8"?>


Video
Recent videos
http://www.dallascowboys.com/
<atom:link rel=“self” href=“http://www.dallascowboys.com/rss/video” />
en
Tue, 24 Feb 2015 15:10:07 -0600
Tue, 24 Feb 2015 15:16:25 -0600

Talkin’ Cowboys: Contract Talk & Priorities In The Draft
http://www.dallascowboys.com/video/2015/02/24/talkin-cowboys-contract-talk-priorities-draft
Join Kelsey, Bryan, and David for their thoughts on the Combine and the continuing thoughts on free agency.
30ce5361-6a98-4e6b-b376-a15ee7d33719
Tue, 24 Feb 2015 15:10:07 -0600
Video
dc:dateTue, 24 Feb 2015 15:10:07 -0600</dc:date>
media:group
<media:content url=“http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/talkin_cowboys_022415-32k.mp3” bitrate=“64.0” duration="-1" type=“video/mp4”>
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_thumb/public/_audio_thumbnails/talkin.jpg?itok=kblAEFMi” width=“130” height=“80” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_small/public/_audio_thumbnails/talkin.jpg?itok=uH4WNRot” width=“270” height=“152” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_medium/public/_audio_thumbnails/talkin.jpg?itok=THmmXhGP” width=“540” height=“360” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_large/public/_audio_thumbnails/talkin.jpg?itok=lZM4cw1r” width=“580” height=“1000” />
</media:content>
<media:content url=“http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/talkin_cowboys_022415-180k.mp4” bitrate=“148.0” duration="-1" type=“video/mp4” height=“234” width=“416”>
media:description416x234</media:description>
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_thumb/public/_audio_thumbnails/talkin.jpg?itok=kblAEFMi” width=“130” height=“80” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_small/public/_audio_thumbnails/talkin.jpg?itok=uH4WNRot” width=“270” height=“152” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_medium/public/_audio_thumbnails/talkin.jpg?itok=THmmXhGP” width=“540” height=“360” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_large/public/_audio_thumbnails/talkin.jpg?itok=lZM4cw1r” width=“580” height=“1000” />
</media:content>
<media:content url=“http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/talkin_cowboys_022415-700k.mp4” bitrate=“734.0” duration="-1" type=“video/mp4” height=“360” width=“640”>
media:description640x360</media:description>
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_thumb/public/_audio_thumbnails/talkin.jpg?itok=kblAEFMi” width=“130” height=“80” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_small/public/_audio_thumbnails/talkin.jpg?itok=uH4WNRot” width=“270” height=“152” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_medium/public/_audio_thumbnails/talkin.jpg?itok=THmmXhGP” width=“540” height=“360” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_large/public/_audio_thumbnails/talkin.jpg?itok=lZM4cw1r” width=“580” height=“1000” />
</media:content>
<media:content url=“http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/talkin_cowboys_022415-500k.mp4” bitrate=“486.0” duration="-1" type=“video/mp4” height=“360” width=“640”>
media:description640x360</media:description>
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_thumb/public/_audio_thumbnails/talkin.jpg?itok=kblAEFMi” width=“130” height=“80” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_small/public/_audio_thumbnails/talkin.jpg?itok=uH4WNRot” width=“270” height=“152” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_medium/public/_audio_thumbnails/talkin.jpg?itok=THmmXhGP” width=“540” height=“360” />
<media:thumbnail url=“http://www.dallascowboys.com/sites/dallascowboys.com/files/styles/rss_video_large/public/_audio_thumbnails/talkin.jpg?itok=lZM4cw1r” width=“580” height=“1000” />
</media:content>
</media:group>


';

$xml = simplexml_load_string($data);

echo ‘

’;
foreach($xml->channel->item as $item) {
foreach($item->children(‘media’, true) as $mediaContent) {
foreach($mediaContent->children(‘media’, true) as $mediaThumbnail) {
print_r($mediaThumbnail->attributes());
}
}
}[/php]

Did the foreaches a little verbose so you can try to fiddle around with it / dump stuff to see how it works.

NO that pretty much hit the nail on the head… WOW… Thank you.

have to play with it… it’s returning the video file which is the first object in the media array and not the thumbnail but it does work and returns every one of them :)!!

NULL SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/DS_022115-32k.mp3 [bitrate] => 64.0 [duration] => -1 [type] => video/mp4 ) ) SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/DS_022115-320k.mp4 [bitrate] => 298.0 [duration] => -1 [type] => video/mp4 [height] => 234 [width] => 416 ) ) SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/DS_022115-180k.mp4 [bitrate] => 148.0 [duration] => -1 [type] => video/mp4 [height] => 234 [width] => 416 ) ) SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/DS_022115-500k.mp4 [bitrate] => 486.0 [duration] => -1 [type] => video/mp4 [height] => 360 [width] => 640 ) ) SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://prod.video.cowboys.clubs.nfl.com/DAL/videos/dct/video_audio/2015/02-February/DS_022115-1200k.mp4 [bitrate] => 1134.0 [duration] => -1 [type] => video/mp4 [height] => 360 [width] => 640 ) )

So next thing is to figure out how to grab just the 1st thumbnail for each entry… :wink:

Just add a break after fetching the thumbnail, then it will exit the inner loop and move on to the next in the outer loop

You got it! Thanks :slight_smile:

I inserted a break; at the end and that worked… also I added two more lines after your last foreach

[php]
foreach ($xml->channel->item as $item){
foreach($item->children(‘media’, true) as $mediaContent) {
foreach($mediaContent->children(‘media’, true) as $mediaThumbnail) {
$link = (string)$mediaThumbnail->attributes()->url;
$imag = $mediaThumbnail->thumbnail->attributes()->url;
[/php]

The way it was written was picking up the audio or video file, unintended blessing as it turns out lol
But the $imag line picks up the actual image file I was after :wink:

So I’m kinda happy that I was able to figure that out… man NameSpaces are not fun LOL

Once again Thank you Jim!!

After tweaking it’s exactly what I wanted!!!

[php]
$xml = simplexml_load_string($data);

foreach ($xml->channel->item as $item){
foreach($item->children(‘media’, true) as $mediaContent) {
foreach($mediaContent->children(‘media’, true) as $mediaThumbnail) {
$link = (string)$mediaThumbnail->attributes()->url;
$imag = $mediaThumbnail->thumbnail[1]->attributes()->url;
$title= ($item->title);
$description= ($item->description);
$time = $params->get(‘time’);
$isizeh = $params->get(‘isizeh’);
$isizew = $params->get(‘isizew’);
$adate = ($item->pubDate);
$cdate = date(‘D, d M Y’, time());
$newdate = date(“l, n-j-Y”, strtotime($adate));
if (strtotime($adate) >= strtotime("$time")) {
?>
[/php]

I’m posting the final in case maybe it’ll help someone else.

Attached is the final product…


Sponsor our Newsletter | Privacy Policy | Terms of Service