【问题标题】:youtube api search.php foreach loop erroryoutube api search.php foreach 循环错误
【发布时间】:2015-06-24 05:15:23
【问题描述】:

我的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() 在 /home/u691059747/public_html/search.php 第 69 行警告: simplexml_load_file(): http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1: 解析器错误:文档为空 /home/u691059747/public_html/search.php 第 92 行警告: simplexml_load_file():在 /home/u691059747/public_html/search.php 上 第 92 行警告:simplexml_load_file(): ^ in /home/u691059747/public_html/search.php 第 92 行警告: simplexml_load_file(): http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1: 解析器错误:需要开始标记,在中未找到“

搜索结果:

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

【问题讨论】:

  • 下次不要在你的问题中使用.......!!!
  • 添加链接并尝试修复语法和拼写问题。
  • @Ofir Baruch 好的先生,我会关心这个问题
  • 尊敬的专家,基本上我想从 youtube api rss 获取输出,因为 youtube 在巴基斯坦被禁止,所以我从 github 获取此代码,但现在这不起作用,这表明错误太多我不是专业人士我是个孩子:)

标签: php api youtube feed


【解决方案1】:

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

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

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

浏览此 URL 会得到以下输出:

No longer available

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

由于上面的字符串只是一个文本 - 不是一个有效的 XML 字符串,你会得到 PHP 错误:

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

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 2018-01-22
    • 1970-01-01
    • 2021-01-09
    • 2013-12-18
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    相关资源
    最近更新 更多