douzouchang7448
douzouchang7448
2012-06-27 22:33

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

已采纳

I have a PHP script that creates large complex tables. 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.

Example shell script looks like this and I want the document_content to be my generated 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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dran0703 dran0703 9年前

    The examples supplied did not mention a document_url parameter but DocRaptor's error message did.

    Working code using what I learned from hakre and anttix!

    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
    
    点赞 评论 复制链接分享
  • douzhu1188 douzhu1188 9年前

    The best way is to modify that mytablemaker.php to take the command line use case into account. e.g. like this:

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

    Then from BASH you do:

    # 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'
    
    点赞 评论 复制链接分享
  • dongzou9047 dongzou9047 9年前

    If that is bash, something like this should work:

    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. Or do a addcslashes($output, '"') on the " characters.

    See as well:

    点赞 评论 复制链接分享

相关推荐