PHP and GoLang

1,575 views
Skip to first unread message

Giovanni Panaro

unread,
Mar 9, 2014, 5:28:26 AM3/9/14
to golan...@googlegroups.com
Is there anything out there that i can install that would allow net/http to read PHP files? If not, where would I start if I wanted to make this?

Justin Israel

unread,
Mar 9, 2014, 5:31:39 AM3/9/14
to Giovanni Panaro, golang-nuts

I think you asked this same question on G+ but didn't respond to the replies? People were asking for clarification of what you are trying to do.
Are you trying to parse Php?
Are you trying to make http requests to a Php endpoint?
You mentioned something on G+ about wanting to parse HTML?

On Mar 9, 2014 10:28 PM, "Giovanni Panaro" <tsrw...@gmail.com> wrote:
Is there anything out there that i can install that would allow net/http to read PHP files? If not, where would I start if I wanted to make this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mikespook

unread,
Mar 9, 2014, 7:09:22 AM3/9/14
to Giovanni Panaro, golang-nuts

Try this: https://github.com/mikespook/goemphp

2014-3-9 下午5:28于 "Giovanni Panaro" <tsrw...@gmail.com>写道:
Is there anything out there that i can install that would allow net/http to read PHP files? If not, where would I start if I wanted to make this?

--

Giovanni Panaro

unread,
Mar 9, 2014, 12:41:58 PM3/9/14
to golan...@googlegroups.com
I did ask he same question on G+ and I forgot. I'm trying to build a web server in Go that can serve a file directory.

http.FileServer(http.Dir("D:/webroot")) 

In that directory, I have a file named test.php

<?php
print("test");
?>

I want this to execute in the Go web server. Is this possible yet?

Christoph Hack

unread,
Mar 9, 2014, 1:09:05 PM3/9/14
to golan...@googlegroups.com
The Go standard library comes with an excellent web server, but (luckily) without any PHP interpreter. You could try out some PHP interpreters written in Go (like goemphp which has been suggested earlier), but I doubt that the result would be pleasing. The better approach is to rewrite all dynamic parts in Go (instead of PHP). If that's to much work for the short term, you might also consider a "hybrid" solution. For example:

1) Run Go's http server on port 80 and let it serve the static content (images, static html pages, javascript, etc.)
2) Additionally, you could also implement some custom HTTP handlers in Go to do some server side computations and generate dynamic content (instead of PHP)
3) If you still need PHP support, you could still run an Apache webserver with PHP enabled on a different port (e.g. 8080) and use Go's ReverseProxy of the "net/http/httputil" package
to re-route some of your requests (that haven't been implemented in Go yet) to the Apache server.

-christoph

Hotei

unread,
Mar 9, 2014, 1:10:39 PM3/9/14
to golan...@googlegroups.com
One of the main benefits of go is to avoid the issues associated with php.  So I can't recommend actually doing this...

If the php code is truly as small as what your example suggests then in your handler you could recognize (method TBD) the php section and execute it with a command line php using the facilities explained in http://golang.org/pkg/os/exec/.  You'd of course have to have php installed - in which case - you could perhaps have done this without using go at all.

I suspect your goals are more complicated php code - and if if that's the case then you're probably better off just converting the php entirely to go.  Not an easy task in most cases so I understand the motivation for a simpler way.
If what you're really asking for is a php interpreter written in go then I wouldn't expect to hear a yes - but I could be surprised.

Oleku Konko

unread,
Mar 9, 2014, 1:20:21 PM3/9/14
to golan...@googlegroups.com

You are run php via fast cgi mode and use golang to route *.php to the PHP fast cgi port. That would work seamlessly.

Kevin Gillette

unread,
Mar 9, 2014, 2:15:47 PM3/9/14
to golan...@googlegroups.com
I'd certainly recommend one of the *cgi methods. It can also be used to host other languages besides PHP as well.
Reply all
Reply to author
Forward
0 new messages