Using RSS Feeds and Mod_rewrite to add Classified Listings on your Website

Today we’re going to discuss how to put job listings on your site using RSS feeds. You could easily adapt this script to use any type of RSS feeds, but our target for this article is Craigslist.org.

Craigslist.org is an extremely popular classified ads site. With the RSS feeds that they make available to us, we’ll be able to create our own classified section that displays various types of ads from craigslist.org. We’re going to use RSS feeds and some mod_rewrite to create our site. Craigslist makes their ads available for cities all over the world, and we are going to make our listings available all over the world too.

Our first step is to create a file called “.htaccess.” This file contains our mod_rewrite rules:

  RewriteEngine on
  RewriteRule ^ads/(.*)/(.*)/ rssads.php?city=$1&cat=$2 [QSA,L]
  RewriteRule ^ads/(.*)/ rssads.php?city=$1 [QSA,L]
  RewriteRule ^ads/$ rssads.php [QSA,L]

What this means is that, whenever someone goes to your site and types in “yoursite.com/ads/”, it will take them to rssads.php.

Next, we want to create a file called “rssads.php.” This file will contain everything related to this script.

The first line of rssads.php is the base href. This is where you will put the URL of your site.

<base href=”http://mysite.com”>

For our mod_rewrite work, this is very important, because it tells the site where it is.

Next, we create an array containing our cities:

<?
  $cities = array(
    “US” => array(“atlanta”,”austin”,”baltimore”, “boston”,
      “chicago”,”cleveland”,”dallas”,”denver”,”detroit”,
      “honolulu”,”houston”, “las vegas”,”los angeles”,
      “miami”,”minneapolis”,”montreal”,”new orleans”,
      “new york”,”philadelphia”, “phoenix”,”pittsburgh”,
      “portland”,”providence”,”raleigh”,”sacramento”,
      “san diego”,”seattle”,”st louis” ),
    “Canada” => array(“calgary”,”edmonton”,”halifax”,
      “montreal”,”ottawa”,”quebec”,”saskatoon”,”toronto”,
      “vancouver”,”victoria”,”winnipeg”),
    “United Kingdom & Ireland” => array(“london”,
      “manchester”,”edinburgh”,”dublin”,”belfast”,
      “birmingham”,”cardiff”,”glasgow”),
    “Americas”=>array(“americas”,”buenos aires”,”caracas”,
      ”costa rica”,”lima”,”mexico city”,”rio de janeiro”,
      “santiago”,”sao paulo”,”tijuana”),
    “Europe”=>array(“amsterdam”,”athens”,”barcelona”,
      “berlin”,”brussels”,”copenhagen”,”florence”,
      “frankfurt”,”geneva”,”madrid”,”milan”,”moscow”,
      “munich”,”paris”,”prague”,”rome”,”stockholm”,
      “vienna”,”zurich”), 
    ”Australia & New Zealand” => array(“adelaide”,
      “auckland”,”brisbane”,”melbourne”,”perth”,”sydney”),
    “Asia” => array(“bangalore”,”bangkok”,”beijing”,
      “delhi”,”hong kong”,”istanbul”,”jerusalem”,”manila”,
      “mumbai”,”osaka”,”seoul”,”shanghai”,”singapore”,
      “tel aviv”,”tokyo”)
  );
?>

This array is set up so that we could easily add more to it. We use the format of country, followed by the city for display purposes, which you will see later in this article.

Next, we create an array containing our categories:

