2015-04-17 1 views
0

Mon YouTube API résultat de la recherche fetch script php. Affiche des erreurs:youtube api search.php foreach erreur de boucle

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

Il travaillait auparavant, mais maintenant sa ne fonctionne pas. J'ai changé le serveur et l'exécuter encore mais les résultats de recherche n'apparaissent pas.

Message d'erreur

Deprecated: Function ereg_replace() is deprecated in /home/u691059747/public_html/search.php on line 69 Warning: simplexml_load_file(): http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1 : parser error : Document is empty in /home/u691059747/public_html/search.php on line 92 Warning: simplexml_load_file(): in /home/u691059747/public_html/search.php on line 92 Warning: simplexml_load_file():^in /home/u691059747/public_html/search.php on line 92 Warning: simplexml_load_file(): http://gdata.youtube.com/feeds/api/videos?vq=dj&max-results=10&start-index=1:1 : parser error : Start tag expected, '<' not found in /home/u691059747/public_html/search.php on line 92 Warning: simplexml_load_file(): in /home/u691059747/public_html/search.php on line 92 Warning: simplexml_load_file():^in /home/u691059747/public_html/search.php on line 92

Résultats de la recherche:

Warning: Invalid argument supplied for foreach() in /home/u691059747/public_html/search.php on line 101

+0

contrôle sur ce http://stackoverflow.com/questions/3132844/function-ereg-replace-is-deprecated-how-to-clear-this-bug – ajaykumartak

+0

la prochaine fois, n'utilisez pas '.......' et '!!! 'dans votre question. –

+0

Ajouter un lien et essayer de résoudre les problèmes de grammaire et d'orthographe. – DaImTo

Répondre

0

noter simplement que YouTube a désactivé l'extraction des informations en utilisant leur API v2 et vous devez passer à la version 3.

Mais en tout cas, quand j'imprime la variable $feedURL dans votre search.php je reçois ie:

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

Parcourir cette URL donne sortie:

No longer available 

Donc, vous devriez d'abord essayer de comprendre pourquoi cette URL youtube ne fonctionne plus (sinon V2/3 chose alors peut-être les noms de paramètres a changé/etc ...)

Et puisque la chaîne est au-dessus juste un texte - pas une chaîne XML valide, vous obtenez l'erreur PHP:

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