Heim  >  Artikel  >  Backend-Entwicklung  >  七牛整合PHP上传文件

七牛整合PHP上传文件

WBOY
WBOYOriginal
2016-08-08 09:21:40802Durchsuche

七牛支持抓取远程图片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后向 post url 就完成上传了。

Sample code:

  1. /* 
  2.  * 
  3.  * @desc URL安全形式的base64编码 
  4.  * @param string $str 
  5.  * @return string 
  6.  */  
  7.   
  8.   
  9. function urlsafe_base64_encode($str){  
  10.     $find = array("+","/");  
  11.     $replace = array("-", "_");  
  12.     return str_replace($find, $replace, base64_encode($str));  
  13. }  
  14.   
  15. /** 
  16.  * generate_access_token 
  17.  * 
  18.  * @desc 签名运算 
  19.  * @param string $access_key 
  20.  * @param string $secret_key 
  21.  * @param string $url 
  22.  * @param array  $params 
  23.  * @return string 
  24.  */  
  25. function generate_access_token($access_key, $secret_key, $url, $params = ''){  
  26.     $parsed_url = parse_url($url);  
  27.     $path = $parsed_url['path'];  
  28.     $access = $path;  
  29.     if (isset($parsed_url['query'])) {  
  30.         $access .= "?" . $parsed_url['query'];  
  31.     }  
  32.     $access .= "\n";  
  33.     if($params){  
  34.         if (is_array($params)){  
  35.             $params = http_build_query($params);  
  36.         }  
  37.         $access .= $params;  
  38.     }  
  39.     $digest = hash_hmac('sha1', $access, $secret_key, true);  
  40.     return $access_key.':'.urlsafe_base64_encode($digest);  
  41. }  
  42.   
  43. /** 
  44.  * 测试 
  45.  */  
  46.   
  47. $access_key = '''your access_key';  
  48. $secret_key = 'your secret_key';  
  49.   
  50. $fetch = urlsafe_base64_encode('http://203.208.46.200/images/srpr/logo11w.png');  
  51. $to = urlsafe_base64_encode('ibeircn:11.jpg');  
  52.   
  53. $url  = 'http://iovip.qbox.me/fetch/'. $fetch .'/to/' . $to;  
  54.   
  55. $access_token = generate_access_token($access_key, $secret_key, $url);  
  56.   
  57. $header[] = 'Content-Type: application/json';  
  58. $header[] = 'Authorization: QBox '. $access_token;  
  59.   
  60.   
  61. $con = send('iovip.qbox.me/fetch/'.$fetch.'/to/'.$to, $header);  
  62. var_dump($con);  
  63.   
  64. function send($url, $header = '') {  
  65.     $curl = curl_init($url);  
  66.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
  67.     curl_setopt($curl, CURLOPT_HEADER,1);  
  68.     curl_setopt($curl, CURLOPT_HTTPHEADER, $header);  
  69.     curl_setopt($curl, CURLOPT_POST, 1);  
  70.   
  71.     $con = curl_exec($curl);  
  72.   
  73.     if ($con === false) {  
  74.         echo 'CURL ERROR: ' . curl_error($curl);  
  75.     } else {  
  76.         return $con;  
  77.     }  
  78. }  
  79. ?>  

    来源: >

     

以上就介绍了七牛整合PHP上传文件,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn