golang 调用 php7
<h2><strong>执行php文件</strong></h2> <pre> <code class="language-go">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) } }</code></pre> <p>其中 /tmp/index.php 的内容为</p> <pre> <code class="language-go"><?php echo("hello\n");</code></pre> <h2><strong>Eval,返回值</strong></h2> <pre> <code class="language-go">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() } }</code></pre> <p>返回的value的生命周期所有权是golang程序,所以我们要负责DestroyValue</p> <h2><strong>设置全局变量来传参</strong></h2> <pre> <code class="language-go">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() } }</code></pre> <p>传递进去的参数的生命周期是php控制的,在request shutdown的时候内存会被释放。</p> <h2><strong>PHP 回调 Golang</strong></h2> <pre> <code class="language-go">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() } }</code></pre> <h2><strong>PHP 错误日志</strong></h2> <pre> <code class="language-go">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) } }</code></pre> <p>其中 /tmp/php.ini 的内容为</p> <pre> <code class="language-go">error_reporting = E_ALL error_log = "/tmp/php-error.log"</code></pre> <p>错误会被输出到 /tmp/php-error.log。直接调用error_log会同时再输出一份到stderr</p> <h2><strong>HTTP 输入输出</strong></h2> <pre> <code class="language-go">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() } }</code></pre> <p>所有的PHP超级全局变量都会被初始化为传递进去的Request的值,包括</p> <pre> <code class="language-go">$_SERVER $_GET $_POST $_FILE $_COOKIE $_ENV</code></pre> <p>echo的内容,http code和http header会被写回到传入的ResponseWriter</p> <h2><strong>fastcgi_finish_request</strong></h2> <p>PHP-FPM 很常用的一个功能是 fastcgi_finish_request ,用于在php里做一些异步完成的事情。这个特殊的全局函数必须支持</p> <pre> <code class="language-go">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() } }</code></pre> <p>实际的作用就是把output提前输出到 ResposneWriter 里去,让调用方知道结果。对于当前进程的执行其实是没有影响的,只是影响了output。</p> <p> </p> <p>来自:https://segmentfault.com/a/1190000007619087</p> <p> </p>
本文由用户 xiejo2876v2 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!