4

It's possible make a bridge from Java to php file?

I've got an application written in Java and I need execute http://piwik.org/ that is written in PHP. In the server I have PHP running but I cannot access from the browser to the php directory because all incoming traffic is redirected by apache to glassfish Application server.

So my idea is to use Java servlet to execute php files with:

Runtime.getRuntime().exec("php /path/to/file/file.php");

Then write the PHP output as java servlet response.

The only problems to accomplish this are:

How can I execute PHP cli that act like a browser?

Which parameters I need to pass to PHP to allow PHP to read or write cookie and session?

Andrea Catania
  • 1,361
  • 3
  • 21
  • 37
  • If you run the PHP process in CGI mode and se the expected environment variables, PHP should react as though it was executing in say mod_php. You can reference http://php.net/manual/en/install.unix.commandline.php for details. – Mark Jan 20 '16 at 17:50

3 Answers3

4

When you execute a php script from the command line the GET/POST/SESSION/COOKIE variables are meaningless. When your file.php send a cookie there is no browser to receive it, save it and use it for subsequent requests.

What you can do is use CGI SAPI, so that all HTTP_* variables will be usable, and the headers will be written to the output.

The php-cgi binary implements the CGI interface, which allows you to pass cookies on the command line like this:

HTTP_COOKIE='PHPSESSID=XXXX' php-cgi /path/to/file/file.php

Where XXXX can be the session id of an user. You can read the cookie analyzing the headers on the output.

Federkun
  • 36,084
  • 8
  • 78
  • 90
  • I tried to do that but I receive a warning that alert me I cannot use cookie because the browser not support it. I think i need to set a parameter that allow it. – Andrea Catania Jan 21 '16 at 09:20
  • Cannot run program "HTTP_COOKIE="test=1"": error=2, No such file or directory Now I receive this error only when I execute through Java, using console I receive the output. – Andrea Catania Jan 21 '16 at 09:48
  • can you try to provide the absolute path of php-cgi? something like `HTTP_COOKIE='PHPSESSID=XXXX' /usr/bin/php-cgi /path/to/file/file.php` – Federkun Jan 21 '16 at 09:59
  • Yes now I will try. I need to understand first what this do HTTP_COOKIE='PHPSESSID=XXXX' because the Java try to parse as script. probably i need do that in other way with Java. – Andrea Catania Jan 21 '16 at 10:04
  • it's an environment variable; maybe with `export HTTP_COOKIE='PHPSESSID=XXXX'`? – Federkun Jan 21 '16 at 10:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101270/discussion-between-andrea-catania-and-federico). – Andrea Catania Jan 21 '16 at 10:11
4

If you're using Apache anyway to proxy the traffic, I'd just exclude all traffic to Piwik and serve those directly from the file system / mod_php / php-fpm / whatever you normally use.

You could also use php-cgi and pass the appropriate environment variables, but that complicates a lot of stuff, like you'd have to proxy the response back to the browser, too. Apache has already support for this, so don't implement another proxy in your application, do it directly in Apache.

You can exclude a directory from being proxied:

ProxyPass /piwik ! 
ProxyPass / 127.0.0.1:8080 
ProxyPassReverse / 127.0.0.1:8080
kelunik
  • 6,750
  • 2
  • 41
  • 70
2

Thanks to the idea of kelunik to use Apache to exclude the traffic and the help By Federico I've resolved the problem using this Apache rule:

ProxyPass /phpdir ! 
ProxyPass / 127.0.0.1:8080 
ProxyPassReverse / 127.0.0.1:8080

Apache redirect all request to port 8080, except the folder /phpdir. Into the Apache document root I've created the directory phpdir that is the new root where the PHP applications will be run.

That's it

Andrea Catania
  • 1,361
  • 3
  • 21
  • 37
  • That's the syntax I've been looking for. Edited it into my answer as it's way nicer than with rewrites. – kelunik Jan 22 '16 at 12:09