Lately I’ve been getting into rails, what can I say its a nice clean language. I also run a forum, its a forum written in php. Now I want to build apps for my php (vbulletin) forum, but I didn’t want to do cross site database calls. I wanted my plug ins to be as independent as possible from the main vbulletin app.. In another website I’ve been writing, there are three components that interconnect to each other using activeresource! Since it was so easy to implement I thought I’d give it a go cross programming language! Heres how I did it! First in PHP create your restfull program! I called mine rest.php :) I know very original! Using a sexy XML library I created for php. From this simply class, I can easily create from a select from a database my xml!

fetch_array($result);
        $xml->openTag("user");      

        foreach ($user as $tag => $value) {
            $xml->addfield($tag, $value);
        }
        $xml->closeTag();
    }
    echo $xml->build();
?>

So far so good! So if we go to file.php?user=1 we should get the user with id 1.. You could add all sorts of cool things here. But this is just an example to get you guys going. Now finally ensure you have mod_rewrite working in apache! now if your php file is called rest.php, you’ll need this rewrite rule to make the magic happen. Just add it to a .htaccess file, or in your vhost RewriteRule ^/rest/users/(.*) /rest.php?user=$1 In your rails app, now create a model named User, e.g. models/user.rb, and add

class User < ActiveResource::Base
  self.site = "http://domain.com/rest/"
end

and don’t forget your corresponding controller. This will now fetch all requests from our rest.php file. If you need any help, please drop me a line! Now for our rails app in our model if we have set up

class User < ActiveResource::Base
  self.site = "http://domain.com/rest/"
end

And we can simply do this in our rails app! You’ll see below the find method on the user model, and the resulting url it’ll call.

User.find(:all)      # http://domain.com/rest/users.xml
User.find(1)        # http://domain.com/rest/users/1.xml
User.find(100)    # http://domain.com/rest/users/100.xml

So if your doing an :all, the app will look for model.xml. If your doing any other call, it’ll look for model/?.xml. I hope this helps when building external active resource communicators.