An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

ColdFusion 8 running PHP and Ruby!

June 2, 2007 ·

Update: now supports Ruby as well as PHP! I'll post a couple of Ruby examples next. We're always getting grief from those PHP folks, aren't we? What if you could run PHP code directly inside ColdFusion?
<cfset who = "Sean" />
echo "Hello ".$_COLDFUSION["who"]."<br />";
$_COLDFUSION["greeting"] = "wibble";
<cfoutput>greeting = #greeting#</cfoutput>
This outputs Hello Sean and greeting = wibble. Yes, you can pass ColdFusion variables into PHP and set variables in PHP that you can access back in ColdFusion land. [Note: Unlike the first version I posted, the new version takes no attributes but instead makes available the entire variables scope from the calling page - and now uses $_COLDFUSION which is more in keeping with PHP.] How is this possible? Well, ColdFusion 8 uses Java 6 which provides access to the javax.script package and all the J-language implementations available. Quercus is a Java implementation of PHP and it is implemented as a script engine. Sun has provided a number of JSR-223 ScriptEngine and ScriptEngineFactory implementations and there are Java implementations of a number of languages. Combine that with a simple custom tag and the code you see above becomes a trivial extension for ColdFusion 8! Credit to axelclk for the missing piece of the puzzle!
Update: you can download what you need from my 'software' pod on my blog: Scripting for ColdFusion 8. Installation instructions are given inside each script directory's example.cfm page. The project now has Ruby support. The source code is available on Google Code - along with my other Open Source projects and I'm setting up a central location on RIAForge as well (as soon as Ray approves my project request).

Tags: coldfusion · oss

