librelist archives

« back to archive

Discussing A Full Sever API for PHP

Discussing A Full Sever API for PHP

From:
Devon Weller
Date:
2010-08-23 @ 20:24
Hi.

What would be the best way of adding some better server API support to a 
PHP application running behind a mongrel2 server?  A full server API in 
PHP has handy features like automatic population of $_GET, $_POST, 
$_COOKIE and $_SERVER variables.

Is this even something that makes sense when using mongrel2?  Should I 
just route dynamic page requests to nginx via a proxy and let it connect 
to PHP via FastCGI?  Or is there a better way?

mongrel2 and PHP work well together for passing json messages back and 
forth via ZeroMQ.  But many of the great PHP libraries and frameworks will
not work under this paradigm.  They expect a server API to populate those 
magic variables.


If anyone on this list has thought about this, I'd love to hear your thoughts.

Thanks,
- Devon

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Colin Curtin
Date:
2010-08-23 @ 20:51
You should be able to use http://github.com/winks/m2php, and if it
doesn't populate those variables, you could certainly contribute to it
to make sure it does. :)

Colin

On Mon, Aug 23, 2010 at 1:24 PM, Devon Weller <dweller@devonweller.com> wrote:
> Hi.
>
> What would be the best way of adding some better server API support to a
PHP application running behind a mongrel2 server?  A full server API in 
PHP has handy features like automatic population of $_GET, $_POST, 
$_COOKIE and $_SERVER variables.
>
> Is this even something that makes sense when using mongrel2?  Should I 
just route dynamic page requests to nginx via a proxy and let it connect 
to PHP via FastCGI?  Or is there a better way?
>
> mongrel2 and PHP work well together for passing json messages back and 
forth via ZeroMQ.  But many of the great PHP libraries and frameworks will
not work under this paradigm.  They expect a server API to populate those 
magic variables.
>
>
> If anyone on this list has thought about this, I'd love to hear your thoughts.
>
> Thanks,
> - Devon
>
>



-- 
=begin
Colin Curtin
Engineer, Cramer Development
http://cramerdev.com
email:  colin@cramerdev.com
skype: colin.t.curtin
phone: +1.805.694.UNIX (8649)
=end

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Devon Weller
Date:
2010-08-23 @ 21:59
Thanks Colin.

http://github.com/winks/m2php does work great for sending and receiving JSON.  

But it has only very basic support for the HTTP headers that it receives.
There is, for example, no support for sessions or query parameters.


One option is certainly to write PHP code that populates the super globals
like $_SERVER, $_GET and $_POST.  But I think one would run into trouble 
with the session handling.


For performance and other reasons, I think it would make a lot more sense 
to write a true server API in C.  

Does anyone have a sense of how hard it would be to modify the fastcgi 
SAPI code to create a mongrel2 SAPI?  Or is this the wrong approach?


- Devon


On Aug 23, 2010, at 3:51 PM, Colin Curtin wrote:

> You should be able to use http://github.com/winks/m2php, and if it
> doesn't populate those variables, you could certainly contribute to it
> to make sure it does. :)
> 
> Colin
> 
> On Mon, Aug 23, 2010 at 1:24 PM, Devon Weller <dweller@devonweller.com> wrote:
>> Hi.
>> 
>> What would be the best way of adding some better server API support to 
a PHP application running behind a mongrel2 server?  A full server API in 
PHP has handy features like automatic population of $_GET, $_POST, 
$_COOKIE and $_SERVER variables.
>> 
>> Is this even something that makes sense when using mongrel2?  Should I 
just route dynamic page requests to nginx via a proxy and let it connect 
to PHP via FastCGI?  Or is there a better way?
>> 
>> mongrel2 and PHP work well together for passing json messages back and 
forth via ZeroMQ.  But many of the great PHP libraries and frameworks will
not work under this paradigm.  They expect a server API to populate those 
magic variables.
>> 
>> 
>> If anyone on this list has thought about this, I'd love to hear your thoughts.
>> 
>> Thanks,
>> - Devon
>> 
>> 
> 
> 
> 
> -- 
> =begin
> Colin Curtin
> Engineer, Cramer Development
> http://cramerdev.com
> email:  colin@cramerdev.com
> skype: colin.t.curtin
> phone: +1.805.694.UNIX (8649)
> =end



Begin forwarded message:

> From: Colin Curtin <colin.t.curtin@gmail.com>
> Date: August 23, 2010 3:51:30 PM CDT
> To: mongrel2@librelist.com
> Subject: Re: [mongrel2] Discussing A Full Sever API for PHP
> Reply-To: mongrel2@librelist.com
> 
> You should be able to use http://github.com/winks/m2php, and if it
> doesn't populate those variables, you could certainly contribute to it
> to make sure it does. :)
> 
> Colin
> 
> On Mon, Aug 23, 2010 at 1:24 PM, Devon Weller <dweller@devonweller.com> wrote:
>> Hi.
>> 
>> What would be the best way of adding some better server API support to 
a PHP application running behind a mongrel2 server?  A full server API in 
PHP has handy features like automatic population of $_GET, $_POST, 
$_COOKIE and $_SERVER variables.
>> 
>> Is this even something that makes sense when using mongrel2?  Should I 
just route dynamic page requests to nginx via a proxy and let it connect 
to PHP via FastCGI?  Or is there a better way?
>> 
>> mongrel2 and PHP work well together for passing json messages back and 
forth via ZeroMQ.  But many of the great PHP libraries and frameworks will
not work under this paradigm.  They expect a server API to populate those 
magic variables.
>> 
>> 
>> If anyone on this list has thought about this, I'd love to hear your thoughts.
>> 
>> Thanks,
>> - Devon
>> 
>> 
> 
> 
> 
> -- 
> =begin
> Colin Curtin
> Engineer, Cramer Development
> http://cramerdev.com
> email:  colin@cramerdev.com
> skype: colin.t.curtin
> phone: +1.805.694.UNIX (8649)
> =end

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Florian Anderiasch
Date:
2010-08-23 @ 21:54
On 23.08.2010 22:51, Colin Curtin wrote:
> You should be able to use http://github.com/winks/m2php, and if it
> doesn't populate those variables, you could certainly contribute to it
> to make sure it does. :)

Hello there,
m2php atm is only a command line executable that's talking to Mongrel2
just like the python example - thus the superglobals are populated just
like in any other command line php script.

What you probably mean, Devon, is something like PHP-FPM or PHP-FCGI
that's directly hosting PHP files, like any "usual" web server - this is
indeed not working out of the box right now.

Greetings,
Florian

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Devon Weller
Date:
2010-08-23 @ 22:04
> What you probably mean, Devon, is something like PHP-FPM or PHP-FCGI
> that's directly hosting PHP files, like any "usual" web server - this is
> indeed not working out of the box right now.

Exactly.  The m2php handler works great for communicating with a flash 
socket once an HTML page is built.

But to take advantage of PHP's session handling and other nifty HTTP 
features, I think we would need a full blown ZeroMQ-based server API 
option for PHP.  

Is that crazy?


- Devon


On Aug 23, 2010, at 4:54 PM, Florian Anderiasch wrote:

> On 23.08.2010 22:51, Colin Curtin wrote:
>> You should be able to use http://github.com/winks/m2php, and if it
>> doesn't populate those variables, you could certainly contribute to it
>> to make sure it does. :)
> 
> Hello there,
> m2php atm is only a command line executable that's talking to Mongrel2
> just like the python example - thus the superglobals are populated just
> like in any other command line php script.
> 
> What you probably mean, Devon, is something like PHP-FPM or PHP-FCGI
> that's directly hosting PHP files, like any "usual" web server - this is
> indeed not working out of the box right now.
> 
> Greetings,
> Florian

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Florian Anderiasch
Date:
2010-08-23 @ 22:15
On 24.08.2010 00:04, Devon Weller wrote:
>> What you probably mean, Devon, is something like PHP-FPM or PHP-FCGI
>> that's directly hosting PHP files, like any "usual" web server - this is
>> indeed not working out of the box right now.
> 
> Exactly.  The m2php handler works great for communicating with a flash 
socket once an HTML page is built.
> 
> But to take advantage of PHP's session handling and other nifty HTTP 
features, I think we would need a full blown ZeroMQ-based server API 
option for PHP.  
> 
> Is that crazy?

