require_once'qiniu/autoload.php';
header('Access-Control-Allow-Origin:*');
define('QINIU_BUCKET', 'winroad'); //这是你的存储空间名称
define('QINIU_ACCESSKEY', 'CMD9E7Oj7ftTm_e****WFWvL5C7rWDHnuy');
define('QINIU_SECRETKEY', 'Gce828lG-_wTB****EKbrkF7MetST6sIPn0mS');
define('QINIU_DOMAIN', ' //测试域名
define('QINIU_UPLOAD_URL', 'http://upload.qiniu.com/putb64/-1');//上传路径
useQiniu\Auth;
$bucket= QINIU_BUCKET;
$accessKey= QINIU_ACCESSKEY;
$secretKey= QINIU_SECRETKEY;
$auth= newAuth($accessKey, $secretKey);
$upToken= $auth->uploadToken($bucket, null, 3600);//获取上传所需的token
define('QINIU_UP_TOKEN', $upToken);//token
functionrequest_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;
}
functionqiniu_upload($base64)
{
$base64= trim($base64);
$base64= str_replace('data:image/jpeg;base64,', '', $base64); //只要逗号后面的
$ret= $this->request_by_curl(QINIU_UPLOAD_URL, $base64, QINIU_UP_TOKEN);
$ret= json_decode($ret, true);
if($ret) {
return$ret['key']; //只要返回的key,将key与domain拼接就能得到 上传后的url了
} else{
returnfalse;
}
}
网友评论