我有两个子域名admin.xxx.com(服务器1)和ressource.xxx.com(服务器2)
服务器1有一个PHP脚本,它根据一些参数动态生成和写入Javascript文件。这部分工作正常,使用PHP命令" file_put-contents()"
我需要将javascript文件写入服务器2,即同一个域,不同的子域;在任何跨域编写都没有成功,所以我使用这个CURL代码将其提交到服务器端,使文件" write.php"在服务器2上捕获它并代表服务器1将其写入磁盘:
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, "http://ressource.xxx.dk/write.php");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, "asset=".base64_encode($asset));
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
这部分有效。它生成包含javascript的文件,但代码在几行中被破坏
<param name="'+aed+'" value="'¥õ•±Í•íÙ…Èhõ¡È¤íh¹Í•Ñ (15 lines of garbage)
然后在最后再次变得可读。
知道为什么会这样吗?
答案 0 :(得分:1)
您需要按照您的方式对base64数据进行URL编码。 Base64字符串可以包含“+”,“=”和“/”字符。这可能会搞乱发送的帖子参数。
curl_setopt($ch,CURLOPT_POSTFIELDS, "asset=".urlencode(base64_encode($asset)));
如果您使用base64对其进行编码以尝试保护,如果要进行转移,则无需执行此操作即可对其进行urlencode。
curl_setopt($ch,CURLOPT_POSTFIELDS, "asset=".urlencode($asset));
另一种选择是不发送字符串,而是发送字段的数组。然后curl会为你做编码:
curl_setopt($ch,CURLOPT_POSTFIELDS, array("asset" =>$asset));