doumei8258 2012-11-10 17:50
浏览 47
已采纳

youtube API有时会抛出错误:在非对象上调用成员函数children()

When i launch the php script, sometime works fine, but many other times it retrieve me this errror

Fatal error: Call to a member function children() on a non-object in /membri/americanhorizon/ytvideo/rilevametadatadaurlyoutube.php on line 21

This is the first part of the code

// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/dZec2Lbr_r8';

// read feed into SimpleXML object
$entry = simplexml_load_file($feedURL);

$video = parseVideoEntry($entry);


function parseVideoEntry($entry) {      
  $obj= new stdClass;

  // get nodes in media: namespace for media information
  $media = $entry->children('http://search.yahoo.com/mrss/'); //<----this is the doomed line 21 

UPDATE: solution adopted

   for ($i=0 ; $i< count($fileArray); $i++)
  {

    // set feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$fileArray[$i];


    // read feed into SimpleXML object
    $entry = simplexml_load_file($feedURL);


   if (is_object($entry))
   {
       $video = parseVideoEntry($entry);

       echo ($video->description."|".$video->length);
       echo "<br>";
    }
     else
     {
       $i--;
     }

 }

In this mode i force the script to re-check the file that caused the error

  • 写回答

2条回答 默认 最新

  • dongyo7931 2012-11-10 20:09
    关注

    Sometimes? Really? Take a look at this:

    <?php
    
    $dummy; //IN FACT, this var is NULL now
    
    // Will throw exactly the same error you get
    $dummy->children();
    

    Why? Because, we can call method from an object type.

    So, if you wanna avoid errors like this one, next time you would call the method ensure that it's "possible".

    <?php
    
    if ( is_object($dummy) && method_exists($dummy, 'children') ){
       //sure it works
       $dummy->children();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样