在 PHP 中 POST Base64 编码数据
我需要使用 cURL 将一些数据 POST 到 PHP 页面,并且请求包含三个参数:
- 其中两个是常规文本值,
- 一个是 Base64 编码文件
我注意到 Base64 值在传输过程中被损坏。
这是发送请求的代码:
$filename = "img2.jpg"; //A sample image file
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$base64 = base64_encode($data);
$postData = "id=1234&sometext=asdasd&data=" . $base64;
$ch = curl_init("http://mydomain/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$httpResponse = curl_exec($ch);
curl_close($ch);
有什么提示吗?
I need to POST some data to a PHP page using cURL, and the request contains three parameters:
- Two of them are regular text values
- One is a Base64 encoded file
I've noticed that the Base64 value is corrupted during the transmission.
This is the code that's sending the request:
$filename = "img2.jpg"; //A sample image file
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$base64 = base64_encode($data);
$postData = "id=1234&sometext=asdasd&data=" . $base64;
$ch = curl_init("http://mydomain/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$httpResponse = curl_exec($ch);
curl_close($ch);
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您应该使用 urlencode() 因为
+
和=
在 base64 字符串中?Maybe you should use urlencode() because the
+
and=
in a base64 string?确保帖子数据的大小不超过 php.ini 文件中的“max_post_size”。
Make sure that the size of the post data does not exceed your 'max_post_size' in your php.ini file.
合理的猜测是编码添加了 + - 符号,这会弄乱您的数据。
编码后,尝试将替换 + 添加到 -
(当然,接收时也向后退。)
参考:
http://en.wikipedia.org/wiki/Base64#URL_applications
A fair guess is that the encoding adds + - signs, which mess up your data.
After encode, try to add replace + to -
(And backwards on recieve of course.)
Ref:
http://en.wikipedia.org/wiki/Base64#URL_applications