如何将字符串从php传递到tcl并执行脚本

如何将字符串从php传递到tcl并执行脚本,php,string,tcl,passthru,Php,String,Tcl,Passthru,我想从php中传递字符串,就像 <?php str1="string to pass" #not sure about passthru ?> 这可能吗?请让我知道我被这件事缠住了,这是可能的 test.php <?php $str1="Stackoverflow!!!"; $cmd = "tclsh mycode.tcl $str1"; $output = shell_exec($cmd); echo $output; ?> 最简单的机制是将Tcl脚本作为一个子进程

我想从php中传递字符串,就像

<?php
str1="string to pass"
#not sure about passthru
?>
这可能吗?请让我知道我被这件事缠住了,这是可能的

test.php

<?php
$str1="Stackoverflow!!!";
$cmd = "tclsh mycode.tcl $str1";
$output = shell_exec($cmd);
echo $output;
?>

最简单的机制是将Tcl脚本作为一个子进程运行,该子进程运行一个接收脚本(您可能会将该脚本放在与PHP代码相同的目录中,或者放在其他位置),该脚本对传递的参数进行解码,并执行您所需的操作

因此,在PHP方面,您可以这样做(注意这里的
escapeshellarg
的重要用途!我建议使用中带空格的字符串作为测试用例,以确定代码引用的内容是否正确):


在Tcl端,参数(在脚本名称之后)被放在全局
argv
变量的列表中。脚本可以使用任意数量的列表操作将它们拉出。这里有一种方法,使用
lindex

set msg [lindex $argv 0]
# do something with the value from the argument
puts "Hello to '$msg' from a Tcl script running inside PHP."
另一种方法是使用
lassign

lassign $argv msg
puts "Hello to '$msg' from a Tcl script running inside PHP."
但是请注意(如果您使用Tcl的
exec
调用子程序),Tcl会自动为您引用参数。(实际上,出于技术原因,它确实在Windows上做到了这一点。)Tcl不需要像escapeshellarg这样的东西,因为它将参数视为字符串序列,而不是单个字符串,因此更了解正在发生的事情



传递值的其他选项包括环境变量、管道、文件内容和套接字。(或通过更奇特的方式)进程间通信的一般主题在两种语言中都可能变得非常复杂,并且涉及到许多权衡;你需要非常确定你要做什么才能明智地选择一个选项。

你能解释一下什么是$argv吗?还有一件事,如果我在本地主机上运行php代码,它可以在我的服务器上触发tcl脚本吗?@ArunBaskar:是的,它可以。请参阅
argv
用法。我在localhost中运行您的php代码,但在第2行出现解析错误。@ArunBaskar:已修复。
set msg [lindex $argv 0]
# do something with the value from the argument
puts "Hello to '$msg' from a Tcl script running inside PHP."
lassign $argv msg
puts "Hello to '$msg' from a Tcl script running inside PHP."