Home >Backend Development >PHP Tutorial >PHP uploads base64 encoded images to Qiniu
Recently I have been studying how to upload base64-encoded images to Qiniu Cloud Storage through PHP. I asked for help from Baidu and Google but couldn’t find the answer. I had no choice but to do my own research. It’s hard enough to type code on New Year’s Day. I’m happy to have succeeded. I solved the problem and share my code with you:
<code><?php require_once 'vendor/autoload.php'; header('Access-Control-Allow-Origin:*'); use Qiniu\Auth; $bucket = '要上传的空间名'; $accessKey = '你的accessKey'; $secretKey = '你的secretKey'; $auth = new Auth($accessKey, $secretKey); $upToken = $auth->uploadToken($bucket, null, 3600);//获取上传所需的token function request_by_curl($remote_server,$post_string,$upToken) { $headers = array(); $headers[] = 'Content-Type:image/png'; $headers[] = 'Authorization:UpToken '.$upToken; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$remote_server); //curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $data = curl_exec($ch); curl_close($ch); return $data; } $str="base64编码的字符串"; echo "<pre class="brush:php;toolbar:false">"; echo request_by_curl('http://upload.qiniu.com/putb64/-1',$str,$upToken); echo "";
get successfully.
The above introduces PHP to upload base64 encoded images to Qiniu, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.