<cfset who = "Sean" />
<cf_php>
<?php
echo "Hello ".$_COLDFUSION["who"]."<br />";
$_COLDFUSION["greeting"] = "wibble";
?>
</cf_php>
<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!
<cf_php>
<?php
echo "Hello ".$_COLDFUSION["who"]."<br />";
$_COLDFUSION["greeting"] = "wibble";
?>
</cf_php>
<cfoutput>greeting = #greeting#</cfoutput>
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).

50 responses so far ↓
1 Rupesh Kumar // Jun 2, 2007 at 2:11 AM
2 Mike Huntington // Jun 2, 2007 at 2:11 AM
3 Peter Tilbrook // Jun 2, 2007 at 2:19 AM
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
5 christopher Cachor // Jun 2, 2007 at 6:18 AM
6 John Farrar // Jun 2, 2007 at 6:53 AM
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
8 Mark Drew // Jun 2, 2007 at 7:41 AM
MD
9 iongion // Jun 2, 2007 at 8:02 AM
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
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
12 iongion // Jun 2, 2007 at 9:28 AM
13 Sean Corfield // Jun 2, 2007 at 9:32 AM
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["varname"] 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 "hit list" to make work like this are: Ruby, Python, Groovy and Haskell. They all have javax.script implementations - see http://scripting.dev.java.net - I intend to wrap them all as custom tags for ColdFusion.
14 Pedro Claudio // Jun 2, 2007 at 5:03 PM
<cfset factory = thisTag.getClass().forName("com.caucho.quercus.script.QuercusScriptEngineFactory").newInstance() />
<cfset engine = factory.getScriptEngine() />
<cfset engine.getContext().setWriter( thisTag.getClass().forName("java.io.StringWriter").newInstance() ) />
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
16 Pedro Claudio // Jun 2, 2007 at 6:42 PM
http://pcsilva.blogspot.com/2007/06/coldfusion-8-servindo-paginas-php-sim.html
17 Dan Russell // Jun 4, 2007 at 6:43 AM
18 Sean Corfield // Jun 4, 2007 at 6:59 AM
19 Josh Giese // Dec 17, 2007 at 1:23 PM
20 Sean Corfield // Dec 17, 2007 at 1:51 PM
21 CarlosBsAs // Feb 15, 2008 at 5:49 AM
22 Jeff Pierson // Jul 1, 2008 at 9:38 AM
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
24 JC // Jul 14, 2008 at 3:42 PM
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
26 Sean Corfield // Jul 14, 2008 at 7:30 PM
27 Simon Nicol // Sep 6, 2008 at 5:35 AM
Good move ma' boy many thanks!
28 Karl DeBisschop // Nov 4, 2008 at 10:27 AM
29 Sean Corfield // Nov 4, 2008 at 10:47 AM
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 "not a valid path". Anyone else run into the same issue?
Thanks.
31 Sean Corfield // Dec 30, 2008 at 3:48 PM
32 Saul // Mar 1, 2009 at 6:26 PM
<html>
<head>
</head>
<body>
<cf_php>
<?php
Echo "Hello, World!";
?>
</cf_php>
</body>
</html>
The error I get is ...
Variable APPLICATION is undefined.
The error occurred in C:\ColdFusion8\CustomTags\php.cfm: line 30
28 :
29 : <!--- create Quercus engine and script cache: --->
30 : <cfif not structKeyExists(application,"__scripting") or
31 : not structKeyExists(application.__scripting,"php") or
32 : not structKeyExists(application.__scripting.php,"cache")
I think I installed correctly as per the sourceforge instructions. Any clues appreciated
33 Sean Corfield // Mar 1, 2009 at 6:41 PM
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 "any old" 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
36 Saul // Mar 2, 2009 at 12:22 PM
37 jon // Mar 28, 2009 at 9:55 PM
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
http://scripting.riaforge.org/wiki/index.cfm/UsingPHPInColdFusion
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
<cfproperty name="projectID" displayName="Project ID" type="numeric" default="0" required="yes">
<cfscript>variables.lockName = CreateUUID();</cfscript>
<cffunction name="init" access="public" returntype="ModelDb" output="no" >
<cflock name="#variables.lockName#" throwontimeout="yes" timeout="10" type="exclusive">
Great thanks in advance
40 nagesh // Aug 6, 2009 at 7:37 AM
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 "uid" i.e. <cfset session.uid = "12345" />
I have another file test.php, and within that I want to access this session.uid and I tried to access it using $_SESSION["uid"] 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...
Thanks
Nagesh.
41 Sean Corfield // Aug 6, 2009 at 8:05 AM
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?
Thanks.
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..
"java.lang.NullPointerException" in <cfset code.eval() /> (line 89).
Should I switch to Coldfusion (9, nowadays) instead of Openbluedragon?
Thank you for helping,
Riccardo
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?
Thanks,
Rajeev
45 Sean Corfield // Jun 15, 2010 at 3:01 PM
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?
Thanks,
Chester
47 Sean Corfield // Nov 22, 2010 at 3:01 PM
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 -
Chad
49 Sean Corfield // Dec 8, 2010 at 5:30 PM
50 cvh // Dec 10, 2010 at 10:03 AM
Leave a Comment