在API Indeed.com xml上进行分页,每个页面提供25个结果,如何分页


Pagination on the API Indeed.com xml feed 25 results per page, How to Paginate?

我已经在我的网站上设置了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身体

    <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>

这就是它的工作原理。用户到达web页面,然后页面加载基于用户位置的作业结果。如果找到的邮政编码结果少于25个,则没有问题,不需要分页。

但是如果xml提要有超过25个结果,它将显示25个,仅此而已。如果我想显示其余部分,我必须分页。这就是我需要帮助的地方。

他们的API url是这样工作的

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将是显示25个结果的第0页,&start=25&limit=25将是显示接下来25个结果的第1页,&start=50&limit=25将是显示剩余25个结果的第2页。这个例子是基于xml提要中是否总共有75个结果。

在上面的// Indeed.com API URL parameters中,我将其设置为从第0页开始,限制为25页。他们不允许超过25个。如果设置得更高,则默认为25。

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

我需要一些帮助来实现使用我当前的PHP代码上面的分页方式。我如何添加到我的PHP代码?

如果xml中有25个结果,则has_more函数返回true

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