在shell脚本中使用PHP的HTML输出
我有一个创建大型复杂表的PHP脚本。我试图设置一个shell脚本,它将接受一个ID作为参数,然后使用该ID运行PHP脚本,并接受PHP脚本的HTML输出,作为cURL帖子的一部分用于DocRaptor制作PDF。在shell脚本中使用PHP的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
我该如何正确地做到这一点?
提供的例子没有提到DOCUMENT_URL参数,但DocRaptor的错误消息。
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
如果是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
但是你不要问HTML但对于HTML作为一个JSON字符串,所以让你的PHP脚本编码字符串作为JSON,看json_encode
。或者在"
字符上执行addcslashes($output, '"')
。
看得那么清楚:
谢谢! DocRaptor的示例不对HTML AFAICS进行编码。他们的例子sh在这里:https://raw.github.com/gist/1045686/5173a5a148eabe6065a67b85018f41a15f099045/doc_raptor_example.sh – jerrygarciuh
他们做的,看看我的意思是看看json中的字符串是如何工作的:http:// json。 org - 如果PHP脚本输出的HTML包含一个'''例如并且它没有被转义,那么remove服务会认为'document_content'字符串在那里结束 - 如果甚至整个json都是无效的并且它完全拒绝操作( !)。 – hakre
heh,生成一个带有内容的PDF文件:'usr/bin/php reports_scorecard_blank_PDFMAKER.php?id = $ 1'我会尝试不用引号 – jerrygarciuh
,最好的办法是修改mytablemaker.php采取命令行使用情况考虑在内。 例如像这样:
if(isset($argv[1])) {
$id=$argv[1];
} else {
$id=$_GET["id"];
}
从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
注意在最后一行所做的字符串连接:
'first part'"second part"'third part'
不确定你对连线的意思是什么?我正在尝试你的方式,并在PHP中使用或不使用JSON编码。DocRaptor报告获取空字符串。 – jerrygarciuh
您是否编辑了PHP脚本来考虑命令行用例?如果将echo“$ myHTML”作为第二个命令添加到脚本中会怎样? – anttix
错误信息帮我解决了!他们还接受一个document_url参数,这些参数在示例中未显示!现在就开始工作吧。谢谢! – jerrygarciuh
现在我很困惑。如果您删除以myHTML = ...开头的行,该脚本是否仍然有效?我认为它会,并且你已经救了你自己的一些记忆:) – anttix
你是对的......老板在我工作之后把我从这个权利中解救出来。他决定使用screencap服务并提供JPEG而不是PDF。我没有很好的关注。将从答案中删除。 – jerrygarciuh