PHP and Java EE Backend - php

Can I use Struts as a backend and PHP as front end for a web application? If yes, what may be the implications.

The first thing to came to mind is Quercus (from the makers of the Resin servlet engine), as Jordi mentioned. It is a Java implementation of the PHP runtime and purportedly allows you to access Java objects directly from your PHP (part of me says "yay, at last").
On the other hand, while I have been itching to try a project this way, I would probably keep the separation between Java EE and PHP unless there was a real reason to integrate on the code-level.
Instead, why don't you try an SOA approach, where your PHP "front-end" calls into the Struts application over a defined REST or SOAP API (strong vote for REST here) over HTTP.
http://mydomain.com/rest/this-is-a-method-call?parameter1=foo
You can use Struts to build your entire "backend" model, dealing only with business logic and data, and completely ignoring presentation. As you expose the API with these URLs, and you are basically building a REST API (which may come in handy later if you ever need to provide greater access to your backend, perhaps by other client apps).
Your PHP application can be built separately (and rather thinly), calling into the REST API (perhaps using Curl) as if it would call into a database or some native PHP class library.
Anyway, that's what I'd do. But, if you do use Quercus, please post how it went.

I don't know much about Java, but I remember running into Quercus a while ago. It's a 100% Java interpreter for PHP code.
So yes, you could have PHP templates on your Java app. Update: see Quercus: PHP in Java for more info.

What do you mean by backend and and frontend?
If you mean using Java for the admin side of your site and PHP for the part that the public will see then there is nothing stopping you.
The implications are that you will have to maintain two applications in different languages.

I think what you mean is you want to use PHP as your templating language and structs as your middleware (actions etc).
I would imaging the answer would be no, not without some kind of bridge between the structs session and the PHP.
If you say change x to 3 in java in a structs action, you couldn't just go <?php echo x ?> or whatever to get the value out, you would need to transfer that information back and forth somehow.
Submitting would be OK though, I would imagine.
Not recommended though.

Related

Pagelets in PHP

Is there a way to implement the "pagelets" concept using PHP as Facebook does? From their blog post:
BigPipe is a fundamental redesign of the dynamic web page serving system. The general idea is to decompose web pages into small chunks called pagelets, and pipeline them through several execution stages inside web servers and browsers. This is similar to the pipelining performed by most modern microprocessors: multiple instructions are pipelined through different execution units of the processor to achieve the best performance. Although BigPipe is a fundamental redesign of the existing web serving process, it does not require changing existing web browsers or servers; it is implemented entirely in PHP and JavaScript.
Pagelets in Facebook home page; each rectangle corresponds to one pagelet:
Is there a way to implement pagelets concept using PHP
No. It barely has something to do strictly with the server-side (PHP).
That is done in Javascript on the client (browser).
It appears that FB is referring to what is called the widget problem in MVC design. So this certainly appears backend, but the question is extremely open ended and impossible to answer.
I'd say go checkout some HMVC frameworks that allow you to do this.
The concept of pagelets is totally unrelated to PHP, however as you can do as well 3D graphics in PHP, there sure is a way to implement such a concept in PHP.
While you're at it, I would also suggest to implement the user-agent in PHP as well, so you can develop a cross-client-server-platform-pagelet implementation in PHP totally.
You might be looking for so called EDGE Side Includes for which you can find a PHP implementation in the Symfony2 framework. This also needs a server component, like Varnish or Squid.

Interpreting JavaScript in PHP

I'd like to be able to run JavaScript and get the results with PHP and is wondering if there is a library for PHP that allows me to parse it out. My first thought was to use node.js, but since node.js has access to sockets, files and things I think I'd prefer to avoid that.
Rationale: I'm doing screen scraping in PHP and have encountered many scenarios where the data is being produced by JavaScript on the frontend, and I would like to avoid writing specialized filtering functions to act on the JavaScript on a per-case basis since that takes a lot of time. The more general case would be to parse the JavaScript directly.
Downvoting: I don't really see what's so controversial about this question, modern web crawlers are known to do it, the only difference is that they tend to not be written in PHP. [1]
[1] http://blogs.forbes.com/velocity/2010/06/25/google-isnt-just-reading-your-links-its-now-running-your-code/
It's an interesting question and the down-voters are being unimaginative about potential use-cases. Page archiving tools, printing scripts, preview images - all valid reasons to want to manipulate a document with the JavaScript included within the page.
I'm not aware of any existing PHP implementations, but you could probably adapt Mozilla's SpiderMonkey as a PHP module, or as a standalone tool to manipulate a DOMDocument and return the result.
I haven't had experience with server-side JavaScript, but some issues that I believe might need to be dealt with:
Host objects like document and window are not part of the ECMAScript specification (these are objects provided by the implementing browser) so you need to make sure that the library provides equivalent host objects.
You might have security issues around executing client side scripts within a server side environment. This is a lot like allowing the user to submit a PHP script to be evaluation, so you need to make sure the security sandbox is tight.
Another (perhaps) safer and easier to implement option might be to use a modified FireFox or WebKit instance that runs as a browser, loading up the target pages and returning the modified source to your application.
From PHP 5.3 you can use V8JS extention from PHP. It's a native library that uses the new Google V8 Javascript engine to execute JS and return the result.
It's good because you can pass vars in PHP arrays and are interpreted very well
NodeJS (or some other derivative of google's v8) might actually be the best way to go here. If you're concerned about the various things nodejs can do (eg. sockets, etc), you can probably "strip it down" by removing modules and/or addons -- I think even the built in stuff is ultimately implemented in such a way that it could be stripped out fairly easily.
An alternate approach might be to simply replace, override, or remove the require function from node.js.
There's also envjs which should make it easier to run js that was designed to run the browser.

Should I use PHP or PHP + Java EE?

I'm going to start a new project with instant message support.
I find that there is no good long polling solution in PHP, but there is some good ones in Java EE.
I'm wondering if I can integrate PHP and Java EE to get the function? Or should I just use Java EE instead of PHP?
Keeping the number of development platforms to a minimum is always a good idea. It will keep deployment requirements low, gives less operational complexity and gives the best possible integration.
If you want to replace PHP by Java, you have lots of frameworks you can use to replace PHP. A good option would be the Stripes framework it’s an easy to use MVC framework that does not need much configuration.
An other very workable solution would be using Java in the backend for filling the database. And use PHP in the frond-end and use the database to retrieve data. This way the integration is limited to a shared database.
If you would like to have a direct integration between PHP and Java, things start to get more complex. It could be implemented by either web services or the faster php-java-bridge. But I don’t think you need this and if you do, I would seriously consider migrating the whole project to Java.
For an implementation example of the php-java-bridge see: How to share session between Java and PHP
You CAN build your application with PHP together with Java EE. The PHP is to generate HTML, and Java EE supports Comet.
Actually, Facebook generates HTML with Apache server. I bet Facebook web pages are written in PHP. And the Comet server of chatting is written in ErLang hosted in Mochiweb. Two languages & servers works fine together.
The decision is up to your own preference.

PHP complex uses - Interact with external devices

Is it possible to access & control external devices in PHP?
For example, would it be capable of changing the speed of a USB fan or change the direction of a wireless toy car.
I'm trying to go beyond web dev and I'm wondering if the programming language I use is capable of handling my ideas or should I consider changing the environment.
If these would be possible, I'd very much appreciate any pointers to reading materials or suggestions on other languages that might be more suitable.
Thanks!
On Linux it surely is possible by accessing /dev/ files. But it'll be very tedious. I'd recommend you switching to Python, Ruby, Lua or Java.
For example there are bindings for libusb for Python, Ruby, Lua and Java.
You could write an external program, then use PHP's exec (or was it system?) function to interact with the executable or script.
Seems like the most sane way to do it. Another good alternative is to build a program or script that controls an external device that can communicate with a RESTfull type API exposed via HTTP - and then use lib_curl in PHP land to talk back and forth between it. Believe me, building a basic HTTP server in C++ that can be used to be remote controlled with PHP (or JS for that matter) is very simple.
Wait
I think I read the question wrong ;)
If you want to get into really cool stuff, I say that you learn C++. C++ is a great language that not only opens a lot of doors, but also provides a good learning experience. C++ is lots and lots of fun.
In response to comment
In the case with USB its a bit different and more complicated (as USB has an established protocol and such) but serial is as easy as dumping data into a handle.
You should be able to pick up C++ to get to that point fairly soon. Either way it's a great experience.
This would be possible with a extension, but not with pure PHP code. I don't know of any extensions being able to do something like this, but I think it should be possible.
If you find a command line tool that does this, then you can control it using exec(), system(), passthru() or shell_exec() (based on what output the program gives you back)
Just be sure to escapeshellcmd() if you give access to this program from a public website.

Flex with PHP

I'm new to Flex and done a few application with only client side programming. Now I'm trying to do the back-end stuff using PHP. Can some one explain where(which folder) to put the PHP file for the Flex file to communicate with PHP. I'm using Flexbuilder 3 IDE.
Thanks
Flex will not access your PHP script via the filesystem, you have to invoke your PHP script via a web server. The easiest solution is to use XML as in your data transport language. But you can get fancy and use something like BlazeDS
This all really depends on what you want. I would make a very different recommendation if you wanted to simply poll the server occasionally than if you wanted to have a regular interface between the two.
If you are simply polling the server, I would recommend simply using the HTTPRequest class. It will allow you to create POST and GET data and serve to communicate relatively well.
On the other hand, if you are looking to have a more complicated set of communications between the server and the SWF, your best bet is the RemoteObject class with either AMFPHP, WebOrb, or an equivalent (we use WebOrb where I work). This has the benefit of allowing you to have objects of similar name/type in both Flex and PHP, meaning that communication can be made a good deal more comprehensive.
(If you are working this way, email me cwATallen-poole.com and I can give some pointers so that you do not have insane compiler arguments for Flex).
The next option is to use the XMLSocket class, but that seems to be a bit more intense than what you are looking for.
You may want to take a look at AMFPHP too, pretty handy.
Try XAMPP.Very useful.
The new Flash Builder 4 (product formerly known as FlexBuilder) supports connecting to PHP services out of the box. The use the Zend Framework to do it, and will install the framework to your test webserver for you whenever you go to setup a data service.
Adobe video tutorials:
Part 1
Part 2
Another good, non-video tutorial

Categories