salve a tutti
Ho un bel problema e non riesco a venirne a capo quindi spero che menti più esperte nel campo della mia sappiano risolverlo.. Ho un codice che ottiene un xml da youtube con tutte i video pubblicati da uno specifico canale e dopo averlo elaborato stampa alcune info nella mia pag web. Il codice l'ho trovato online e l'ho leggermente modificato.
Il problema sta nel fatto che questo codice a volte funziona e a volte no. In pratica a volte invece di darmi durata titolo ecc. non campare nulla..Altre volte tutto funziona perfettamente. Non riesco proprio a capire il perchè di questo funzionamento
Eccolo:
ottengo xml e lo elaboro
codice:
<?php
$gotPAr=false;
$arrayId = array(); // array che conterrà gli id dei video
$arrayTitle = array(); // array che conterrà il titolo di ogni video
$arrayViews = array(); // array che conterrà il numero delle visite
$arrayDate = array(); // array che conterrà le date di upload
$arrayDescription = array(); // array che conterrà le descrizioni dei video
$arrayDuration = array(); // array che cntiene la durata del video
$arrayRating = array(); // array che conterrà il rating del video
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/users/valoriassolutiOCH/uploads';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
$temp_updated=0;
$temp_count=0;
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
array_push($arrayId, substr($entry->id,-11));
array_push($arrayTitle, $media->group->title);
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
array_push($arrayDuration,$length);
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
array_push($arrayViews, (int)$viewCount);
// get <gd:rating> node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
array_push($arrayRating, $rating);
$updated = $entry->updated;
array_push($arrayDate, $updated);
if ($updated > $temp_updated)
{
$temp_updated = $updated;
}
if($viewCount > $temp_count)
{
$temp_count = $viewCount;
}
array_push($arrayDescription, $media->group->description);
}
$tmp=0;
$index=0;
for($i=0; $i<count($arrayViews); $i++)
{
if ($arrayViews[$i]>$tmp)
{
$tmp = $arrayViews[$i];
$index = $i;
}
}
?>
<?php if ( (isset($_GET['ind'])) && (isset($_GET['id'])) )
{
if ( (trim($_GET['ind']) != "") && (trim($_GET['id']) != ""))
{
$id = trim($_GET['id']);
$ind = trim($_GET['ind']);
$gotPAr = true;
}
else
{
$id = $arrayId[0];
$ind = 0;
}
}
else
{
$id = $arrayId[0];
$ind = 0;
}
?>
organizzo la mia pag web
codice:
<p style="font-size:19px; font-weight:bold"><?php echo substr($arrayTitle[$ind],0,45)."..."; ?></p>
<object width="480" height="390" style="margin-top:-10px;">
<param name="movie"
value="http://www.youtube.com/v/<?php echo $id;?>?version=3&autohide=1&showinfo=0"></param>
<param name="allowScriptAccess" value="always"></param>
<embed src="http://www.youtube.com/v/<?php echo $id;?>?version=3&autohide=1&showinfo=0"
type="application/x-shockwave-flash"
allowscriptaccess="always"
width="480" height="390"></embed>
</object>
<table width="478" height="29"m border="0" bordercolor="#000000" style="float:left">
<tr>
<td width="365" height="23"> Visite:<?php echo $arrayViews[$ind]; ?> </td>
<td width="97" height="23"> Rating:<?php printf('%0.2f', $arrayRating[$ind]); ?> </td> </tr>
</table>
<table width="478" height="29"m border="0" bordercolor="#000000" style="float:left">
<tr>
<td width="291" height="23"> Durata:<?php printf('%0.2f', $arrayDuration[$ind]/60);?></td>
<td width="171" height="23"><a href="home.php?ind=<?php echo $index; ?>&id=<?php echo $arrayId[$index];?>" style="font-weight:600"><?php
if ($gotPAr == false){echo 'Most Popular Song >>';} else {echo 'Latest Song >>';}?>
</a></td>
</tr>
</table>