How would I allow a PHP front end to communicate with a back end written in Go (or any other combination of languages)?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/258558

  •  05-10-2020
  •  | 
  •  

Question

I like PHP. It's not overly complex to achieve what you want, you can write straight up HTML inside it, and I suppose I'm just used to it. I also like Go, having just discovered it while looking for something to write the back-end of a web app in that wasn't Node.js (can't abide callbacks everywhere).

I would like to be able to do most page generation in PHP because it's just what I'm comfortable doing for page generation, but I want the heavy lifting at the back to be done by Go. The back-end 'engine' needs to be able to do things like user-scheduled jobs to start with, but later extending to a web service endpoint and more, something I'd feel more comfortable doing in a compiled, statically typed language such as Go.

The only way I can think of allowing this to work is having all the PHP on an HTTP server and making REST or SOAP calls to the engine written in Go. Is this the only way to allow interoperability between the two languages or is there some other way?

Was it helpful?

Solution

Sockets are a perfectly acceptable way to do language interop. The protocol you use to pass the messages between two different processes doesn't really matter much if you only plan to expose one of them to the rest of the world. If the long term plan is to make them both available then going with a standard protocol like HTTP with JSON is the right solution.

FFI (foreign function interface) is also a potential solution but I don't really know much about PHP and its FFI support to say whether this is better or worse than going with plain sockets.

OTHER TIPS

PHP is a server-side programming language. If you want to communicate with other applications, you need to implement webservices in your Go and have PHP doing requests to those webservices. There are various ways already implemented in most of the established frameworks (Laravel, Phalcon, Symfony, Zend, CodeIgniter, CakePHP and so on - the list really is huge). The way to achieve this without a PHP framework behind your project, is via cURL calls.

However, the workflow of this is as follows: the user will have to initiate an action by doing a request to your PHP server-side component, which in turn will do a cURL request to the Go backend and afterwards serve the appropriate response back to the user. It's basically the same thing as the user doing a request directly to the Go backend, except you can do intermediary processing in the PHP component, on your requests to and responses from the Go backend.

Licensed under: CC-BY-SA with attribution
scroll top