0

我的网页上有一个搜索功能,使用 youtube api 搜索音乐视频。稍后您可以在同一页面上观看它。但是,当我使用 api 并搜索其中包含空格的内容时,搜索功能将完全起作用。当我对字符串进行一些更改并用“/”替换所有空格时,例如 eminem lost yourself 变成: eminem/lose/yourself,当你搜索这个时,你不会得到正确的结果。这是我正在使用的代码的和平:这是我的代码:

<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<p>Search:
<input type="text" name="q" />
<input type="submit" name="submit" value="Search" style="margin-top:-30px"/>
</p>
</form>

然后在我的php方面我有:

if (!isset($_POST['q']) || empty($_POST['q'])) {
        die ('ERROR: Please enter one or more search keywords');
} 
else {  
        $q = $_POST['q'];
    $q = str_replace(' ','/',$q);
        //echo $q;

} $feedURL = "http://gdata.youtube.com/feeds/api/videos/-/{$q}?orderby=viewCount&max-results={$i}"; $sxml = simplexml_load_file($feedURL); // 从 opensearch 获取汇总计数: namespace $counts = $sxml-> children('http://a9.com/-/spec/opensearchrss/1.0/'); $total = $counts->totalResults; $startOffset = $counts->startIndex; $endOffset = ($startOffset-1) + $counts->itemsPerPage;

我尝试了不同的方法来使搜索功能正常工作。我使用了这个 $q = ereg_replace('[[:space:]]+', '/', trim($q)),它不起作用,然后我开始用“+”替换字符串,它不起作用再次。我想看看我怎样才能有一个可以很好地处理空格的搜索功能。

4

1 回答 1

0

尝试像这样格式化您的查询

https://gdata.youtube.com/feeds/api/videos?
q=football+-soccer
&orderby=published
&start-index=11
&max-results=10
&v=2

API 的版本很重要 v=2 是最新的稳定版本

于 2013-01-07T14:07:19.783 回答