Anders
Anders

Reputation: 279

Data gets corrupted when posting

I've got two subdomains, admin.xxx.com (server 1) and ressource.xxx.com (server 2)

Server 1 has a PHP-script that dynamically generates and writes a Javascript-file, based on some parameters. This part works fine, using the PHP-command "file_put-contents()"

I need to write the javascript file to server 2, that is, same domain, different subdomain; didn't succeed with any cross-domain writing, so I used this CURL-code to submit it serverside, making the file "write.php" on server 2 catch it and write it to disk on behalf of server 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);

This partly works. It generates the file, containing the javascript but the code gets corrupted for several lines

<param name="'+aed+'" value="'¥õ•±Í•íÙ…Èhõ¡È¤íh¹Í•Ñ (15 lines of garbage)

and then becomes readable again in the end.

Any idea why this is happening?

Upvotes: 2

Views: 1088

Answers (1)

Ray
Ray

Reputation: 41448

You need to URL encode the base64 data the way your doing it. Base64 strings can contain the "+", "=" and "/" characters. This could mess up the post parameters being sent.

 curl_setopt($ch,CURLOPT_POSTFIELDS, "asset=".urlencode(base64_encode($asset)));

If you're base64 encoding it to try and protect if for transfer, you won't need to do this is you urlencode it.

  curl_setopt($ch,CURLOPT_POSTFIELDS, "asset=".urlencode($asset));

Another option is to not send a string, but an array for the post fields. Then curl will do the encoding for you:

  curl_setopt($ch,CURLOPT_POSTFIELDS, array("asset" =>$asset));

Upvotes: 1

Related Questions