2013-01-07 55 views
0

我在我的網頁上使用YouTube YouTube api搜索音樂視頻的搜索功能。稍後,您可以在同一頁面上觀看。然而,當我使用api時,以及當我搜索其中有空格的東西時,搜索功能完全可以工作。當我對字符串進行一些更改並用「/」替換所有空格時,例如,eminem迷失自己變成:eminem/lose/yourself,並且當你搜索到這個時,你不會得到正確的結果。這是我使用的代碼和平: 這是我的代碼:YOUTUBE API搜索功能無法正常工作

<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的摘要計數:命名空間 $ 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)),但沒有起作用,然後我開始用「+」替換字符串,再次。 我想看看如何能有一個與空間一起工作的搜索功能。

+0

有你試圖將一個'urlencode'適用於'$ q'像你應該_ ? URL中通常不允許使用空格,它們應該編碼爲'%20'。當然允許斜線。 – Halcyon

+0

另外,假設一個圖書館 - 被數百萬人使用 - 「不起作用」是非常冒失的 - 你總是應該假設你做錯了什麼。 – Halcyon

+0

我確實使用過urlencode,但它不起作用。 –

回答

0

嘗試格式化您的查詢像這樣

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

API的版本是非常重要的V = 2是最新的穩定版本