Coder Social home page Coder Social logo

arnaud-lb / php-go Goto Github PK

View Code? Open in Web Editor NEW
185.0 13.0 19.0 61 KB

php-go allows to call Go code from PHP, with minimal code boilerplate

License: MIT License

Go 21.34% C 61.96% PHP 13.14% Shell 1.68% Makefile 0.35% M4 0.76% JavaScript 0.77%
php go golang php-go

php-go's Introduction

php-go

Supported PHP versions: 7.x Build Status

php-go allows to call Go code from PHP, with minimal code boilerplate.

Goals:

  • Allow to export Go functions and Go constants from Go to PHP
  • Be reliable and always safe
  • Deploy Go code without re-building the PHP extension

TODO:

  • Support exporting functions
  • Support all scalar types in arguments and return values
  • Support exporting constants
  • Support slices, maps (copying)
  • Support objects (proxying)

Install

You can download this package using "go get". When using "go get", you'll have to set your $GOPATH first. Then you can run:

go get github.com/arnaud-lb/php-go

When this is finished, change directories to the included "ext" folder:

cd $GOPATH/src/github.com/arnaud-lb/php-go/ext

Then configure and make the binary:

phpize && ./configure && make && sudo make install

Then add extension=phpgo.so to your php.ini, or call php with -dextension=phpgo.so

Note: php-go supports PHP 7 (non-ZTS). For PHP 5, use the php5 branch.

Usage

Exporting Go functions

package main

import (
  "strings"
  "github.com/arnaud-lb/php-go/php-go"
)

// call php.Export() for its side effects
var _ = php.Export("example", map[string]interface{}{
  "toUpper": strings.ToUpper,
  "takeOverTheWorld": TakeOverTheWorld,
})

func TakeOverTheWorld() {
}

func main() {
}

The module can then be compiled as a shared library using -buildmode c-shared:

go build -o example.so -buildmode c-shared .

Note: Go requires that the module be a main package with a main function in this mode.

Using the module in PHP

// Create a class from the Go module, and return an instance of it
$module = phpgo_load("/path/to/example.so", "example");

// Display the methods defined by the class
ReflectionClass::export($module);

// Call some method
$module->toUpper("foo");

php-go's People

Contributors

arnaud-lb avatar dasjott avatar roelofjan-elsinga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-go's Issues

HHVM support?

I'm trying to use php-go with hhvm

Here are the steps I've done so far.

  • Install hhvm-dev
  • Cloned https://github.com/arnaud-lb/php-go
  • Changed to php5 branch
  • Cd to php-go/etc/
  • Run make
  • Make install
  • Go get https://github.com/arnaud-lb/php-go
  • Write example go script
  • Run buildmode c-shared
  • Update php.ini to point to .so file
  • enable hhvm.enable_zend_compat=1
  • Tried running hhvm and got undefined symbol = zend_new_interned string
    • [Wed Jul 25 16:05:17 2018] [hphp] [1:7fa402c81980:0:000001] [] Uncaught exception: Could not open extension /etc/hhvm/phpgo.so: /etc/hhvm/phpgo.so: undefined symbol: zend_new_interned_string\n

So my issue right now is that i need to run hphpize but it cannot find config.cmake. Not sure what needs to be in this file.

Here's a few links that might help
https://github.com/hhvm/extension-example
https://github.com/facebook/hhvm/wiki/Extension-API

Cause php-fpm crash, [pool www] child 47122 exited on signal 11

Sometimes php-go works, but sometimes cause php-fpm crash: [pool www] child 47122 exited on signal 11.

test.php

<?php

$module = phpgo_load(__DIR__ . "/example.so", "main");
$res =  $module->zk_get(implode('|', array('127.0.0.1:2181', '127.0.0.1:2182', '127.0.0.1:2183')), implode('|', array('/some/path')));
var_dump($res);

test.go

package main

import (
	"strings"
	"time"

	"github.com/arnaud-lb/php-go/php-go"
	"github.com/samuel/go-zookeeper/zk"
)

type noLogger struct{}

func (l *noLogger) Printf(fmt string, v ...interface{}) {
}

