如何在PHP中获取AHK脚本执行的结果?

evrscar2  于 2023-03-28  发布在  PHP
关注(0)|答案(2)|浏览(100)

这是我的简单AHK脚本:

MsgBox % "Hello world"
ExitApp, 7777

我通过以下代码从PHP运行这个AHK:

$result = exec('start path\ahk.exe', $output, $result_code);

现在我想从ahk.exe中获取一些数据。但是当我转储变量时:

var_dump($result, $output, $result_code);

结果是:

string(0) "" array(0) { } int(0)

所有的变量都是空的,那么,我如何在执行ahk.exe后得到一些有用的数据呢?

6qfn3psc

6qfn3psc1#

您可以将结果存储在一个文本文件中,然后在PHP中读取该文本文件。

FileOpen, output.txt, w
FileWrite, % "Hello World"
FileClose, output.txt
ExitApp, 7777

然后在PHP中调用exec函数,等待文件创建,然后使用file_get_contents获取结果。

k10s72fa

k10s72fa2#

编辑:看起来我得到了它在相反.我以为你想要你的PHP结果在AHK,但看起来你想要你的AHK结果在PHP.尽管如此,下面的概念应该对您有所帮助,因为AHK可以使用带星号的FileAppend命令写入StdOut(*)的文件名,导致文本被发送到标准输出(标准输出)(但检查帮助-不是在当前控制台窗口,这与我的技术无关)。
这是可以做到的,但我不确定你想实现什么。只是发送“Hello World”到控制台?PHP可以使用StdOut吗?
如果是这样,您可以使用WScript.Shell,然后在.Exec(ComSpec " /C cscript " command)方法中传递命令,并返回exec.StdOut.ReadAll()
我有一个工作的例子如下,也许它会帮助你:

;// The following AHK script runs a command (add.vbs but yours would be the PHP script?) 
;// and retrieves its output via StdOut:

InputBox, x,,"Enter two numbers" ;//asks for two ints to pass to script
MsgBox % """" RunWaitOne("add.vbs " x) """" ;// result in quotes in a ahk msgbox
ExitApp

RunWaitOne(command) ;// this is the function
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec(ComSpec " /C cscript /nologo " command)
    return exec.StdOut.ReadAll()
}

/* This is the external "add.vbs" command used in example (replace with yours):
a=3
b=4
if WScript.Arguments.Count > 0 Then
    a=cint(WScript.Arguments.Item(0))
    b=cint(WScript.Arguments.Item(1))
End If
Dim StdOut : Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
x=a+b
StdOut.Write x
*/

嗨,

相关问题