从PHP运行Go脚本

huangapple go评论144阅读模式
英文:

Run Go script from PHP

问题

我有一个托管了一个PHP文件的主机,该文件接收请求,从中获取一个字符串,并将其传递给Go(GoLang)脚本。我该如何做呢?

package main

import (
	"log"
	"fmt"
	"io/ioutil"
	"strings"
	ivona "github.com/jpadilla/ivona-go"
)

func main() {
	client := ivona.New("GDNAICTDMLSLU5426OAA", "2qUFTF8ZF9wqy7xoGBY+YXLEu+M2Qqalf/pSrd9m")
	text, err := ioutil.ReadFile("/Users/Igralino/Desktop/text.txt")
	if err != nil {
		log.Fatal(err)
	}

	arrayOfParagraphs := strings.Split(string(text), "\n\n")
	i := 0
	for _, paragraph := range arrayOfParagraphs {
		paragraph = strings.TrimSpace(paragraph)
		if len(paragraph) < 1 { // against empty lines
			continue
		}
		log.Printf("%v\n", paragraph)
		options := ivona.NewSpeechOptions(paragraph)
		options.Voice.Language = "ru-RU"
		options.Voice.Name = "Maxim"
		options.Voice.Gender = "Male"
		options.OutputFormat.Codec = "MP3"
		r, err := client.CreateSpeech(options)
		if err != nil {
			log.Fatal(err)
		}

		i++
		file := fmt.Sprintf("/Users/Igralino/Desktop/tts%04d.MP3", i) // files like 0001.ogg
		ioutil.WriteFile(file, r.Audio, 0644)
	}
}

以上是您的Go脚本。

英文:

I have a hosting with a PHP file which gets the request, take a string from it and have to give to Go (GoLang) script. How could I do it?

package main
My GO script:
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-go -->

package main

import (
&quot;log&quot;
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;strings&quot;
ivona &quot;github.com/jpadilla/ivona-go&quot;
)

func main() {
	client := ivona.New(&quot;GDNAICTDMLSLU5426OAA&quot;, &quot;2qUFTF8ZF9wqy7xoGBY+YXLEu+M2Qqalf/pSrd9m&quot;)
	text, err := ioutil.ReadFile(&quot;/Users/Igralino/Desktop/text.txt&quot;)
	if err != nil {
		log.Fatal(err)
	}

	arrayOfParagraphs := strings.Split(string(text), &quot;\n\n&quot;)
	i := 0
	for _,paragraph := range arrayOfParagraphs {
		paragraph = strings.TrimSpace(paragraph)
		if (len(paragraph) &lt; 1) { // against empty lines
			continue
		}
		log.Printf(&quot;%v\n&quot;, paragraph)
		options := ivona.NewSpeechOptions(paragraph)
		options.Voice.Language = &quot;ru-RU&quot;
		options.Voice.Name = &quot;Maxim&quot;
		options.Voice.Gender = &quot;Male&quot;
		options.OutputFormat.Codec = &quot;MP3&quot;
		r, err := client.CreateSpeech(options)
		if err != nil {
			log.Fatal(err)
		}

		i++
		file := fmt.Sprintf(&quot;/Users/Igralino/Desktop/tts%04d.MP3&quot;, i) // files like 0001.ogg
		ioutil.WriteFile(file, r.Audio, 0644)
	}
}

<!-- end snippet -->

答案1

得分: 1

Go不是一种脚本语言!它是一种编译语言。

编译语言

编译语言是一种编程语言,其实现通常是编译器(从源代码生成机器代码的翻译器),而不是解释器(逐步执行源代码的执行器,在运行前不进行预翻译)。

所以使用"脚本"这个术语是非常错误的!

如何与其他程序通信

首先,你必须先构建和安装你的程序。如果你已经准备好并配置好了Go环境,这很容易做到:

$ go install github.com/user/hello

然后,你可以在命令行中调用它,因为它已经安装到操作系统中了。

$ hello

如果你的程序需要接受参数,可以使用flag包来声明它。

$ hello -name <value>

显然,你可以使用system函数或其他可能的方式从PHP中调用它(我对PHP一无所知)。

另一种方式是将你的Go程序设计为一个守护进程,并通过端口直接与它通信。你的Go程序必须持续运行并监听一个端口。这里有一个关于Go程序守护化的好答案

英文:

Go is not a scripting language! It is a compiled one.

Compiled languages

> A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place).

So usage of "script" term is dramatically wrong!

How to communicate with Go program from other programs.

You must build and install your program first. This is easy if you already prepared and configured your Go environment:

$go install github.com/user/hello

Than you can invoke it from command line since it is installed to OS.

$hello

If your program should expect arguments use flag package to declare it.

$hello -name &lt;value&gt;

Obviously you can call it from PHP with system function or whatever is possible (I don't know nothing about PHP).

Other way is to design your Go program as a daemon and communicate with it directly through a port for example. Your Go program must run continuously and listen for a port. A good answer about daemonization of Go programs.

huangapple
  • 本文由 发表于 2016年12月30日 02:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41385156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定