簡體   English   中英

在shell腳本中使用PHP的HTML輸出

[英]Use HTML output of PHP in shell script

我有一個PHP腳本,可以創建大型復雜表。 我正在嘗試設置一個shell腳本,該腳本將接受一個I​​D作為參數,然后使用該ID運行PHP腳本並接受PHP腳本的HTML輸出,以作為cURL發布到DocRaptor的一部分來制作PDF。

示例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

我該怎么做呢?

如果這是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, '"')執行addcslashes($output, '"')

另見:

最好的方法是修改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'

提供的示例沒有提到document_url參數,但DocRaptor的錯誤消息沒有提到。

使用我從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