首页 > 代码库 > php总结 --- 18. 七牛云存储
php总结 --- 18. 七牛云存储
第三方资源拉取
1 /** 2 * 七牛云抓取文件 3 * @param string $url 文件URL地址 4 * 5 * 参考地址:http://developer.qiniu.com/code/v6/api/kodo-api/rs/fetch.html 6 */ 7 function qiniuFetch($url){ 8 $encodedURL = str_replace(array(‘+‘, ‘/‘), array(‘-‘, ‘_‘), base64_encode($url)); 9 $encodedEntryURI = str_replace(array(‘+‘, ‘/‘), array(‘-‘, ‘_‘), base64_encode(C(‘QINIU_BUCKET‘)));10 $url = ‘/fetch/‘ . $encodedURL . ‘/to/‘ . $encodedEntryURI;11 12 $sign = hash_hmac(‘sha1‘, $url . "\n", C(‘QINIU_SK‘), true);13 $token = C(‘QINIU_AK‘) . ‘:‘ . str_replace(array(‘+‘, ‘/‘), array(‘-‘, ‘_‘), base64_encode($sign));14 15 $header = array(‘Host: iovip.qbox.me‘, ‘Content-Type:application/x-www-form-urlencoded‘, ‘Authorization: QBox ‘ . $token);16 17 $curl = curl_init();18 curl_setopt($curl, CURLOPT_URL, trim(‘http://iovip.qbox.me‘ . $url, ‘\n‘));19 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);20 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);21 curl_setopt($curl, CURLOPT_POSTFIELDS, "");22 $result = json_decode(curl_exec($curl), true);23 curl_close($curl);24 25 return $result[‘key‘] ? C(‘QINIU_HOST‘) . $result[‘key‘] : false;26 }
上传图片(服务端)
1 /** 2 * 文件上传获取Token 3 */ 4 public function getToken(){ 5 $type = I(‘get.type‘, 0, ‘intval‘); 6 header("Content-type:text/html;charset=utf-8"); 7 $data = array("scope" => C(‘QINIU_BUCKET‘), "deadline" => time() + 3600); 8 $data1 = array_merge($data, array(‘returnUrl‘ => ‘http://icms.weilt.net‘.U(‘Home/Upload/uploadReturn‘), ‘returnBody‘ => ‘{"url":"‘.C(‘QINIU_HOST‘).‘$(key)", "size":$(fsize), "name":"$(fname)"}‘)); 9 $token1 = $this->token($data1, C(‘QINIU_AK‘), C(‘QINIU_SK‘));10 $data2 = array_merge($data, array(‘callbackUrl‘ => ‘http://icms.weilt.net‘.U(‘Home/Upload/uploadCallback‘), ‘callbackBody‘ => ‘url=‘.C(‘QINIU_HOST‘).‘$(key)&size=$(fsize)&name=$(fname)‘));11 $token2 = $this->token($data2, C(‘QINIU_AK‘), C(‘QINIU_SK‘));12 if($type == 3){13 $this->ajaxReturn(array(‘token1‘ => $token1, ‘token2‘ => $token2));14 }else if($type == 2){15 $this->ajaxReturn($token2);16 }else{17 $this->ajaxReturn($token1);18 }19 }20 /**21 * 单文件上传返回信息22 */23 public function uploadReturn(){24 $str = json_decode(base64_decode(str_replace(array(‘-‘, ‘_‘), array(‘+‘, ‘/‘), $_GET[‘upload_ret‘])), true);25 exit(‘{"error":0, "url": "‘.$str[‘url‘].‘"}‘);26 }27 /**28 * 上传成功回调函数29 */30 public function uploadCallback(){31 exit(‘{"error":0, "url": "‘.$_POST[‘url‘].‘"}‘);32 }
客户端
1 <form method="post" action="http://upload.qiniu.com/" enctype="multipart/form-data">2 <input name="token" type="hidden" value="http://www.mamicode.com/...">3 <input name="file" type="file" />4 </form>
可以使用回调方式,或者直接返回的方式
php总结 --- 18. 七牛云存储
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。