youtube api search.php foreach循环错误

时间:2015-04-17 11:18:41

标签: php api youtube feeds

我的YouTube API搜索结果获取php脚本。显示错误:

SEARCH.php

<?php
 $ytapi="http://gdata.youtube.com";
?>

<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
<meta charset='UTF-8'/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Searching for videos by keyword - Muftatube</title>
    <style>
	p{font-size: 30px;color: #FFE131; font-weight: bold;}
    td {
      vertical-align: top;
    }
    td.line {
      border-bottom: solid 1px black;
    }a{color: #ff0000; font-weight:bold; text-size: 18px;}
a:hover {
background-color: yellow;
	}</style>
  </head>
  <body
<form action="search.php" method="get">
      Search: 
      <input type="text" name="vq" /> 
      Items per page: 
      <select name="i">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
      </select>
      <input type="submit" name="submit" value="Search"/>
</form></div>
<div id="nav">
</div>

<div id="section" text-align="center">

    <?php
    // if form not submitted
    // display search box
    if (!isset($_GET['vq'])) {

echo "<h2>Welcome to FullMufta Tube</h2><br /> This Is Search Page Go to home To serch and browse Videos";
    // if form submitted
    } else {
      // check for search keywords
      // trim whitespace
      // encode search string
      if (!isset($_GET['vq']) || empty($_GET['vq'])) {
        die ('ERROR: Please enter one or more search keywords');
      } else {
        $vqs = $_GET['vq'];
        $vq = ereg_replace('[[:space:]]+', ' ', trim($vqs));
        $vq = urlencode($vq);
      }

      // set max results per page
      if (!isset($_GET['i']) || empty($_GET['i'])) {
        $i = 10;
      } else {
        $i = htmlentities($_GET['i']);
        if($i>100)exit;
      }
      // set start index
      if (!isset($_GET['pageID']) || $_GET['pageID'] <= 0) {
        $o = 1;
      } else {
        $pageID = htmlentities($_GET['pageID']);
        $o = (($pageID-1) * $i)+1;
      }

      // generate feed URL
      $feedURL = $ytapi."/feeds/api/videos?vq={$vq}&max-results={$i}&start-index={$o}";

      // read feed into SimpleXML object
      $sxml = simplexml_load_file($feedURL);
        echo '<!-- $feedURL: '.$feedURL.' -->';
      ?>

      <h1>Search results: </h1><h4><?php echo $vqs; ?></h4>
<table>
      <?php
      // iterate over entries in resultset
      // print each entry's details
      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 <yt:duration> node for video length
        $yt = $media->children($ytapi.'/schemas/2007');
        $attrs = $yt->duration->attributes();
        $length = $attrs['seconds'];

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

        // get video ID
        $arr = explode('/',$entry->id);
        $id = $arr[count($arr)-1];

        // print record
        preg_match('#v\=(.*?)\&#',$watch,$vid);
        $watch='watch/?vid='.$vid[1];
        echo "<tr><td colspan=\"2\" class=\"line\"></td></tr>\n";
        echo "<tr>\n";
        echo "<td><a href=\"{$watch}\">
        <img src=\"fullmuftaimg.php?vid=$vid[1]&sz=0\"/></a></td>\n";
        echo "<td><a href=\"{$watch}\">
        {$media->group->title}</a><br/>\n";
        echo sprintf("%0.2f", $length/60) . " min. | {$rating} user rating |
        {$viewCount} views<br/>\n";
        echo $media->group->description . "<br/>\n";
        echo "</td></tr>\n";
      }
    }
    ?>
    </table>
  </body>
</html>

它以前工作但现在不能正常工作。我更改了服务器并再次运行它,但搜索结果没有出现。

错误消息

  

不推荐使用:函数ereg_replace()已弃用   第69行/home/u691059747/public_html/search.php警告:   使用simplexml_load_file():   http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1:   解析器错误:文档为空   第92行/home/u691059747/public_html/search.php警告:   simplexml_load_file():在/home/u691059747/public_html/search.php上   第92行警告:simplexml_load_file():^ in   第92行/home/u691059747/public_html/search.php警告:   使用simplexml_load_file():   http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1:   解析器错误:期望开始标记,'&lt;'找不到   第92行/home/u691059747/public_html/search.php警告:   simplexml_load_file():在/home/u691059747/public_html/search.php上   第92行警告:simplexml_load_file():^ in   第92行/home/u691059747/public_html/search.php

搜索结果:

  

警告:为foreach()提供的参数无效   第101行/home/u691059747/public_html/search.php

1 个答案:

答案 0 :(得分:0)

请注意,YouTube已停用使用其API v2获取信息,您应切换到版本3.

但无论如何,当我在你收到的$feedURL中打印search.php变量时,即:

http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1

浏览此网址会输出:

No longer available

所以你应该首先弄清楚为什么这个youtube网址不再有效(如果不是V2 / 3那么可能参数名称已经改变/等等......)

由于上面的字符串只是一个文本 - 而不是一个有效的XML字符串,因此您会收到PHP错误:

 parser error : Start tag expected, '<' not found in