+
80
-

如何在go中创建php环境运行php代码返回给go?

如何在go中创建php环境运行php代码返回给go?


网友回复

+
0
-

这个项目:https://github.com/arnaud-lb/php-go

package main

import (
    "fmt"
    php "github.com/deuill/go-php"
)

func main() {
    engine, _ := php.New()
    context, _ := engine.NewContext()

    var str string = "Hello"
    context.Bind("var", str)

    val, _ := context.Eval("return $var.' World';")
    fmt.Printf("%s", val.Interface())
    // Prints 'Hello World' back to the user.

    engine.Destroy()
}

+
0
-

还可以通过exec

		
package main
import "fmt"
import "os/exec"
func main() {


    phpCode := `
    echo "hello"; 
    ?>
    ` 

    out, err := exec.Command("php", "-r", phpCode).Output()

    str := string(out)

    fmt.Println(str)
    fmt.Println(err)
   fmt.Println("\n")
}

我知道答案,我要回答