how to encode data so that Bash does not interfere with content (bash script using PHP)
I m usin开发者_Go百科g a bash script to curl an XMLRPC file.
However, the XML I am sending does often contain bash variables and other interfering characters (and/or variables) like"
, '
, $
, $HOME
etc.
I am not sure what all characters do interfere here or if this is related to something else.
When I run a very minimal XML through the same script, I get the expected results, but an XML with above chars (or variables) gives me: parse error. not well formed
Any help here is really appreciated!
Regards
Nikhil GuptaADDED CODE:
curl -k -H "Content-Type: application/xml" -X POST --data-urlencode "${XML}" $URL/xmlrpc.php
data1=$(php -r "echo htmlentities('$data1',ENT_QUOTES,'UTF-8');")
data2=$(php -r "echo htmlentities('$data2',ENT_QUOTES,'UTF-8');")
$XML is XML which utilizes data1
and data2
variables, which contain the problematic chars, e.g. the following XML gives error:
<?xml version='1.0' encoding='iso-8859-1'?>
<methodCall>
<methodName>get.usage</methodName>
<params>
<param>
<struct>
<member> <name>script</name> <value> <string>#!/bin/bash echo $HOME;</string> </value> </member>
</struct>
</param>
</params>
</methodCall>
Use a file, rather than passing the XML on the command line. This will help you avoid escaping issues:
curl -k -H "Content-Type: application/xml" -X POST --data-urlencode @xml_filename $URL/xmlrpc.php
You obviously have to write to that file first. You can use tempnam if you want a temporary name.
Found the reason.. sorry to bother everyone here.. (esp. Matthew Flaschen)
Somehow, while testing different configurations, I fixed on to: --data-urlencode
while --data-binary
is what curl will need.
精彩评论