Hehe,
not crazy - but I think just putting some php-fpm behind a Mongrel2
route would be the way to go, haven't had time to look into it.

I think it would be a bad idea to introduce yet another SAPI into php
(even starting one) - if it can be avoided.

I'm not even sure if just proxying the requests to a zmq-enabled fpm
would be matching most use cases already.

Greetings,
Florian

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Zed A. Shaw
Date:
2010-08-23 @ 22:55
On Tue, Aug 24, 2010 at 12:15:33AM +0200, Florian Anderiasch wrote:
> Hehe,
> not crazy - but I think just putting some php-fpm behind a Mongrel2
> route would be the way to go, haven't had time to look into it.

I'm not terribly familiar with php or how the files run, but what other
languages did is they have a bit that receives the messages from
mongrel2 on 0MQ, and then "translates" those into what the host web app
platform needs.  For example, in Ruby they need specific script
variables like it's CGI even though they don't use them.  WSGI for
Python has a slightly different set.

To do the same thing in PHP I'd imagine you'd follow the same pattern.
Get it receive messages, then translate what's needed for the php
scripts to run as if they're inside a regular web server.  Of course
that might be harder to do in PHP, but I can't imagine why it wouldn't
be doable.

Is there a particular thing about PHP I'm missing that prevents this?

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Devon Weller
Date:
2010-08-27 @ 03:33
I'm going to give it a shot at implementing this in pure PHP code.

There are some interesting problems to solve here such as what happens 
when a script dies and whether or not to fork processes.

I'll let you know when I have some code up on github.


- Devon



On Aug 23, 2010, at 5:55 PM, Zed A. Shaw wrote:

> On Tue, Aug 24, 2010 at 12:15:33AM +0200, Florian Anderiasch wrote:
>> Hehe,
>> not crazy - but I think just putting some php-fpm behind a Mongrel2
>> route would be the way to go, haven't had time to look into it.
> 
> I'm not terribly familiar with php or how the files run, but what other
> languages did is they have a bit that receives the messages from
> mongrel2 on 0MQ, and then "translates" those into what the host web app
> platform needs.  For example, in Ruby they need specific script
> variables like it's CGI even though they don't use them.  WSGI for
> Python has a slightly different set.
> 
> To do the same thing in PHP I'd imagine you'd follow the same pattern.
> Get it receive messages, then translate what's needed for the php
> scripts to run as if they're inside a regular web server.  Of course
> that might be harder to do in PHP, but I can't imagine why it wouldn't
> be doable.
> 
> Is there a particular thing about PHP I'm missing that prevents this?
> 
> -- 
> Zed A. Shaw
> http://zedshaw.com/

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Zed A. Shaw
Date:
2010-08-27 @ 05:22
On Thu, Aug 26, 2010 at 10:33:37PM -0500, Devon Weller wrote:
> 
> I'm going to give it a shot at implementing this in pure PHP code.
> 
> There are some interesting problems to solve here such as what happens 
when a script dies and whether or not to fork processes.
> 
> I'll let you know when I have some code up on github.

Cool, also check out:

http://nanoweb.si.kz/

Gotta be some pieces you can reuse or at least get some inspiration.

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Discussing A Full Sever API for PHP

From:
Devon Weller
Date:
2010-08-27 @ 16:46
Thanks for the link.  I bet I'll find some useful stuff in there!

- Devon


On Aug 27, 2010, at 12:22 AM, Zed A. Shaw wrote:

> On Thu, Aug 26, 2010 at 10:33:37PM -0500, Devon Weller wrote:
>> 
>> I'm going to give it a shot at implementing this in pure PHP code.
>> 
>> There are some interesting problems to solve here such as what happens 
when a script dies and whether or not to fork processes.
>> 
>> I'll let you know when I have some code up on github.
> 
> Cool, also check out:
> 
> http://nanoweb.si.kz/
> 
> Gotta be some pieces you can reuse or at least get some inspiration.
> 
> -- 
> Zed A. Shaw
> http://zedshaw.com/