Use HTML output of PHP in shell script - php

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?

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:
Bash Scripting: Redirect output into variable

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'

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

Related

Convert curl with additional parameters into PHP curl

Trying to convert a curl command for building Jenkins jobso I can use it in PHP. Here is what I do:
<?php
$testrun_id = "1744";
$cmd="curl -X POST http://build:f9280f220bfb4c596f83a0#mobile-jenkins.me.com:8080/job/android-job-git/build --data-urlencode json='{"parameter": [{"name":"POST_RESULTS", "value":"true"}, {"name":"RUN_ID", "value":"{$testrun_id}"}, {"name":"CHECK_NAME", "value":"SampleAutomatedPlan"}]}'";
exec($cmd, $result);
?>
Here I am passing 3 parameters to the jenkins job and they are POST_RESULTS with value true, RUN_ID with value of var $testrun_id which is 1744 and CHECK_NAME whose value is SamplAutomatedPlan. When I run this curl command on mac terminal, it works perfectly fine.
What would be the missing things OR errors that gives me Parse error: parse error on that $cmd="curl..." line when I try to run this php script?
You need to escape the double quotes by adding backslashes.
$cmd = "curl -X POST http://build:f9280f220bfb4c596f83a0#mobile-jenkins.me.com:8080/job/android-job-git/build --data-urlencode json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";

Base64 and ROT13 decode in single shell command

I have a string in the following format :
(str_rot13(base64_decode("my string")))
I am trying to decode it using a single linux command by piping the output of Base64 decode to rot13.
I am attempting to use echo 'my string' | base64 --decode then pipe the output to tr 'n-za-mN-ZA-M' ‘a-zA-Z’ which applies the Rot13 decode operation on the output.
Can you guide me for the best possible way I can do it using the command line.
Edit
Apologies guys, I was looking at the partial script. I just noticed that the complete script is something like this:
<?php eval(gzinflate(str_rot13(base64_decode('my string')))); ?>
You should be able to pipe those two commands straight through (the only possible issue I can see are the ‘curly’ quotes in your tr command).
PHP
php > echo str_rot13(base64_decode("c2JiCg=="));
foo
Bash
echo 'c2JiCg==' | base64 --decode | tr 'n-za-mN-ZA-M' 'a-zA-Z'
foo

GetOpt doesn't read full URL

I have a script that need to run from a terminal or a command prompt. I'm using PHP. GetOpt is the function that I use to get data or a parameter that the user input in the terminal.
This is my script.
<?php
$opt = getopt("f:");
$input = $opt['f'];
$u = fopen($input, 'r');
echo "\n\n$input\n\n";
I tried to run it like this:
$ php myscript.php -f http://myurl.com/file.csv?city=london&status=3
My url is http://myurl.com/file.csv?city=london&status=3, but it only outputs http://myurl.com/file.csv?city=london. The status parameter is lost from the full URL.
How can I get this to work?
it's because you have to wrap your link around into quotes:
$ php myscript.php -f "http://myurl.com/file.csv?city=london&status=3"
I'll go ahead and assume you are running your script in Bash, and & in Bash might be interpreted as bitwise AND in your case:
$ echo $(( 98 & 7 ))
2

ERROR_CACHE_MISS when passing value from PHP to bash

I am trying to pass some values from a PHP file to a BASH script. I am getting a ERROR CACHE_MISS response.
The variable 'coretown' holes the value 'Houston, TX'. It must be in that format for the bash script to work.
Results of a test to prove the variables are correct
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston, TX
Execute the bash script.
$errorTrap=shell_exec("./Find-Town.sh $workPath $outPath $coreTown");
Bash script:
#!/bin/bash
set -x
InFile="./zipcode.txt"
"$Work1"="$1"
"$OutFile"="$2"
"$InString"="$3"
echo "$1";
echo "$2";
echo "$3";
Returned by the 'echo' in the script:
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston,
Notice the state (TX) is missing. If I put 'echo "$4";' in there it will display the 'TX'.
Is one of these languages handling the content of 'coreTown' ('Houston, TX') as an array? If so, which one? Amd how do I fix it? My google searches did not address this problem.
Since $coreTown contains a space, that's being treated as an argument delimiter in the shell; you need to quote it or escape the space. Luckily, PHP has a function that does that for you: escapeshellarg.
$workPathEsc = escapeshellarg($workPath);
$outPathEsc = escapeshellarg($outPath);
$coreTownEsc = escapeshellarg($coreTown);
$errorTap = shell_exec("./Find-Town.sh $workPathEsc $outPathEsc $coreTownEsc");

decode htmlentities of a file in PHP

How can I convert/decode html entities of a file's contents (XML) in PHP.
I tried to run this on a command line:
perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_1.xml
It works fine running it on command line but when I try to call it within PHP:
system("perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_6.xml");
It creates the file but it is empty. I tried to use html_entity_decode but the XML file is just too big. 20megs at least.
Any help or suggestion is greatly appreciated.
Thanks,
try escaping the $ in the system call. It is possible php is looking for a variable $_ since you are using double quotes for the string command.

Categories