简体   繁体   English

在shell脚本中使用PHP的HTML输出

[英]Use HTML output of PHP in shell script

I have a PHP script that creates large complex tables. 我有一个PHP脚本,可以创建大型复杂表。 I am attempting to set up a shell script which would accept an ID as an argument and then run the PHP script using the ID and accept the HTML output of the PHP script for use as part of a cURL post to DocRaptor to make a PDF. 我正在尝试设置一个shell脚本,该脚本将接受一个I​​D作为参数,然后使用该ID运行PHP脚本并接受PHP脚本的HTML输出,以作为cURL发布到DocRaptor的一部分来制作PDF。

Example shell script looks like this and I want the document_content to be my generated HTML. 示例shell脚本如下所示,我希望document_content是我生成的HTML。

myHTML = usr/bin/php mytablemaker.php?id=$1
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"myHTML"}}' http://docraptor.com/docs > docraptor_sample.pdf

How do I do this correctly? 我该怎么做呢?

If that is bash, something like this should work: 如果这是bash,这样的事情应该有效:

myHTML = $(usr/bin/php mytablemaker.php?id=$1)
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"'"$myHTML"'"}}' http://docraptor.com/docs > docraptor_sample.pdf

However you don't ask for HTML but for HTML as a json string, so make you PHP script encode the string as json, see json_encode . 但是你不要求HTML,而是要求HTML作为json字符串,所以让PHP脚本将字符串编码为json,参见json_encode Or do a addcslashes($output, '"') on the " characters. 或者对"字符addcslashes($output, '"')执行addcslashes($output, '"')

See as well: 另见:

The best way is to modify that mytablemaker.php to take the command line use case into account. 最好的方法是修改mytablemaker.php以考虑命令行用例。 eg like this: 像这样:

if(isset($argv[1])) {
    $id=$argv[1];
} else {
    $id=$_GET["id"];
}

Then from BASH you do: 然后从BASH你做:

# Get HTML from PHP script and escape quotes and
# backslashes in string to comply to the JSON specification 
myHTML=$(/usr/bin/php -f mytablemaker.php $1 | sed -e 's/[\\"]/\\&/g')

# Put the value of myHTML in a JSON call and send it to the server
curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"true", "document_content":"'"$myHTML"'"}}' http://docraptor.com/docs -o docraptor_sample.pdf

Note the string concatenation done at the last line: 请注意在最后一行完成的字符串连接:

'first part'"second part"'third part'

The examples supplied did not mention a document_url parameter but DocRaptor's error message did. 提供的示例没有提到document_url参数,但DocRaptor的错误消息没有提到。

Working code using what I learned from hakre and anttix ! 使用我从hakreanttix学到的东西来编写 代码

curl -H "Content-Type:application/json" -d'{"user_credentials":"API_KEY", "doc":{"name":"docraptor_sample.pdf", "document_type":"pdf", "test":"false", "document_url":"'"http://foo.com/tablemaker.php?CTN=$1"'"}}' http://docraptor.com/docs -o docraptor_sample.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM