0

嘿,我敢肯定这是一个简单的解决方法,但它让我发疯了。

我正在使用 youtube api,我正在尝试将用户生成的搜索词发布到 url 中,如下所示:

<form action="pagination.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>

<?

$search_term = $_POST['search_term'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds  
/api/videos?q='.$search_term.'&safeSearch=none&orderby=viewCount&v=2&alt=json&start- 
index=75&max-results=50');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch); 

$data = json_decode($output,true);

$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);



echo "<ul style='float:right'>";
foreach($video as $video) {
echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url'].'"><br><br>';
$title = $video['title']['$t'];
$video_id = $video['media$group']['yt$videoid']['$t'];
echo '<a href="search_4.php?video_get_id='.$video_id.'">'.$title.'</a>';
echo '<br>';

当我运行此代码时,没有任何反应,但是如果我像这样手动为 $search_term 分配一个值:

$search_term = 'baseball';

一切正常。

任何帮助将不胜感激!

4

1 回答 1

0

我发现了一些语法错误。以下代码适用于我。注意:文件名的更改,以便您可以测试此文件并更新您自己文件中的代码。也许是故意的,但在开始时它总是会使用“无条件”进行检索,因为代码已执行。

ENTER terms and press Submit button
<form action="ytcurltest1.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>

<?PHP
$search_term = $_POST['search_term'];
$startIndex = 1;
$maxResults = 25;
$ch = curl_init();
$url =  'http://gdata.youtube.com/feeds/api/videos?q='
     . $search_term
     . '&safeSearch=none&orderby=viewCount&v=2&alt=json'
     . '&start-index=' . $startIndex
     . '&max-results=' . $maxResults;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch); 

$data = json_decode($output,true);
if ( count($data) == 0 )
  {
    echo 'NO RESULTS FOUND. ENTER other terms';
    RETURN;
  }
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);

echo "<ul style='float:right'>";
foreach ($video as $video)
    {
     echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url']
          .'"><br><br>';
     $title = $video['title']['$t'];
     $video_id = $video['media$group']['yt$videoid']['$t'];
     echo '<a href="search_4.php?video_get_id='.$video_id.'">'.$title.'</a>';
     echo '<br>';
   }
?>
于 2013-05-28T06:43:42.867 回答