使用 https://github.com/taowen/go-php7
基於https: //github.com/deuill/go-php 修改而來,fork緣由(https://github.com/deuill/go-php/issues/32)
執行php檔
func Test_exec(t *testing.T) { engine.Initialize() ctx := &engine.Context{ Output: os.Stdout, } err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) err = ctx.Exec("/tmp/index.php") if err != nil { fmt.Println(err) } }
其中/tmp/index.php
的內容為
<?php echo("hello\n");
Eval,傳回值
func Test_eval(t *testing.T) { engine.Initialize() ctx := &engine.Context{} err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) val, err := ctx.Eval("return 'hello';") if err != nil { fmt.Println(err) } defer engine.DestroyValue(val) if engine.ToString(val) != "hello" { t.FailNow() } }
傳回的value的生命週期所有權是golang程序,所以我們要負責DestroyValue
設定全域變數來傳參
func Test_argument(t *testing.T) { engine.Initialize() ctx := &engine.Context{} err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) err = ctx.Bind("greeting", "hello") if err != nil { fmt.Println(err) } val, err := ctx.Eval("return $greeting;") if err != nil { fmt.Println(err) } defer engine.DestroyValue(val) if engine.ToString(val) != "hello" { t.FailNow() } }
傳遞進去的參數的生命週期是php控制的,在request shutdown的時候記憶體會被釋放。
PHP 回呼Golang
type greetingProvider struct { greeting string } func (provider *greetingProvider) GetGreeting() string { return provider.greeting } func newGreetingProvider(args []interface{}) interface{} { return &greetingProvider{ greeting: args[0].(string), } } func Test_callback(t *testing.T) { engine.Initialize() ctx := &engine.Context{} err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) err = engine.Define("GreetingProvider", newGreetingProvider) if err != nil { fmt.Println(err) } val, err := ctx.Eval(` $greetingProvider = new GreetingProvider('hello'); return $greetingProvider->GetGreeting();`) if err != nil { fmt.Println(err) } defer engine.DestroyValue(val) if engine.ToString(val) != "hello" { t.FailNow() } }
PHP 錯誤日誌
func Test_log(t *testing.T) { engine.PHP_INI_PATH_OVERRIDE = "/tmp/php.ini" engine.Initialize() ctx := &engine.Context{ Log: os.Stderr, } err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) _, err = ctx.Eval("error_log('hello', 4); trigger_error('sent from golang', E_USER_ERROR);") if err != nil { fmt.Println(err) } }
其中/tmp/php.ini
的內容為
error_reporting = E_ALL error_log = "/tmp/php-error.log"
錯誤會被輸出到/tmp/php-error.log。直接呼叫error_log會同時再輸出一份到stderr
HTTP 輸入輸出
func Test_http(t *testing.T) { engine.Initialize() recorder := httptest.NewRecorder() ctx := &engine.Context{ Request: httptest.NewRequest("GET", "/hello", nil), ResponseWriter: recorder, } err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) _, err = ctx.Eval("echo($_SERVER['REQUEST_URI']);") if err != nil { fmt.Println(err) } body, err := ioutil.ReadAll(recorder.Result().Body) if err != nil { fmt.Println(err) } if string(body) != "/hello" { t.FailNow() } }
所有的PHP超級全域變數都會被初始化為傳遞進去的Request的值,包括
$_SERVER $_GET $_POST $_FILE $_COOKIE $_ENV
echo的內容,http code和http header會被寫回傳入的ResponseWriter
fastcgi_finish_request
PHP-FPM 很常用的一個功能是fastcgi_finish_request
,用於在php裡做一些非同步完成的事情。這個特殊的全域函數必須支援
func Test_fastcgi_finish_reqeust(t *testing.T) { engine.Initialize() buffer := &bytes.Buffer{} ctx := &engine.Context{ Output: buffer, } err := engine.RequestStartup(ctx) if err != nil { fmt.Println(err) } defer engine.RequestShutdown(ctx) ctx.Eval("ob_start(); echo ('hello');") if buffer.String() != "" { t.FailNow() } ctx.Eval("fastcgi_finish_request();") if buffer.String() != "hello" { t.FailNow() } }
實際的作用就是把output提前輸出到 ResposneWriter 裡去,讓呼叫方知道結果。對於目前進程的執行其實是沒有影響的,只是影響了output。