在API Indeed.com XML提要每页25个结果分页,如何分页?(Pagination on

2019-10-20 09:04发布

我已经建立了我的网站上Indeed.com XML饲料。 他们的API只允许每个查询25分的结果。 我怎样才能分页的结果,如果有大于25?

我还没有找到一个令人满意的或不够彻底回答具竞争力的超低。 我搜索了这个星期。

以下是我在我的代码:

PHP:

        // Indeed.com API URL parameters
    $url = 'http://api.indeed.com/ads/apisearch'.'?';
    $publisher = 'xxxxxxxxxxxxxxxx';
    $q = $query;

    $location = '';
    if (isset($_POST['location'])) {
        $location = $_POST['location'];
    } else {
        $geo = geoCheckIP($_SERVER['REMOTE_ADDR']);
        if (isset($geo) && ($geo != "not found, not found")) {
            $location = $geo;
        }
    }
    $sort = 'date';
    $radius = '20';
    $st = '';
    $jt = '';
    $start = '0';
    $limit = '25';
    $fromage = '';
    $highlight = '0';
    $filter = '1';
    $latlong = '0';
    $co = 'us';
    $chnl = '';
    $userip = $_SERVER['REMOTE_ADDR'];
    $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? ($_SERVER['HTTP_USER_AGENT']) : 'unknown';
    $v = '2';
    $xml = simplexml_load_file($url."publisher=".$publisher."&q=".$q."&l=".$location."&sort=".$sort."&radius=".$radius."&st=".$st."&jt=".$jt."&start=".$start."&limit=".$limit."&fromage=".$fromage."&highlight=".$highlight."&filter=".$filter."&latlong=".$latlong."&co=".$co."&chnl=".$chnl."&userip=".$userip."&useragent=".$useragent."&v=".$v);

HTML BODY

    <div class="paradiv">
      <h1><?php echo $xml->totalresults . " " . $jobroll_title . " Near " . $location ?></h1>
      <!-- BEGIN INDEED ORDERED LIST-->
      <ol class="jobs">
        <?php

    foreach($xml->results->result as $result) { ?>
        <li class="job <?php echo (++$liBgColor%2 ? 'odd' : 'even'); ?>">
          <div class="title_wrapper">
            <div id="jobtitle"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank"><?php echo $result->jobtitle;?></a></strong></div>
            <div id="company"><?php echo $result->company;?></div>
          </div>
          <div id="snippet">
            <?php $result->snippet = str_replace("  ", ". ", $result->snippet); echo $result->snippet;?>
          </div>
          <div id="location"><strong>Location:</strong> <?php echo $result->formattedLocationFull;?></div>
          <div id="date"><span class="posted <?php echo (++$locationBgColor%2 ? 'even' : 'odd'); ?>">Posted <?php echo $result->formattedRelativeTime;?></span></div>
          <div id="details-2"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank">Details</a></strong></div>
        </li>
        <?php } ?>
      </ol>
      <!-- END INDEED ORDERED LIST -->

      <!-- THIS IS WHERE THE PAGINATION WILL DISPLAY -->
      <div class="pagenumber"><?php echo "Page Number " . "<a href=\"" . (rtrim(dirname($_SERVER['PHP_SELF']), '/')) . "\">" . $xml->pageNumber . "</a>" ?></div>
    </div>

这是如何工作的。 用户到达网页上,然后在页面加载与基于用户位置的作业结果。 如果低于25个结果发现他们的邮政编码那么就没有问题,不需要分页。

但是,如果XML源有超过25分的结果,它会显示25,仅此而已。 如果我想显示的休息,我必须进行分页。 这就是我需要帮助。

这里是他们的API网址是如何工作的。

http://api.indeed.com/ads/apisearch?publisher=xxxxxxxxxxxxxxx&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=0&limit=25&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2

该说的部分&start=0&limit=25是如何基于XML的页面数量来显示结果。

因此,例如: &start=0&limit=25将页0表示25分的结果, &start=25&limit=25将页面1示出了下一个25个结果和&start=50&limit=25将第2页显示剩余25个结果。 如果存在总共75个结果在XML饲料是本实施例中是基于。

在我// Indeed.com API URL parameters上面我已经将它设置为0页,并限制在开始25他们不允许超过25的限制。 如果设置较高,则默认为25。

    $start = '0';
    $limit = '25';

我需要在实施方式使用上述我当前的PHP代码进行分页一些帮助。 我怎样才能添加到我在我的PHP代码?

Answer 1:

has_more如果有25个结果中的XML函数返回true

$start = 0;
do {
  $xml = simplexml_load_file(...$start...);
  // process $xml
  $start += 25;
} while(has_more($xml));


文章来源: Pagination on the API Indeed.com xml feed 25 results per page, How to Paginate?