<?
  $categories = array(
    “Community” => array(
      “ccc”=>”All Community”,”act”=>”activities”,”laf”=>
      “lost & found”,”ats”=>”artists”,”muc”=>”musicians”,
      “kid”=>”childcare”,”vnn”=>”news & views”,
“com”=>
      “general”,”pol”=>”politics”,”grp”=>”groups”,
      “rid”=>”rideshare”,”pet”=>”pets”,”vol”=>
      “volunteers” ), “Personals”=>array( “stp”=>
      “strictly platonic”,”w4w”=>”women seek women”,
      “w4m”=>”women seeking men”,”m4w”=>
      “men seeking women”, “msr”=>”misc romance”,
      “mis”=>”missed connections”
    ),
    “Jobs” => array(
      “jjj”=>”All Jobs”,”acc”=>”Accounting / Finance”,
      “ofc”=>”Admin / Office”,”med”=>
      “Art / Media / Design”, “sci”=>”Biotech & Science”,
      “bus”=>”Business / Management”,”csr”=>
      “Customer Service”,”edu”=>”Education / Teaching”,
      “egr”=>”Engineering / Architecture”,”hum”=>
      “Human Resources”,”eng”=>”Internet Engineering”,
      “lgg”=>”Legal / Government”, “mar”=>
      “Marketing / PR / Advertising”,”hea”=>
      ”Medical / Healthcare”,”npo”=>”Non-profit sector”,
      “ret”=>”Retail / Food / Hosp”, “sls”=>
      “Sales / Biz Dev”,”trd”=>”Skilled Trades / Craft”,
      “sof”=>”Software/QA/DBA/etc”,”sad”=>
      “Systems / Networks”,”tch”=>”Technical Support”,
      “tfr”=>”TV / Film / Video”,”art”=>
      “Web / HTML / Info Design”,”wri”=>
      “Writing / Editing”, “etc”=>”[ETC]”
    ),
    “Gigs”=> array( “gggg”=>”All Gigs”,”cpg”=>”Computer”,
      “evg”=>”Event”,”crg”=>”Creative”,”lbg”=>”Labor”,
      “cwg”=>”Crew”,”wrg”=>”Writing”,”dmg”=>”Domestic”,
      “tlg”=>”Talent”,
    ),
    “Misc”=>array(
      “res”=>”Resumes”
    ),
    “Housing”=>array( “hhh”=>”All Housing”,”apa”=>
      “apts / housing”,”roo”=>”rooms / shared”,
      “sub”=>”sublets / temporary”,”hsw”=>”housing wanted”,
      “swp”=>”housing swap”,”vac”=>”vacation rentals”,
      “prk”=>”parking / storage”,”off”=>
      “office / commercial”, “rfs”=>”real estate for sale”
    ),
    “For Sale”=>array( “sss”=>”For Sale”,”bar”=>”Barter”,
      “bab”=>”Baby & Kids”,”bik”=>”Bikes”,”car”=>
      “Cars & Trucks”,”bks”=>”Books”, “clo”=>
      “clothes & acc.”,”zip”=>”Free”,”clt”=>”collectibles”,
      “fur”=>”furniture”,”sys”=>”computer”,”for”=>
      “general”,”ele”=>”electronics”,”spo”=>”sporting”,
      “gms”=>”garage sales”,”tix”=>”tickets”,”hsh”=>
      “household”,”wan”=>”wanted”, “mcy”=>”motorcycles”,
      “msg”=>”music instr”
    ),
    “Services”=>array( “bbb”=>”All Services”,”cps”=>
      “computer”,”aos”=>”automotive”,”crs”=>”creative”,
      “hss”=>”household”,”lbs”=>”labor/move”,”evs”=>
      “event”,”sks”=>”skill’d trade”,”fns”=>”financial”,
      “rts”=>”real estate”,”lgs”=>”legal”,”biz”=>
      “sm biz ads”, “lss”=>”lessons”,”ths”=>”therapeutic”
    )
  );
?>

As you can see, this array is also a multi array. It lets us group categories and add more if we want to.

There are also four functions used in rssads.php.

The first function is called “listCities.” This function runs through our cities array and displays a page of cities, grouped by country. This is used on the main page of our site:

<?
  function listCities($cities){
    echo “<table width=100%>”;
    foreach($cities as $country=>$list){
      echo “<tr><td colspan=3>”.$country.”</td></tr>”;
      $i = 0;
      foreach($list as $k=>$city){
        $i++;
        if($i == 1)echo “<tr>”; 
        echo “<td width=33%><a href=”ads/”.urlencode($city).”/”>”.$city.”</a></td>”;
        if($i == 3){
          echo “</tr>”;
          $i = 0;
        }
      }
    }
    echo “</table>”;
  }
