PHP脚本,用于使用关键字和参数获取youtube视频信息

问题描述 投票:0回答:2

我目前正在尝试找到一个脚本,该脚本可用于使用关键字和参数从youtube获取详细信息。我在脚本中想要的是,php脚本应使用持续时间参数(时间> 900秒)在视频中搜索特定关键字(例如:人们很棒),然后获取有关这些视频的详细信息(缩略图网址,视频网址,持续时间) ,desc等)。搜索了一会儿之后,我在ibm]中遇到了这个脚本

<!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">
  <head>
    <title>Searching for videos by keyword</title>
    <style>
    img {
      padding: 2px; 
      margin-bottom: 15px;
      border: solid 1px silver; 
    }
    td {
      vertical-align: top;
    }
    td.line {
      border-bottom: solid 1px black;  
    }
    </style>
  </head>
  <body>
    <?php
    // if form not submitted
    // display search box
    if (!isset($_GET['submit'])) {
    ?>
    <h1>Keyword search</h1>  
    <form method="get" action="<?php echo 
     htmlentities($_SERVER['PHP_SELF']); ?>">
      Keywords: <br/>
      <input type="text" name="vq" />
      <p/>
      Items sorted by: <br/>
      <select name="s">
        <option value="viewCount">User views</option>
        <option value="rating">User rating</option>
        <option value="published">Publication time</option>
      </select>
      <p/>
      Items per page: <br/>
      <select name="i">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
      </select>
      <p/>
      <input type="submit" name="submit" value="Search"/>  
    </form>
    <?php      
    // 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 {
        $vq = $_GET['vq'];
        $vq = ereg_replace('[[:space:]]+', ' ', trim($vq));
        $vq = urlencode($vq);
      }

      // set max results per page
      if (!isset($_GET['i']) || empty($_GET['i'])) {
        $i = 25;
      } else {
        $i = htmlentities($_GET['i']);
      }

      // set sort critera
      if (!isset($_GET['s']) || empty($_GET['s'])) {
        $s = 'viewCount';
      } else {
        $s = htmlentities($_GET['s']);
      }

      // 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 = "http://gdata.youtube.com/feeds/api/videos
      ?vq={$vq}&orderby={$s}&max-results={$i}&start-index={$o}";

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

      // get summary counts from opensearch: namespace
      $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
      $total = $counts->totalResults; 
      $startOffset = $counts->startIndex; 
      $endOffset = ($startOffset-1) + $counts->itemsPerPage;       

      // include Pager class
      require_once 'Pager/Pager.php';
      $params = array(
          'mode'       => 'Jumping',
          'perPage'    => $i,
          'delta'      => 5,
          'totalItems' => $total,
      );
      $pager = & Pager::factory($params);
      $links = $pager->getLinks();     
      ?>

      <h1>Search results</h1>
      <?php echo $total; ?> items found. 
      Showing items <?php echo $startOffset; ?> to 
      <?php echo $endOffset; ?>:
      <p/>

      <?php
      // print page links
      echo $links['all'];
      ?>

      <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 video thumbnail
        $attrs = $media->group->thumbnail[0]->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; 
        }

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

        // print record
        echo "<tr><td colspan=\"2\" class=\"line\"></td></tr>\n";
        echo "<tr>\n";
        echo "<td><a href=\"{$watch}\">
        <img src=\"$thumbnail\"/></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 "<a href=\"details.php?id=$id\">More information</a>
        </td>\n"; 
        echo "</tr>\n";
      }
    }
    ?>
    </table>
  </body>
</html>

我复制了上面的代码,并将其另存为wamp服务器根目录中的u.php

,但是单击搜索按钮后出现此错误

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8wTmIzdS5wbmcifQ==” alt =“在此处输入图像描述”>

Deprecated: Function ereg_replace() is deprecated in C:\wamp\www\u.php on line 60

Warning: simplexml_load_file(http://gdata.youtube.com/feeds/api/videos%0D%0A%20%20%20%20%20%20?vq=lol&amp;orderby=viewCount&amp;max-results=10&amp;start-index=1) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\wamp\www\u.php on line 91

Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity &quot;http://gdata.youtube.com/feeds/api/videos%0D%0A%20%20%20%20%20%20?vq=lol&amp;orderby=viewCount&amp;max-results=10&amp;start-index=1&quot; in C:\wamp\www\u.php on line 91

Fatal error: Call to a member function children() on a non-object in C:\wamp\www\u.php on line 94

您能告诉我如何在此脚本中纠正此错误,或者您可以建议任何类似的php脚本吗?simplehtmldom.php,可用于使用关键字和参数从youtube提取视频。

我目前正在尝试找到一个脚本,该脚本可用于使用关键字和参数从youtube获取详细信息。我在脚本中想要的是,php脚本应在视频中搜索...

php xml video youtube youtube-api
2个回答
1
投票

我没有确切的脚本,但是您可以使用the PHP examples of YouTube API创建它。


0
投票

请检查此链接我想这一定行得通Search Videos By Keyword玩得开心

© www.soinside.com 2019 - 2024. All rights reserved.