是否有一个TCL等效的PHP的readfile函数?

时间:2011-09-20 14:41:12

标签: php xml tcl

情况:
想象一下两台服务器A和B.服务器B只输出一个PDF文件,可以很好地显示从通过url参数传递给它的任何xml字符串中检索到的信息。服务器A的工作是构建一个xml字符串,将这个xml字符串附加到一个url(指向服务器B上的代码)作为参数,然后使用php的readfile()函数调用url ...

问题:
有没有办法在TCL中做同样的事情(远程服务器上的readfile和输出到浏览器)?

2 个答案:

答案 0 :(得分:3)

等效的TCL读取文件是文件 http://www.tcl.tk/man/tcl8.6/TclCmd/file.htm

但是,如果已启用fopen包装器,则PHP readfile函数可以选择从URL 读取文件 http://php.net/manual/en/function.readfile.php

因此,在这种情况下,您需要使用 HTTP客户端。试试这个http://www.tcl.tk/man/tcl8.6/TclCmd/http.htm,这里有一个如何在http://wiki.tcl.tk/24061

中使用它的示例

答案 1 :(得分:2)

如果您的脚本在其stdout通道上执行输出(或假装),那么您可以使用Tcl的http包的一些额外技巧,以便将数据保存在OS层中,而不是直接通过代码拖动它:

package require http 2

set url "http://example.org/getpdf/fromxml"
set data "<example>this might be your xml</example>"

# Generate headers, based on example from PHP readfile() page
puts "Content-Description: File Transfer"
puts "Content-Type: application/pdf"
puts "Content-Disposition: attachment; filename=example.pdf"
puts "Content-Transfer-Encoding: binary"
puts "Expires: 0"
puts "Cache-Control: must-revalidate, post-check=0, pre-check=0"
puts "Pragma: public"
# No content length; data streamed from elsewhere
puts "";    # End of headers!

set tok [http::geturl $url -query $data -type text/xml -channel stdout]
# You ought to check for errors here I suppose...
http::cleanup $tok

这假设从远程主机获取的是POST,因此-query选项的默认处理是合适的。因为我们在请求中发送了一个正文,所以它不是GET(它绝对不是PUT ......)