?>

Our next function is called “listCats.” It works like listCities, and creates a list of categories:

<?
  function listCats($city,$categories){
    echo “<table width=100%>”;
    foreach($categories as $parent=>$list){
      echo “<tr><td colspan=3 align=center>”.
$parent.”</td></tr>”;
      $i = 0;
      foreach($list as $cat=>$name){
        $i++;
        if($i == 1)echo “<tr>”;
        echo “<td width=33%><a href=”ads/”.urlencode($city).”/”.urlencode($cat).”/”>”.$name.”</a></td>”;
        if($i == 3){
          echo “</tr>”;
          $i = 0;
        } 
      } 
    }
    echo “</table>”;
  }
?>

Our third function is called “display.” It processes the city and category and then calls our final function:

<?
  function display($city,$cat,$categories){
    $ocity = $city;
    $city = explode(“,”,$city);
    $city = $city[0];
    $city = str_replace(” “,”",$city);
    $url = “http://”.$city.”.craigslist.org/”.
$cat.”/index.rss”;
    foreach($categories as $parent=>$list){
      if( isset($list[$cat]) ){
        $tname = $parent.” – “.$list[$cat];
        break;
      }
    }
    $ptitle = “craigslist | Displaying “.$tname.” jobs in “.$ocity;
?>
        <a href=”<?=str_replace(“index.rss”,”",$url)?>”><?=$ptitle?></a><br/>
<?  
    echo readRss($url);
  }
?>

Our final function is the backbone of this site, “readRSS.” It is a universal RSS reader function that serves quite well for fetching RSS feeds from craiglist.org for us. Basically, it fetches the RSS feed that the script tells it to fetch and returns with information related to the feeds. In our case, it returns ads:

<?
  function readRss($url){
?>
    <table width=’100%’>
<? 
    $row = @implode( ”,@file($url) );
    if( preg_match_all(“‘<item[^>]*>.*?</item(s?)>’si”, $row, $rowitem ) ) {
      $item = $rowitem[0];
      $res = “”;
      $total = count($item) – 1;
      for( $i = 0; $i < $total; $i++ ) {
        if (trim($item[$i])!=” and !ereg(‘<items’,$item[$i])) {
          eregi(‘<title>(.*)</title>’, $item[$i], $title );
          eregi(‘<link>(.*)</link>’, $item[$i], $url );
          eregi(‘<description>(.*)</description>’, $item[$i], $desc );
          echo “<tr><td>”;
          echo “<a href=’”.str_replace(“&”,”&”,$url[1]).
               “‘>”.$title[1].”</a><span><br>”.
               “<div style=’padding-left:10px;’>”.
               htmlspecialchars( trim($desc[1]) ).
               “</div><br>”;
          echo “</td></tr>”;
        } 
      } 
    }
?>
    </table>
<?
  }
?>

Now, we just put it all together in rssads.php.

rssads.php:

<html>
<head>
  <base href=”http://mysite.com”>
  <title>My classifieds Site</title>
</head>
<body>
<?
  $cities = array(
    “US” => array(“atlanta”,”austin”,”baltimore”, “boston”,
      “chicago”,”cleveland”,”dallas”,”denver”,”detroit”,
      “honolulu”,”houston”, “las vegas”,”los angeles”,
      “miami”,”minneapolis”,”montreal”,”new orleans”,
      “new york”,”philadelphia”, “phoenix”,”pittsburgh”,
      “portland”,”providence”,”raleigh”,”sacramento”,
      “san diego”,”seattle”,”st louis” ),
    “Canada” => array(“calgary”,”edmonton”,”halifax”,
      “montreal”,”ottawa”,”quebec”,”saskatoon”,”toronto”,
      “vancouver”,”victoria”,”winnipeg”),
    “United Kingdom & Ireland” => array(“london”,
      “manchester”,”edinburgh”,”dublin”,”belfast”,
      “birmingham”,”cardiff”,”glasgow”),
    “Americas”=>array(“americas”,”buenos aires”,”caracas”,
      ”costa rica”,”lima”,”mexico city”,”rio de janeiro”,
      “santiago”,”sao paulo”,”tijuana”),
    “Europe”=>array(“amsterdam”,”athens”,”barcelona”,
      “berlin”,”brussels”,”copenhagen”,”florence”,
      “frankfurt”,”geneva”,”madrid”,”milan”,”moscow”,
      “munich”,”paris”,”prague”,”rome”,”stockholm”,
      “vienna”,”zurich”), 
    ”Australia & New Zealand” => array(“adelaide”,
      “auckland”,”brisbane”,”melbourne”,”perth”,”sydney”),
    “Asia” => array(“bangalore”,”bangkok”,”beijing”,
      “delhi”,”hong kong”,”istanbul”,”jerusalem”,”manila”,
      “mumbai”,”osaka”,”seoul”,”shanghai”,”singapore”,
      “tel aviv”,”tokyo”)
  ); 
  $categories = array(
    “Community” => array(
      “ccc”=>”All Community”,”act”=>”activities”,”laf”=>
      “lost & found”,”ats”=>”artists”,”muc”=>”musicians”,
      “kid”=>”childcare”,”vnn”=>”news & views”,“com”=>
      “general”,”pol”=>”politics”,”grp”=>”groups”,
      “rid”=>”rideshare”,”pet”=>”pets”,”vol”=>
      “volunteers” ), “Personals”=>array( “stp”=>
      “strictly platonic”,”w4w”=>”women seek women”,
      “w4m”=>”women seeking men”,”m4w”=>
      “men seeking women”, “msr”=>”misc romance”,
      “mis”=>”missed connections”
    ),
    “Jobs” => array(
      “jjj”=>”All Jobs”,”acc”=>”Accounting / Finance”,
      “ofc”=>”Admin / Office”,”med”=>
      “Art / Media / Design”, “sci”=>”Biotech & Science”,
      “bus”=>”Business / Management”,”csr”=>
      “Customer Service”,”edu”=>”Education / Teaching”,
      “egr”=>”Engineering / Architecture”,”hum”=>
      “Human Resources”,”eng”=>”Internet Engineering”,
      “lgg”=>”Legal / Government”, “mar”=>
      “Marketing / PR / Advertising”,”hea”=>
      ”Medical / Healthcare”,”npo”=>”Non-profit sector”,
      “ret”=>”Retail / Food / Hosp”, “sls”=>
      “Sales / Biz Dev”,”trd”=>”Skilled Trades / Craft”,
      “sof”=>”Software/QA/DBA/etc”,”sad”=>
      “Systems / Networks”,”tch”=>”Technical Support”,
      “tfr”=>”TV / Film / Video”,”art”=>
      “Web / HTML / Info Design”,”wri”=>
      “Writing / Editing”, “etc”=>”[ETC]”
    ),
    “Gigs”=> array( “gggg”=>”All Gigs”,”cpg”=>”Computer”,
      “evg”=>”Event”,”crg”=>”Creative”,”lbg”=>”Labor”,
      “cwg”=>”Crew”,”wrg”=>”Writing”,”dmg”=>”Domestic”,
      “tlg”=>”Talent”,
    ),
    “Misc”=>array(
      “res”=>”Resumes”
    ),
    “Housing”=>array( “hhh”=>”All Housing”,”apa”=>
      “apts / housing”,”roo”=>”rooms / shared”,
      “sub”=>”sublets / temporary”,”hsw”=>”housing wanted”,
      “swp”=>”housing swap”,”vac”=>”vacation rentals”,
      “prk”=>”parking / storage”,”off”=>
      “office / commercial”, “rfs”=>”real estate for sale”
    ),
    “For Sale”=>array( “sss”=>”For Sale”,”bar”=>”Barter”,
      “bab”=>”Baby & Kids”,”bik”=>”Bikes”,”car”=>
      “Cars & Trucks”,”bks”=>”Books”, “clo”=>
      “clothes & acc.”,”zip”=>”Free”,”clt”=>”collectibles”,
      “fur”=>”furniture”,”sys”=>”computer”,”for”=>
      “general”,”ele”=>”electronics”,”spo”=>”sporting”,
      “gms”=>”garage sales”,”tix”=>”tickets”,”hsh”=>
      “household”,”wan”=>”wanted”, “mcy”=>”motorcycles”,
      “msg”=>”music instr”
    ),
    “Services”=>array( “bbb”=>”All Services”,”cps”=>
      “computer”,”aos”=>”automotive”,”crs”=>”creative”,
      “hss”=>”household”,”lbs”=>”labor/move”,”evs”=>
      “event”,”sks”=>”skill’d trade”,”fns”=>”financial”,
      “rts”=>”real estate”,”lgs”=>”legal”,”biz”=>
      “sm biz ads”, “lss”=>”lessons”,”ths”=>”therapeutic”
    )
  ); 

  if( isset($_GET['cat']) && isset($_GET['city']) ){
    display($_GET['city'],$_GET['cat'],$categories);
  }else if( isset($_GET['city']) ){
    listCats($_GET['city'],$categories);
  }else{
    listCities($cities);
  }

  function listCities($cities){
    echo “<table width=100%>”;
    foreach($cities as $country=>$list){
      echo “<tr><td colspan=3>”.$country.”</td></tr>”;
      $i = 0;
      foreach($list as $k=>$city){
        $i++;
        if($i == 1)echo “<tr>”; 
        echo “<td width=33%><a href=”ads/”.urlencode($city).”/”>”.$city.”</a></td>”;
        if($i == 3){
          echo “</tr>”;
          $i = 0;
        }
      }
    }
    echo “</table>”;
  } 

  function listCats($city,$categories){
    echo “<table width=100%>”;
    foreach($categories as $parent=>$list){
      echo “<tr><td colspan=3 align=center>”.
$parent.”</td></tr>”;
      $i = 0;
      foreach($list as $cat=>$name){
        $i++;
        if($i == 1)echo “<tr>”;
        echo “<td width=33%><a href=”ads/”.urlencode($city).”/”.urlencode($cat).”/”>”.$name.”</a></td>”;
        if($i == 3){
          echo “</tr>”;
          $i = 0;
        } 
      } 
    }
    echo “</table>”;
  }

  function display($city,$cat,$categories){
    $ocity = $city;
    $city = explode(“,”,$city);
    $city = $city[0];
    $city = str_replace(” “,”",$city);
    $url = “http://”.$city.”.craigslist.org/”.
$cat.”/index.rss”;
    foreach($categories as $parent=>$list){
      if( isset($list[$cat]) ){
        $tname = $parent.” – “.$list[$cat];
        break;
      }
    }
    $ptitle = “craigslist | Displaying “.$tname.” jobs in “.$ocity;
?>
        <a href=”<?=str_replace(“index.rss”,”",$url)?>”><?=$ptitle?></a><br/>
<?  
    echo readRss($url);
  } 

  function readRss($url){
?>
    <table width=’100%’>
<? 
    $row = @implode( ”,@file($url) );
    if( preg_match_all(“‘<item[^>]*>.*?</item(s?)>’si”, $row, $rowitem ) ) {
      $item = $rowitem[0];
      $res = “”;
      $total = count($item) – 1;
      for( $i = 0; $i < $total; $i++ ) {
        if (trim($item[$i])!=” and !ereg(‘<items’,$item[$i])) {
          eregi(‘<title>(.*)</title>’, $item[$i], $title );
          eregi(‘<link>(.*)</link>’, $item[$i], $url );
          eregi(‘<description>(.*)</description>’, $item[$i], $desc );
          echo “<tr><td>”;
          echo “<a href=’”.str_replace(“&”,”&”,$url[1]).
               “‘>”.$title[1].”</a><span><br>”.
               “<div style=’padding-left:10px;’>”.
               htmlspecialchars( trim($desc[1]) ).
               “</div><br>”;
          echo “</td></tr>”;
        } 
      } 
    }
?>
    </table>
<?
  }
?>
</body>
</html>

A copy can also be found here:
http://www.freekrai.net/articles/rssad.txt

Google+ Comments

Google+ Comments