Topic: PHP integration

Is there any way to include a PHP script in rails?  Like I make a PHP program that outputs "Yo, World!" and use something like @PHP = openfile "yo.php"

Re: PHP integration

If you want to use PHP, make your own framework in PHP. If you want to use Rails, use Ruby.

Answer to your question: No.

Re: PHP integration

sure you can.

You can run command line arguments from inside of ruby, and get the response:

  #yo.php
    echo "Yo, World!"

  #test.rb
    php_output = `php yo.php` #note the backticks
    puts php_output.downcase
   # output => yo, world!

Last edited by pjleonhardt (2007-10-29 23:27:59)

Re: PHP integration

I was attempting to make the point that Rails and PHP should not used together. You should use one or the other.

Re: PHP integration

Although it might be bad practice, it is possible, which is what he asked. I can see the usefulness. Say you've got a complicated application with some cron jobs running to do data integration, file conversions, whatever. If all you're asked to do is to make a new web interface for the application, you can still use the functionality of all those prewritten scripts, even if they happen to be written in php, perl, or what have you. We ran into something similar at work, where we're rewritting an application that had perl cron jobs and a PHP web frontend with a filemaker backend. We were lucky enough that the client allowed us to redo everything with ruby and mysql, but not everyone is so lucky...

Re: PHP integration

Radar wrote:

I was attempting to make the point that Rails and PHP should not used together. You should use one or the other.

+1 for what pjleonhardt said.

It's healthy to push for a single language and framework for an application, but its equally important to not rewrite working and tested code if you don't have to. The #1 reason that my team chose RoR for our current work is that it offers easy interop with other languages and platforms because it sticks to standards. RoR is targeted at web development where one language per project is the norm, but if you start using RoR for it's benefits in bigger projects (like middleware) you'll quickly learn how to play nicely with other languages.

To get a bit back on topic, another option for the OP's question would be to access a php script/application via SOAP or REST. It may be slightly faster to hit apache/mod_php instead of firing up the php interpreter from the command line.

Re: PHP integration

You can call a PHP and parse the response. I've used this method several times where integration with a PHP was required.

Hpricot works wonderfully for reading the returned data.

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: PHP integration

codeking wrote:

Is there any way to include a PHP script in rails?  Like I make a PHP program that outputs "Yo, World!" and use something like @PHP = openfile "yo.php"

This isn't hard.
As long as the php script stands alone so you can access it at say .... http://example.com/myphp_script.php (Make sure it outputs only the text you want included)

Then in Ruby/Rails all you need to do is

require 'net/http'
@the_url = URI.parse('http://example.com/myphp_script.php)
#do not use @url ... it will give you problems if you use it in the view
@response = Net::HTTP.get_response(@the_url)
render :text => @response.body and return false
#or go on to use it in your view as you like

Last edited by ikeo (2008-08-20 00:17:59)

Austin Web Developer
Web Application Development with Ruby on Rails, PHP (w CakePHP or CodeIgniter), ASP.NET (w MVC)

Re: PHP integration

I was very surprised when I looked at my webpage for the first time which I had written in RoR and found that it was grabbing data from a completely different app written in PHP. The data it was grabbing was from a txt file associated with the PHP app which writes a gedcom file from data entered into a webpage. The two apps (the two different webpages) are in different folders, the one in the RailsApps folder and the other in the xampp folder. But the xampp wasn't running, the WEBrick server was running. Why was I seeing my gedcom file on my RoR website when the RoR app has no way of writing a gecom file? It must have been stealing the data from my gedcom.txt file. I didn't do anything to ask it to do that. By the way, this was my first RoR app.