嘿,我知道这是一个简单的修复,但它让我发疯。
我正在使用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';
一切都很完美。
任何帮助都将非常感激!
我发现一些语法错误。下面的代码为我工作。注意:更改文件名,以便您可以测试此文件并在自己的文件中更新代码。也许是故意的,但在开始的时候,它总是会做一个"没有条款"的检索,因为代码是执行的。
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>';
}
?>