2011-08-20 54 views
1

我有一个PHP“foreach”脚本,用于从选定的YouTube频道中提取所有上传的视频(与每个相关数据)。然后,我可以选择我想要的视频。 现在我已经在YouTube论坛上提问过了,但他们不愿意在专门的api之外提供帮助。sxml feed中的php检查(如果存在节点)

到目前为止,我只有一个单一的YouTube频道,但为了我的网站的工作,我需要知道,所有渠道将工作。

对.....这是问题所在。 这就是我得到的。

警告:主()[function.main]:节点不再xxxxxxxx.php存在于管线24 警告:主()[function.main]:节点不再在上线xxxxxxxx.php存在24 致命错误:调用一个成员函数属性()非对象上在xxxxxxxx.php第27行

/////////////////// // PHP代码/////////////////////////

$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; 
    } 

} 

现在我试过了这个(以我有限的knowladge)

// get video player URL 
$attrs = $media->group->player->attributes(); 
if (($attrs =='') || (!$attrs)) { //////no need to double up, but wanted to make sure 
continue ; 
} 

这然后加载我的网页,但不正确,而且还给出了一个错误。

警告:主要()[function.main]:节点不再存在xxxxxxxx.php第21行

现在看到的,因为我需要通过foreach循环多次去,有几个节点要通过,我怎样才能检查,绕过并继续没有错误弹出?

回答

0

要完全删除PHP的警告信息,您可以使用...

error_reporting(E_ERROR | E_PARSE); 
+0

埋葬的警告是不是一个解决方案,因为也有一个致命错误之后的警告。 – Shef

+0

@Shef,偏离过程,但在他修正某些问题后(他的帖子的最后部分再次看到),其余的错误只是一个PHP警告,致命错误在他之前的代码中。所以我的回答是“继续没有出现错误”的特殊效果。 – toopay

+0

@toopay,我把它放在我的脚本中,它似乎与我的“继续”功能一起工作。仍然需要研究它,看看我是否需要更多或其他的东西。谢谢。现在会做一些更多的测试,看看它是否打破.....;) – scrappy

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; 
} 
+0

尚未尝试过你的脚本,但会尽快有更多的时间。可能不需要,但只会在更多的测试后才知道。谢谢。 – scrappy

+0

注意到你的名字没有通过,因为你已经为我编码了,我以为你应该收到回复通知。将尝试你的代码,但目前“旁路”正在工作(从“toopay”)。不是最好的代码练习,但如果它能工作,不能敲它;) – scrappy

+0

重点是解决你的问题,以便它适合你。祝你好运,如果你有任何问题与答复更新线程。 –

相关问题