2011-06-07 125 views
6

我正在嘗試在php中使用YQL以使用amazon.prodlist表和亞馬遜產品廣告API從亞馬遜獲取產品信息。如何使用YQL獲得更多的10個結果?

,我使用的查詢:

select * from amazon.prodlist where Title='harry potter' and SearchIndex='Books' and ResponseGroup='Images,ItemAttributes' 

它僅返回10個結果。我怎樣才能讓它在同一頁面上顯示更多的10個結果?另外,沒有分頁。

完整的PHP代碼:

<?php 
$BASE_URL = "https://query.yahooapis.com/v1/public/yql"; 

$key="my_key"; 
$secret="my_secret"; 
$title="harry potter"; 
$sindex="Books"; 
$rgroup="Images,ItemAttributes"; 
$events=""; 


// Form YQL query and build URI to YQL Web service 
$yql_query = "use 'http://www.datatables.org/amazon/amazon.ecs.xml' as amazon.prodlist; 
set AWSAccessKeyId='$key' on amazon.prodlist; 
set secret='$secret' on amazon.prodlist; 
select * from amazon.prodlist where Title='$title' and SearchIndex='$sindex' and ResponseGroup='$rgroup' "; 

$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json"; 

// Make call with cURL 
$session = curl_init($yql_query_url); 
curl_setopt($session, CURLOPT_RETURNTRANSFER,true); 
$json = curl_exec($session); 
// Convert JSON to PHP object 
$phpObj = json_decode($json); 

// Confirm that results were returned before parsing 
if(!is_null($phpObj->query->results)){ 
    // Parse results and extract data to display 
    foreach($phpObj->query->results->Item as $event){ 
    $events .= "<div><h2>" . $event->ItemAttributes->Title . " by " . $event->ItemAttributes->Author . "</h2></div>"; 
} 
} 
// No results were returned 
if(empty($events)){ 
    $events = "Sorry, no events matching result"; 
} 
// Display results and unset the global array $_GET 
echo $events; 
unset($_GET); 

?> 

這將顯示在頁面上10個結果。然而,當我在亞馬遜網站的「圖書」中搜索「哈利波特」時,我獲得了超過3k的結果。有什麼方法可以在一頁上獲得所有結果嗎?請指教。

+0

閱讀[文檔](http://developer.yahoo.com/yql/guide/paging.html)或許? – Wrikken 2011-06-07 18:35:58

+0

我已經閱讀並嘗試將查詢改爲: 'select * from amazon.prodlist(20)其中Title ='$ title'和SearchIndex ='$ sindex'和ResponseGroup ='$ rgroup'', 'select * from amazon.prodlist(0)其中Title ='$ title'和SearchIndex ='$ sindex'和ResponseGroup ='$ rgroup'', 'select * from amazon.prodlist(0)其中Title ='$ title'和SearchIndex ='$ sindex'和ResponseGroup ='$ rgroup'limit 20' 但是我仍然只得到10個結果。還有其他建議嗎? – 2011-06-07 18:51:50

+0

好吧,亞馬遜和YQL都沒有那麼多的經驗,所以我會把它留給其他人,下次你可能會提到那個小事實,但是,節省人們的時間:) – Wrikken 2011-06-07 18:53:57

回答

3

amazon.ecs打開數據表(在撰寫您的問題時)不支持分頁結果,因此您只能檢索10個項目。這是開放數據表作者的共同疏忽。

我已經修改了我自己的YQL表庫的分支中的數據表源,併發出了一個拉請求(here),希望能夠將這些更改重新導回到主源中。然後,您將能夠使用table.name([offset,]count)語法(docs)獲得更多(或更少!)的結果。

如果你想站起來,馬上跑步,那麼你需要將URL更改到數據表在我的變化在一個特殊的分支點只是這樣一個問題:

https://github.com/salathe/yql-tables/blob/so-6269923/amazon/amazon.ecs.xml

完整的查詢看起來像以下(非常類似於現有查詢):

use 'https://raw.github.com/salathe/yql-tables/so-6269923/amazon/amazon.ecs.xml' as amazon.prodlist; 
use AWSAccessKeyId='my_access_key' on amazon.prodlist; 
use secret='my_secret' on amazon.prodlist; 

select * 
from amazon.prodlist(50) 
where Title='harry potter' 
    and SearchIndex='Books' 
    and ResponseGroup='Images,ItemAttributes'; 

當(如果...請留意pull request)這些更改會被拉回到主YQL表庫中,您將可以返回使用http://www.datatables.org/amazon/amazon.ecs.xml URL。

+1

非常感謝!我在yql控制檯中嘗試了查詢,它工作順利!面對一些錯誤,當我在上面的PHP代碼中使用相同的查詢,但希望我能弄明白。榮譽! – 2011-06-07 23:19:23

+0

祝你好運,帕拉斯。 :) – salathe 2011-06-07 23:32:35

+1

好消息,這些變化被拉入主要的YQL表。你可以回到使用'http:// www.datatables.org/amazon/amazon.ecs.xml'。 – salathe 2011-06-11 19:00:42