Execute php file
func Test_exec(t *) { () ctx := &{ Output: , } err := (ctx) if err != nil { (err) } defer (ctx) err = ("/tmp/") if err != nil { (err) } }
The content of /tmp/ is
<?php echo("hello\n");
Eval, return value
func Test_eval(t *) { () ctx := &{} err := (ctx) if err != nil { (err) } defer (ctx) val, err := ("return 'hello';") if err != nil { (err) } defer (val) if (val) != "hello" { () } }
The lifecycle ownership of the returned value is the golang program, so we are responsible for the DestroyValue
Set global variables to pass parameters
func Test_argument(t *) { () ctx := &{} err := (ctx) if err != nil { (err) } defer (ctx) err = ("greeting", "hello") if err != nil { (err) } val, err := ("return $greeting;") if err != nil { (err) } defer (val) if (val) != "hello" { () } }
The life cycle of the passed parameters is controlled by PHP, and the memory will be released when request shutdown.
PHP callback Golang
type greetingProvider struct { greeting string } func (provider *greetingProvider) GetGreeting() string { return } func newGreetingProvider(args []interface{}) interface{} { return &greetingProvider{ greeting: args[0].(string), } } func Test_callback(t *) { () ctx := &{} err := (ctx) if err != nil { (err) } defer (ctx) err = ("GreetingProvider", newGreetingProvider) if err != nil { (err) } val, err := (` $greetingProvider = new GreetingProvider('hello'); return $greetingProvider->GetGreeting();`) if err != nil { (err) } defer (val) if (val) != "hello" { () } }
PHP Error Log
func Test_log(t *) { engine.PHP_INI_PATH_OVERRIDE = "/tmp/" () ctx := &{ Log: , } err := (ctx) if err != nil { (err) } defer (ctx) _, err = ("error_log('hello', 4); trigger_error('sent from golang', E_USER_ERROR);") if err != nil { (err) } }
The content of /tmp/ is
error_reporting = E_ALL error_log = "/tmp/"
An error will be output to /tmp/. Directly calling error_log will output another copy to stderr at the same time
HTTP Input and Output
func Test_http(t *) { () recorder := () ctx := &{ Request: ("GET", "/hello", nil), ResponseWriter: recorder, } err := (ctx) if err != nil { (err) } defer (ctx) _, err = ("echo($_SERVER['REQUEST_URI']);") if err != nil { (err) } body, err := (().Body) if err != nil { (err) } if string(body) != "/hello" { () } }
All PHP super global variables will be initialized to the value of the passed Request, including
$_SERVER $_GET $_POST $_FILE $_COOKIE $_ENV
The content of echo, http code and http header will be written back to the incoming ResponseWriter
fastcgi_finish_request
A very commonly used function of PHP-FPM is fastcgi_finish_request, which is used to do some asynchronous completion in PHP. This special global function must support
func Test_fastcgi_finish_reqeust(t *) { () buffer := &{} ctx := &{ Output: buffer, } err := (ctx) if err != nil { (err) } defer (ctx) ("ob_start(); echo ('hello');") if () != "" { () } ("fastcgi_finish_request();") if () != "hello" { () } }
The actual function is to output the output to the ResposneWriter in advance so that the caller can know the result. It actually has no impact on the execution of the current process, but it only affects the output.
Thank you for reading, I hope it can help you. Thank you for your support for this site!