php总结 --- 18. 七牛云存储

第三方资源拉取

 /**
  * 七牛云抓取文件
  * @param string $url 文件URL地址
  *
  * 参考地址:http://developer.qiniu.com/code/v6/api/kodo-api/rs/fetch.html
  */
 function qiniuFetch($url){
     $encodedURL = str_replace(array('+', '/'), array('-', '_'), base64_encode($url));
     $encodedEntryURI = str_replace(array('+', '/'), array('-', '_'), base64_encode(C('QINIU_BUCKET')));
     $url = '/fetch/' . $encodedURL . '/to/' . $encodedEntryURI;

     $sign = hash_hmac('sha1', $url . "\n", C('QINIU_SK'), true);
     $token = C('QINIU_AK') . ':' . str_replace(array('+', '/'), array('-', '_'), base64_encode($sign));

     $header = array('Host: iovip.qbox.me', 'Content-Type:application/x-www-form-urlencoded', 'Authorization: QBox ' . $token);

     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, trim('http://iovip.qbox.me' . $url, '\n'));
     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, "");
     $result = json_decode(curl_exec($curl), true);
     curl_close($curl);

     return $result['key'] ? C('QINIU_HOST') . $result['key'] : false;
 }

上传图片(服务端)

 /**
      * 文件上传获取Token
      */
     public function getToken(){
         $type = I('get.type', 0, 'intval');
         header("Content-type:text/html;charset=utf-8");
         $data =  array("scope" => C('QINIU_BUCKET'), "deadline" => time() + 3600);
         $data1 = array_merge($data, array('returnUrl' => 'http://icms.weilt.net'.U('Home/Upload/uploadReturn'), 'returnBody' => '{"url":"'.C('QINIU_HOST').'$(key)", "size":$(fsize), "name":"$(fname)"}'));
         $token1 = $this->token($data1, C('QINIU_AK'), C('QINIU_SK'));
         $data2 = array_merge($data, array('callbackUrl' => 'http://icms.weilt.net'.U('Home/Upload/uploadCallback'), 'callbackBody' => 'url='.C('QINIU_HOST').'$(key)&size=$(fsize)&name=$(fname)'));
         $token2 = $this->token($data2, C('QINIU_AK'), C('QINIU_SK'));
         if($type == 3){
             $this->ajaxReturn(array('token1' => $token1, 'token2' => $token2));
         }else if($type == 2){
             $this->ajaxReturn($token2);
         }else{
             $this->ajaxReturn($token1);
         }
     }
     /**
      * 单文件上传返回信息
      */
     public function uploadReturn(){
         $str = json_decode(base64_decode(str_replace(array('-', '_'), array('+', '/'), $_GET['upload_ret'])), true);
         exit('{"error":0, "url": "'.$str['url'].'"}');
     }
     /**
      * 上传成功回调函数
      */
     public function uploadCallback(){
         exit('{"error":0, "url": "'.$_POST['url'].'"}');
     }

客户端

 <form method="post" action="http://upload.qiniu.com/" enctype="multipart/form-data">
     <input name="token" type="hidden" value="...">
     <input name="file" type="file" />
 </form>

可以使用回调方式,或者直接返回的方式

http

 /**
      * 发送HTTP请求方法,目前只支持CURL发送请求
      * @param string $url 请求URL
      * @param array $param GET参数数组
      * @param array $data POST的数据,GET请求时该参数无效
      * @param string $method 请求方法GET/POST
      * @param string $cert 证书文件
      * @return array 响应数据
      */
     protected static function http($url, $param, $data = '', $method = 'GET', $cert = null){
         $opts = array(CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);

         $opts[CURLOPT_URL] = $url . '?' . http_build_query($param);
         if(strtoupper($method) == 'POST'){
             $opts[CURLOPT_POST] = 1;
             $opts[CURLOPT_POSTFIELDS] = $data;
             if(is_string($data)){
                 $opts[CURLOPT_HTTPHEADER] = array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data));
             }
         }

         if($cert){
             $opts[CURLOPT_SSLCERT] = 'PEM';
             $opts[CURLOPT_SSLCERT] = "/home/niu/work/www/weilt_new/cert/apiclient_cert.pem";
             $opts[CURLOPT_SSLKEYTYPE] = 'PEM';
             $opts[CURLOPT_SSLKEY] = "/home/niu/work/www/weilt_new/cert/apiclient_key.pem";
         }

         $ch = curl_init();
         curl_setopt_array($ch, $opts);
         $data = curl_exec($ch);
         $error = curl_error($ch);
         curl_close($ch);
         return $data;
     }
上一篇:《Web开发中块级元素与行内元素的区分》


下一篇:与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任务)