本地网络中有一个网站。我想编写一个bash脚本,它将从本地网站下载HTML并将其发送到外部服务器,PHP脚本可以从$_POST
读取它:
html=$(wget --post-data="str=data" -qO- http://192.168.1.8/reg/index.php)
#so far so good, and $html contains data I want to send to my website
wget --post-data="html=$html" -qO- http://mywebsite.com/test.php
在test.php
中,数据总是格式不正确。
有没有办法正确逃脱$html
,还是应该完全改变我对问题的处理方法?
答案 0 :(得分:0)
在发送请求之前使用Base64对HTML进行编码:
html=$( wget --post-data="str=data" -qO- http://192.168.1.8/reg/index.php | base64 )
对您的POST数据进行URL编码。
不幸的是,wget
不会为您的帖子数据进行网址编码。如果
可用,您可以使用curl
为其提供URL编码
POST请求:
curl -S -d "str=${html}" --data-urlencode -- http://mywebsite.com/test.php
如果curl
不可用,则需要替换+
中的所有$html
在发布到%2B
之前,/
以及所有%2F
与test.php
匹配,例如
使用:
html=$( sed -e 's/+/%2B/g' -e 's/\//%2F/g' <<< "$html" )
在test.php
中,您之前已将$_POST['str']
传递给base64_decode()
使用它,见其中
documentation