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"
You are not logged in. Please login or register.
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"
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.
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)
I was attempting to make the point that Rails and PHP should not used together. You should use one or the other.
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...
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.
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.
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)