func zk_get(addrs string, paths string) string {
	addrss := strings.Split(addrs, "|")
	pathss := strings.Split(paths, "|")

	conn, _, err := zk.Connect(addrss, time.Second*10)
	if err != nil {
		return ""
	}

	conn.SetLogger(&noLogger{})

	results := []string{}
	for i := range pathss {
		result, _, err := conn.Get(pathss[i])
		if err != nil {
			return ""
		}
		results = append(results, strings.Trim(string(result), "\r\n"))
	}

	return strings.Join(results, "|")
}

var _ = php.Export("main", map[string]interface{}{
	"zk_get": zk_get,
})

func main() {
}

loading extension twice results in an error

Hi,
When you run :

$module = phpgo_load("/path/to/example.so", "example");
$module = phpgo_load("/path/to/example.so", "example");

$module->toUpper("foo");

twice and try to call a function you'll get an error :

Uncaught Error: Call to undefined method PHPGo\Module\��Ә��_1_426c601a::toUpper.. 

initializing it once will work ..

I have PHP running "fpm" context so it can happen that a worker gets reused and thus calls phpgo_load again.
Haven't looked to it in detail but think it's something with the cache key of the loaded module in the php extension that goes wrong.

panic runtime error: index out of range

I'm getting the following error when trying to reproduce the example on php 7.2.8

[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "panic: runtime error: index out of range"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: ""
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "goroutine 1 [running]:"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "reflect.(*rtype).In(0x7f9003b3ad60, 0x0, 0x7f9003b3ad60, 0x7f9003b61370)"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: " /usr/local/go/src/reflect/type.go:995 +0x95"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "github.com/arnaud-lb/php-go/php-go.newPHPExport(0x7f90038fe430, 0x10, 0x7f9003b3ad60, 0x7f9003b61370, 0x1, 0xc42000e040, 0x0)"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: " /root/work/src/github.com/arnaud-lb/php-go/php-go/phpgo.go:130 +0x244"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "github.com/arnaud-lb/php-go/php-go.newPHPExports(0xc4200861b0, 0xc420086180, 0x18, 0x0)"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: " /root/work/src/github.com/arnaud-lb/php-go/php-go/phpgo.go:61 +0x11e"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "github.com/arnaud-lb/php-go/php-go.Export(0x7f90038fceec, 0x7, 0xc4200861b0, 0x10)"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: " /root/work/src/github.com/arnaud-lb/php-go/php-go/phpgo.go:47 +0x31"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: "main.init()"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 said into stderr: " /root/work/src/phpgo/test/main.go:10 +0x157"
[30-Jul-2018 12:04:48] WARNING: [pool www] child 23 exited on signal 6 (SIGABRT) after 1739.775042 seconds from start
[30-Jul-2018 12:04:48] NOTICE: [pool www] child 35 started

Any sugestions?

can't get output to display on browser or curl

It works when I run it in the terminal with php
But it doesn't display anything in the browser. The rest of my php works, but anything after phpgo_load() is no longer displayed.

PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
nginx version: nginx/1.18.0 (Ubuntu)

It can be seen here:
https://impeccableservices.org/dns/dig.php

Code:

<?php

echo "<center>";
echo "<br style='margin-top: 150px'>";
echo "DIG<br>";
echo $_GET['domain'];
echo "<br><a href='.'>Back</a>";
echo "<br>sss</center>";
include("index.php");


///EVERYTHING ABOVE THIS IS DISPLAYED ON THE BROWSER
//THE REST ONLY DISPLAYS WHEN RAN ON A TERMINAL
$module = phpgo_load("/home/impec/public_html/dns/dig.so", "dig");
echo "ssss";

if(isset($_GET['domain'])) {
echo $_GET['domain'];
echo "\n";
echo $module->run($_GET['domain']);
} else {
echo "google.com\n";
echo $module->run('google.com');
}
?>

Add php go to pecl

It would be much more easier for installation if this is added to pecl repository. Though PHP FFI is an alternative to this.

PHP Startup: Invalid library

This is my php file:

<?php
$module = phpgo_load("example.so", "example");
echo $module->toUpper('foo');

When I run the command

php  index.php 

I got this error

PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) '/usr/lib/php/20170718/example.so' in Unknown on line 0
PHP Fatal error:  Uncaught Error: Call to undefined function toUpper() in /home/tohid/go/src/gotutorial/extension/index.php:2
Stack trace:
#0 {main}
  thrown in /home/tohid/go/src/gotutorial/extension/index.php on line 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.