POST Base64 encoded data in PHP

强颜欢笑 提交于 2019-12-10 20:28:07

问题


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?


回答1:


Maybe you should use urlencode() because the + and = in a base64 string?




回答2:


Make sure that the size of the post data does not exceed your 'max_post_size' in your php.ini file.




回答3:


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



来源:https://stackoverflow.com/questions/4206666/post-base64-encoded-data-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!