我有一个PHP“foreach”脚本,用于从选定的YouTube频道中提取所有上传的视频(每个相关数据)。然后我就可以选择我想要的视频了。 现在我已经在youtube论坛上提出要求了,但他们不愿意在他们的专用api之外提供帮助。
到目前为止,我只使用了一个YouTube频道,但为了让我的网站正常运行,我需要知道所有频道都能正常工作。
对.....这是问题所在。 这就是我得到的。
警告:main()[function.main]:第24行的xxxxxxxx.php中不再存在节点 警告:main()[function.main]:第24行的xxxxxxxx.php中不再存在节点 致命错误:在第27行的xxxxxxxx.php中调用非对象的成员函数attributes()
///////////////////// PHP code //////////////////////// /
$thumb_count = 1;
if ($thumb_count <= 50 ){
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $youtube . '/uploads?start-index=1&max-results=50';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// 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'];
// get video thumbnail
$attrs = $media->group->thumbnail[3]->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'];
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['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;
}
$videoID = substr($watch, 31, 11);
$vidTitle = $media->group->title;
$channel = $entry->author->name;
$discript = $media->group->description;
$select .= '<table width="150" border="0">
<tr>
<td><div align="center"><font size="-1" >'.$media->group->title.'</font><br />
<div class="thumbnail"><a href="xxxxxx?id='.$videoID.'">
<img src="'.$thumbnail.'"width="120" height="90" /></a></div></div></td></tr>
</table><hr width="150" color="#FF0000" align="center" size="1" />';
++$thumb_count;
}
}
现在我尝试过这个(我的知识有限)
// get video player URL
$attrs = $media->group->player->attributes();
if (($attrs =='') || (!$attrs)) { //////no need to double up, but wanted to make sure
continue ;
}
然后加载我的页面,但不正确,仍然会出错。
警告:main()[function.main]:第21行的xxxxxxxx.php中不再存在节点
现在看到我需要多次经历foreach循环并且有几个节点可以通过,我如何检查,绕过并继续而不会出现错误?
答案 0 :(得分:0)
要完全删除PHP警告消息,您可以使用...
error_reporting(E_ERROR | E_PARSE);
答案 1 :(得分:0)
尝试:
$thumb_count = 1;
if ($thumb_count <= 50 ){
$domain = "gdata.youtube.com";
$path = 'feeds/api/users/' . $youtube . '/uploads?start-index=1&max-results=50';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
$sxml= loadXML2($domain, $path);
if($sxml){
// 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/');
if($media->group->player->attributes()){
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
}
// get video thumbnail
if($media->group->thumbnail[3]->attributes()){
$attrs = $media->group->thumbnail[3]->attributes();
$thumbnail = $attrs['url'];
}
// get <yt:duration> node for video length
if($media->children('http://gdata.youtube.com/schemas/2007')){
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
if($yt->duration->attributes()){
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
}
// get <yt:stats> node for viewer statistics
if($yt->statistics->attributes()){
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
}
}
// get <gd:rating> node for video ratings
if($entry->children('http://schemas.google.com/g/2005')){
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating & $gd->rating->attributes()) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
}
$videoID = substr($watch, 31, 11);
$vidTitle = $media->group->title;
$channel = $entry->author->name;
$discript = $media->group->description;
$select .= '<table width="150" border="0">
<tr>
<td><div align="center"><font size="-1" >'.$vidTitle.'</font><br />
<div class="thumbnail">
<a href="xxxxxx?id='.$videoID.'">
<img src="'.$thumbnail.'"width="120" height="90" />
</a>
</div>
</div></td>
</tr>
</table>
<hr width="150" color="#FF0000" align="center" size="1" />';
++$thumb_count;
}
}
}
// from: http://www.php.net/manual/es/function.simplexml-load-file.php#97077
function loadXML2($domain, $path, $timeout = 30) {
$fp = fsockopen($domain, 80, $errno, $errstr, $timeout);
if($fp) {
// make request
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $domain\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
// get response
$resp = "";
while (!feof($fp)) {
$resp .= fgets($fp, 128);
}
fclose($fp);
// check status is 200
$status_regex = "/HTTP\/1\.\d\s(\d+)/";
if(preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {
// load xml as object
$parts = explode("\r\n\r\n", $resp);
return simplexml_load_string($parts[1]);
}
}
return false;
}