dtncv04228 2013-03-20 04:40
浏览 52

Yahoo Boss API分页

I am getting this json output from yahoo boss api. It goes on for 50 results, but, I have pasted only the first two...

    (
        [bossresponse] => stdClass Object
            (
                [responsecode] => 200
                [web] => stdClass Object
            (
                [start] => 0
                [count] => 50
                [totalresults] => 2750000
                [results] => Array
                    (
                        [0] => stdClass Object
                            (
                                [date] => 
                                [clickurl] => http://www.apple.com/ipad/
                                [url] => http://www.apple.com/ipad/
                                [dispurl] =&gt; www.apple.com/<b>ipad</b>
                                [title] =&gt; Apple - <b>iPad</b>
                                [abstract] =&gt; <b>iPad</b> is a magical window where nothing comes between you and what you love. And it comes in two sizes.
                            )

                        [1] =&gt; stdClass Object
                            (
                                [date] =&gt; 
                                [clickurl] =&gt; http://en.wikipedia.org/wiki/IPad
                                [url] =&gt; http://en.wikipedia.org/wiki/IPad
                                [dispurl] =&gt; en.wikipedia.org/wiki/<b>IPad</b>
                                [title] =&gt; <b>iPad</b> - Wikipedia, the free encyclopedia
                                [abstract] =&gt; The <b>iPad</b> is a line of tablet computers designed and marketed by Apple Inc., which runs Apple's iOS operating system. The first <b>iPad</b> was released on April 3, 2010; the ...
                            )

                        [2] =&gt; stdClass Object
                            (
                                [date] =&gt; 
                                [clickurl] =&gt; http://www.amazon.com/s?ie=UTF8&amp;page=1&amp;rh=i%3Aaps%2Ck%3Aipad
                                [url] =&gt; http://www.amazon.com/s?ie=UTF8&amp;page=1&amp;rh=i%3Aaps%2Ck%3Aipad
                                [dispurl] =&gt; www.amazon.com/s?ie=UTF8&amp;page=1&amp;rh=i%3Aaps%2Ck%3A<b>ipad</b>
                                [title] =&gt; Amazon.com: <b>ipad</b>
                                [abstract] =&gt; Considering an <b>iPad</b>? Compare it to Kindle Fire HD Check out our easy side-by-side comparison chart to see how the newest <b>iPad</b> stacks up to Kindle Fire HD 8.9".
                            )

I use the following code in php to connect to the api and display the results...

      **//connect to yahoo api and get results in json**
      <?php
      require("OAuth.php");

    $cc_key  = "**confidential**";
    $cc_secret = "**confidential**";
    $url = "http://yboss.yahooapis.com/ysearch/web";
    $args = array();
    $args["q"] = "yahoo";
    $args["format"] = "json";

    $consumer = new OAuthConsumer($cc_key, $cc_secret);
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
    $ch = curl_init();
    $headers = array($request->to_header());
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $rsp = curl_exec($ch);
    $results = json_decode($rsp);
    ?>
    **// Present results in html/php**
    <div id="resultsdiv">
    <?php
     foreach($results->bossresponse->web->results as $result) 
      {
echo '<h3><a href='.$result->url.'>'.$result->title.'</br>'.$result->abstract.'</a></h3>';
}
      ?>

My question is how do I paginate results, since all the 50 results appear on the first web page only. I want to display ten results in every page.

Any help is appreciated.

Thanks.

  • 写回答

1条回答 默认 最新

  • dsf6565 2013-03-28 18:37
    关注

    According to the Yahoo! BOSS documentation, specifically the Universal Arguments section, it looks like you can use the start and count parameters to adjust the pagination of results. It looks like the default return count is 50 which matches what you observed.

    In your code example you can adjust by adding:

    $args["start"] = 0; // increment as needed
    $args["count"] = 10;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值