Skip to content


Paging eBay RSS results

Here is a an extremely simple way of returning a RSS result from eBay and splitting it up into pages. Some people don’t want to show 100 results, at a time, but also not be limited to show 10 results, etc. If this is the case, then paging is the key.

I’ll break my examples into pieces, and at the end offer the completed script so you can see it all in one piece.

First we need to create some function that will need to be used to retrieve our RSS file and parse the elements from it. There are a ton of ways to do this, and this was hobbled together from various scripts throughout the year (so sorry I can’t give credit on the some of the functions). We’ll be using cURL to sent the request and return the CML and use various callback functions to parse the XML file. I primarily work in PHP4, and what I understand this process is further simplified in PHP5. I believe that these functions will still work in 5, but please let me know if they don’t.. I need an excuse to jump to PHP5 anyway.

<?php
function startElement($parser, $name, $attrs){
        global $xml_level, $xml_curr_path, $tree, $tree_levels, $grouping, $tag_data, $tag;
        $xml_level++;
        $xml_curr_path .= ‘/’.$name;
        if ($grouping){
                $tag            = true;
        }
        if ($xml_curr_path == $tree && $xml_level == $tree_levels){
                $grouping       = true;
                $tag_data       = "";
        }
}

function endElement($parser, $name){
        global $xml_level, $xml_curr_path, $tree, $tree_levels, $grouping, $tag_data, $xml_array, $callback_func, $stop_parsing, $tag;
        $xml_level–;
        $xml_curr_path  = substr($xml_curr_path,0,strlen($xml_curr_path)strlen($name)-1);
        $tag                    = false;
        if ($xml_curr_path == $tree && $xml_level == $tree_levels && $grouping == true){
                $xml_array[$name] = $tag_data;
                $tag            = false;
        }
        if ($xml_level == $tree_levels1 && $grouping == true && !$stop_parsing){
                $stop_parsing   = call_user_func($callback_func, $xml_array);
                $grouping               = false;
                $tag                    = false;
                $xml_array              = array();
        }
        $tag_data                       = "";
}

function characterData($parser, $data){
        global $grouping, $tag_data, $tag;
        if ($grouping == true && $tag == true){
                $tag_data .= $data;
        }
}

function process_record($record){
        global $count, $resultArray, $cfg;
        $count                          = 0;
        if(is_array($resultArray) && sizeof($resultArray) > 0){
                $count                  = sizeof($resultArray);
        }
        $resultArray[$count][‘desc’]    = $record[‘DESCRIPTION’];
        $resultArray[$count][‘link’]    = $record[‘LINK’];
        $resultArray[$count][‘title’]   = $record[‘TITLE’];
}

function convert_xml($rest, $callback, $format){
        global $xml_array, $tree, $tree_levels, $grouping, $tag_data, $callback_func, $stop_parsing;
        $xml_array              = array();
        $tree                   = ‘/’.$format;
        $tree_levels    = sizeof(explode("/",$tree))-1;
        $grouping               = false;
        $tag                    = false;
        $tag_data               = "";
        $callback_func  = $callback;
        $stop_parsing   = false;
        $xml_parser     = xml_parser_create();
        xml_set_element_handler($xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($xml_parser, "characterData");
        $ch                     = curl_init();
        $timeout                = 5; // set to zero for no timeout
        curl_setopt ($ch, CURLOPT_URL, $rest);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents  = @curl_exec($ch);
        curl_close($ch);
        xml_parse($xml_parser, $file_contents)
         or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
        xml_parser_free($xml_parser);
}

$rss                                            = ‘http://rss.api.ebay.com/ws/rssapi?FeedName=SearchResults&siteId=0&language=en-US&output=RSS20&from=R34&satitle=star+wars&sacat=246′;
$count                                          = 0;
$itemsPerPage                           = 10;
$format                                         = "RSS/CHANNEL/ITEM";
convert_xml($rss, "process_record", $format);
?>
 

Here we’re passing the URL to a Star Wars RSS search result, this will sent via cURL and a XML/RSS file will be returned. Once we get to the process_record(), we’re taking the parsed values and sticking them into an associative array. This is important since we’ll be paging through the array. RSS results will return a maximum of 100 results, so we’ll be looking at them in chunks.

<?php
if(is_array($resultArray) && sizeof($resultArray) > 0){
        $count                                  = sizeof($resultArray);
        $pages                                  = ceil($count / $itemsPerPage);
        if(!isset($_GET[‘page’])){
                $CurrentPage    = 1;
                $start                  = 0;
                $end                    = $itemsPerPage;
        }else{
                $CurrentPage    = (is_numeric($_GET[‘page’]) ? $_GET[‘page’] : 1);
                if($CurrentPage > $pages){
                        $CurrentPage = 1;
                }
        }
        $start                          = ($CurrentPage1) * ($itemsPerPage);
        $end                            = $start + $itemsPerPage + 1 ;
        if($end > $count){
                $end                    = $count1;
        }
       
        for($i = $start; $i < $end; $i++){
                echo "<strong><a href=\"".$resultArray[$i][‘link’]."\">".$resultArray[$i][‘title’]."</a></strong><br>";
                echo $resultArray[$i][‘desc’]."<br><br>";
        }
        echo "Page: ";
        for ($x = 1; $x <= $pages; $x++){
                if($CurrentPage == $x){
                        echo "<strong>".$x."</strong> ";
                }else{
                        echo "<a href=\"index.php?page=".$x."\">".$x."</a> ";
                }
        }
}else{
        echo "no auctions found";
}
?>
 

Next we check to see if a array was returned and that it has elements in it. If it doesn’t we don’t have any results. If we do, we next calculate how many pages there are. We divide the auctions returned with the number per page we want to display. The next section is to see if we have any paging information being passed to use through the querystring. If there is a page passed, we need to verify that it’s inbound of our pages (does not exceed our maximum page). Once we have this we can calculate start and end points of the loop.

Finally we loop through the array using our start and end we set above. This will change as we move through the pages. And to finish it off, we need our page links at the bottom. Simply doing a for loop from 1 to the end of pages will do the trick.

As I said, this is fairly simple and can be adjusted to fit your needs. This example does not add your rover code, etc. Also you could do some fun stuff with preg_match instead of using the functions above to help parse even further. That’s another topic altogether. I like to pull images and item IDs out now for further customizations.

You can download the code here, just change the name to index.php to see the example work.

Incoming search terms:

  • rss paging
  • paging rss results


Related posts:

  1. Dynamic XML/RSS Feed using PopShops
  2. eBay RSS querystring/variable definitions
  3. Using eBay’s API to search auctions and return results
  4. EPN change to RSS
  5. Monitor your sites – Watch your logs

Posted in EPN, Programming.

Tagged with , , , , , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Derby web design says

    Just wanted to say thanks for some great code. Oh and it does work on php5 too :)

  2. admin says

    Hello Derby

    This does work in PHP5. No problems.
    hanji



Some HTML is OK

or, reply to this post via trackback.

CommentLuv badge