54 responses

  • 1 Rupesh Kumar // Jun 2, 2007 at 2:11 AM

    Rock n Roll !! This is awesome ! That opens up so many possibilities..
  • 2 Mike Huntington // Jun 2, 2007 at 2:11 AM

    Wow this is great!!! Thanks for sharing!
  • 3 Peter Tilbrook // Jun 2, 2007 at 2:19 AM

    That is pretty amazing! And did not take you long to knock it up either!

    I have heaps of PHP books but never bothered to really read them as CF has suited me well for the past nearly 12 years :)

  • 4 Dean Harmon // Jun 2, 2007 at 4:56 AM

    That's pretty cool Sean... Really cool.
  • 5 christopher Cachor // Jun 2, 2007 at 6:18 AM

    Wow, very slick. I'd love to work with CF more for client projects but licensing seems to turn people off. Let's hope the new license helps with this.
  • 6 John Farrar // Jun 2, 2007 at 6:53 AM

    So... real world. Here are my questions.

    1. What does this give us? (What features does PHP have that CF is missing or would be enhanced by having?)

    2. Does it just share the variable scope?

    3. What are the chances of getting a shared hosting company to set this up?

    4. If it is scripting, then it doesn't compile and cache, right? Wouldn't that be a performance hit? (Not to mention in order to get PHP to scale the creators have a paid for version of the free PHP, which isn't likely to work with this.)

    NOTE: I think this is pretty cool. I just don't know the value of it in the real world yet. Much our company goal though is reaching beyond the enterprise so if anyone has serious answers to these questions let's hear them.
  • 7 Raymond Camden // Jun 2, 2007 at 6:57 AM

    Hey, you should post this to RIAForge. :)
  • 8 Mark Drew // Jun 2, 2007 at 7:41 AM

    *THIS* is why its good to see you out of Adobe, you big brained fella you!

  • 9 iongion // Jun 2, 2007 at 8:02 AM

    Well thats a tremendous stuff dude !!!! :)

    Now serious, this is great stuff!

    Thanks for sharing!

    We, the new cf developers seem to ignore the java coupling of cf.

    Now i am going to bash my .net friends also (cause they where always, look now vbscript, now jscript, no c#(C grill, not C sharp :P)
  • 10 TJ Downes // Jun 2, 2007 at 8:15 AM

    ACK! My eyes!

    Seriously though, pretty cool Sean. Have you done any performance testing? Also, could you potentially wrap an entire PHP application in the tags ?
  • 11 Jake Munson // Jun 2, 2007 at 9:04 AM

    So, like we've been saying &quot;CF is Java!&quot; to Java developers, we can now say &quot;CF is PHP!&quot; to PHP heads. ;)
  • 12 iongion // Jun 2, 2007 at 9:28 AM

    Jake, we can also say &quot;CF is Python&quot; to the .NET heads who are so proud of their python CLR implementations. :D
  • 13 Sean Corfield // Jun 2, 2007 at 9:32 AM

    @John, good questions:

    1. Imagine you have some PHP code you want to reuse in CF without rewriting it (see below for more possibilities)...

    2. I have session scope shared in my latest p.o.c. (in PHP you say $_SESSION[&quot;varname&quot;] and it's the same as session.varname in ColdFusion). I'm working on getting URL, form and CGI scope shared (trickier because of how the engine is wired in). PHP has no application scope.

    3. If I switch over to Mark Mandel's Javaloader then you could use this on shared hosting I think.

    4. The Quercus engine compiles the PHP to bytecode but I haven't figured out how to enable the necessary caching to make it only compile it once (yet!). Recommend you read the Quercus site for more details.

    @Ray, yes, it was about after midnight when I first got it working and 2am by the time I gave up on URL / form scope integration so I wasn't awake enough to put it on RIAForge. It will end up there in due course.

    @TJ, no performance testing yet - see my comment to John above. If I can get the rest of the integration working, you could potentially wrap an entire application, yes.

    @All, the mechanism used to do this is fairly generic. Any language implemented as a javax.script engine can be used here. The next languages on my &quot;hit list&quot; to make work like this are: Ruby, Python, Groovy and Haskell. They all have javax.script implementations - see - I intend to wrap them all as custom tags for ColdFusion.
  • 14 Pedro Claudio // Jun 2, 2007 at 5:03 PM

    Sean, uses
       &lt;cfset factory = thisTag.getClass().forName(&quot;com.caucho.quercus.script.QuercusScriptEngineFactory&quot;).newInstance() /&gt;
       &lt;cfset engine = factory.getScriptEngine() /&gt;
       &lt;cfset engine.getContext().setWriter( thisTag.getClass().forName(&quot;;).newInstance() ) /&gt;

    so that serving with shared lodging, and createobject and cfobject blocked JAVA, he does not have problem.
  • 15 Sean Corfield // Jun 2, 2007 at 5:34 PM

    @Pedro, nice tip - thanx! Of course, on shared hosting you probably would not be allowed to install the scripting libraries in the first place...
  • 16 Pedro Claudio // Jun 2, 2007 at 6:42 PM

    Yes, it can be. But the server can have libs and also not allow createobject/cfobject JAVA.
  • 17 Dan Russell // Jun 4, 2007 at 6:43 AM

    You're missing an underscore in one of your $_COLDFUSION vars.
  • 18 Sean Corfield // Jun 4, 2007 at 6:59 AM

    @Dan, thanx - fixed!
  • 19 Josh Giese // Dec 17, 2007 at 1:23 PM

    This is awesome. I have been wanting to port my site to cf for some time, but am tied into interacting with a php based bulletin board system. Im going to try this and get back you you guys with the results. Thanks for putting this out there Sean!
  • 20 Sean Corfield // Dec 17, 2007 at 1:51 PM

    @Josh, be warned that much of the context required for PHP includes and so on is not yet active in the scripting bridge I built. Simple sites run but I don't know how far you'll get with a complex PHP app!
  • 21 CarlosBsAs // Feb 15, 2008 at 5:49 AM

    Do you know if somebody invented a similar tag for ColdFusion 7 MX? some way to run PHP.
  • 22 Jeff Pierson // Jul 1, 2008 at 9:38 AM

    Anyone know how to get this working on a shared server?

    I am getting this error:
    Object Instantiation Exception.
    An exception occurred when instantiating a Java object. The class must not be an interface or an abstract class.
  • 23 Sean Corfield // Jul 1, 2008 at 9:47 AM

    @Jeff, given that it requires the addition of Java libraries into the core CF installation, I doubt any shared host will let you run this.
  • 24 JC // Jul 14, 2008 at 3:42 PM

    Hmm... now, if you can get it working to the point where you can run WordPress and phpBB through ColdFusion. No offense to the CF developers who've written blogging and forum tools for CF, but I don't think there's anything CF out there that competes with those. I've been kicking around the idea of installing PHP on our intranet servers because converting the whole site over to wordpress would be so much cheaper (right now the end users use Contribute) and so much easier to maintain.

    But even as just a way to run some php code snippets inline, this is great. I don't know Java very well, so some of the more advanced text manipulation has always been a pain for me, where in PHP almost every kind of text manipulation you can imagine is already built in and easy to use.
  • 25 TJ Downes // Jul 14, 2008 at 6:25 PM

    Hey JC, try out MangoBlog, It's not quite as mature as WordPress yet, but it's a really nice system
  • 26 Sean Corfield // Jul 14, 2008 at 7:30 PM

    @JC, and seems to be a good port of phpBB...
  • 27 Simon Nicol // Sep 6, 2008 at 5:35 AM

    This is sweet. Now I can write an app for Facebook, host the site myself on my CF8 server and run code for FB written in PHP! Sweet...

    Good move ma' boy many thanks!
  • 28 Karl DeBisschop // Nov 4, 2008 at 10:27 AM

    Sean - one of your comments says you do not yet support includes. Any progress on that front?
  • 29 Sean Corfield // Nov 4, 2008 at 10:47 AM

    @Karl, I haven't had any need to work on the project but if folks want to enhance it, I can work with them on folding changes back into the core project.
  • 30 sutor // Dec 30, 2008 at 11:23 AM


    Great post. Helped me setup and execute php using the ScriptEngine/javax.scripting interface.

    However, PHP files that include other PHP files using include/require are not resolved. I keep getting &quot;not a valid path&quot;. Anyone else run into the same issue?

  • 31 Sean Corfield // Dec 30, 2008 at 3:48 PM

    @sutor, yup, the context to resolve includes is not passed into the script engine. I haven't had enough time - or need - to resolve it yet. You're welcome to dig into the Java and CFML code to help (and if you figure out a fix, send me a patch and I'll add it in).
  • 32 Saul // Mar 1, 2009 at 6:26 PM

    I get an error trying to run this test script

    Echo &quot;Hello, World!&quot;;

    The error I get is ...

    Variable APPLICATION is undefined.

    The error occurred in C:\ColdFusion8\CustomTags\php.cfm: line 30

    28 :
    29 :    &lt;!--- create Quercus engine and script cache: ---&gt;
    30 :    &lt;cfif not structKeyExists(application,&quot;__scripting&quot;) or
    31 :          not structKeyExists(application.__scripting,&quot;php&quot;) or
    32 :          not structKeyExists(application.__scripting.php,&quot;cache&quot;)

    I think I installed correctly as per the sourceforge instructions. Any clues appreciated
  • 33 Sean Corfield // Mar 1, 2009 at 6:41 PM

    @Saul, sounds like you don't have Application.cfc or &lt;cfapplication&gt; to activate the CFML application.
  • 34 Saul // Mar 2, 2009 at 5:29 AM


    I didn't see anything in the documentation about needing a specific application.cfc or particular code in an application.cfc , did I miss something?

    If it's just &quot;any old&quot; application.cfc in order to have an application scope I'll slot one in tonight and retry.

    Thanks for your reply.
  • 35 Sean Corfield // Mar 2, 2009 at 8:11 AM

    @Saul, you need to enable APPLICATION scope which means you need either a &lt;cfapplication&gt; tag (usually in Application.cfm) or an Application.cfc to set an application name. That's not in the Scripting docs since it's basic CFML stuff so I'd just assumed every CFer could figure it out. I guess the docs could say &quot;Requires APPLICATION scope.&quot;?
  • 36 Saul // Mar 2, 2009 at 12:22 PM

    @Sean, yes that worked great with &quot;any old&quot; application.cfc, many thanks, now to figure out more about PHP!
  • 37 jon // Mar 28, 2009 at 9:55 PM

    I'm running joomla and I my application requires that I can populate a PDF contract. I'm using the cf_php tags to receive the user input from php and then CF populates the contract.

    the problem is that i can't pass variables from outside the cf_php to the inside...

    I can't even grab the POST variable from inside the cf_php tag using php... This is a great idea, but completely worthless if it doesn't allow me to cross integration functionality....
  • 38 Sean Corfield // Mar 29, 2009 at 11:40 AM

    @Jon, did you read the documentation on the wiki?

    Also, this is an open source project - please feel free to enhance it and submit patches back to me.

    I don't do PHP so I've no personal need to enhance it myself right now. If I find myself doing more PHP, I probably will improve the integration.
  • 39 Alvin // Aug 3, 2009 at 11:43 PM

    may i ask how would this codes look like after converting to php?

    &lt;cfproperty name=&quot;projectID&quot; displayName=&quot;Project ID&quot; type=&quot;numeric&quot; default=&quot;0&quot; required=&quot;yes&quot;&gt;

    &lt;cfscript&gt;variables.lockName = CreateUUID();&lt;/cfscript&gt;

    &lt;cffunction name=&quot;init&quot; access=&quot;public&quot; returntype=&quot;ModelDb&quot; output=&quot;no&quot; &gt;

    &lt;cflock name=&quot;#variables.lockName#&quot; throwontimeout=&quot;yes&quot; timeout=&quot;10&quot; type=&quot;exclusive&quot;&gt;

    Great thanks in advance
  • 40 nagesh // Aug 6, 2009 at 7:37 AM

    Hi All,
    I have used this tool and it works great... I had checked the example.cfm and used PHP code within CFM and accessed the CF session in PHP and CF variables too...It worked smoothly..

    My problem is:
    Within the example.cfm, I had set a Coldfusion session called &quot;uid&quot; i.e. &lt;cfset session.uid = &quot;12345&quot; /&gt;

    I have another file test.php, and within that I want to access this session.uid and I tried to access it using $_SESSION[&quot;uid&quot;] via PHP code, but it is not giving me the value.....

    Please help me in resolving this problem.. Is it possible to access Coldfusion sessions(set in CFM) in PHP file...

    I can access this session within the CFM files...But I want to access it in .PHP file...

    Eagerly waiting for the response...

  • 41 Sean Corfield // Aug 6, 2009 at 8:05 AM

    @Nagesh, the project is open source for a reason - if folks need enhancements, they can add them and then let me know to incorporate it into a new version.
  • 42 Angel // Sep 17, 2009 at 10:46 AM


    I was trying to run php in CF and I found this blog, it was bery helpfully but I have some problems with a function wich calls a method named openssl_get_privatekey.

    In php I just have to uncommnet the import tag where php_openssl.ddl is called in php.ini file, and after that everything works, but in CF can not do this. Any idea how to call the method openssl_get_privatekey?


  • 43 Riccardo Cecinati // Feb 8, 2010 at 2:15 AM

    I hope the blog is still alive, and I ask for some help.
    I am trying to have running altogether these pieces of software: Openbluedragon (1.2), sitting on Apache Tomcat 5.5, Java Virtual Machine Sun 1.6.0, in order to have Asterisk server 1.4 teamed with some CF app of mmy own. Now, it appears that I also need PHP libraries to be called from within my app... too much? maybe.
    I have found this opening, and still hope it's viable. Installed quercus.jar and resin-util.jar in my Tomcat lib dir , installed php.cfm custom tag and pointed thru cfimport, and run the example template delivered with the tag. But..
    &quot;java.lang.NullPointerException&quot; in &lt;cfset code.eval() /&gt; (line 89).
    Should I switch to Coldfusion (9, nowadays) instead of Openbluedragon?
    Thank you for helping,


  • 44 Rajeev // Jun 15, 2010 at 8:33 AM


    Thanks for the info. I already have PHP code that runs in the background for web scraping. It does not run in the browser. And I have a requirement to read pdf documents from this PHP code.

    I came to know that ColdFusion9 offers cfpdf tags that allow us to extract text from the pdf document using extracttext. They also said I can use it as a web service. But the pdf.cfc?wsdl does not have the extracttext function and has only extractpages. So is it possible to somehow call extracttext of ColdFusion9 from my PHP code?

  • 45 Sean Corfield // Jun 15, 2010 at 3:01 PM

    @Rajeev, I only use CF9 for testing portability of code since I develop on Railo. I've no idea what remote facilities CF9 provides for PDF manipulation.
  • 46 Chester Regen // Nov 18, 2010 at 9:21 AM


    I set this up on CF9 with no problem, but when I try it on Railo 3.1.2, I get a Com.caucho.quercus.quercusexitexception:

    "'railo.runtime.type.scope.CallerImpl' class definition not found"

    This looks like a problem in the Railo build, no?

  • 47 Sean Corfield // Nov 22, 2010 at 3:01 PM

    @Chester, no idea. I haven't touched the project for years and never tested on Railo. I suspect you have a classpath setup problem tho' - not a problem with Railo.
  • 48 cvh // Dec 7, 2010 at 9:51 AM


    Got it working and this is great.. I have one question though..

    How does one pass an array or an object from within the php code back to the coldfusion page?

    It passes strings back and forth perfectly but what about complex data types?

    Thanks -
  • 49 Sean Corfield // Dec 8, 2010 at 5:30 PM

    @Chad, no idea. As I said to Chester, I haven't needed to touch the project for years - the source is out there for folks to enhance and I'd be happy for someone to talk the project and run with it!
  • 50 cvh // Dec 10, 2010 at 10:03 AM

    Thanks for the response Sean. I will check it out.
  • 51 dmitry // Aug 6, 2012 at 7:41 AM

    PHP image resize produces sharper images than coldfusion "imageResize". Can cf_php be used to reduce image size?
  • 52 Sean Corfield // Aug 10, 2012 at 7:18 PM

    @Dmitry, cf_php uses the Quercus PHP engine which is Java so it's not using the same library as native PHP. You'll need to try various Java image manipulation libraries from CFML to see if you can improve the image quality.
  • 53 manuel // Nov 22, 2013 at 10:25 PM

    Do u know why fail the next line:


    I want to use the library tcpdf.

  • 54 Sean Corfield // Nov 22, 2013 at 10:30 PM

    @manuel, as I said to you in response to your direct email: I haven't touched the project in years - it's open source, figure out the bug and fix it yourself.