Home > Backend Development > PHP Tutorial > Qiniu integrates PHP to upload files

Qiniu integrates PHP to upload files

WBOY
Release: 2016-08-08 09:21:40
Original
842 people have browsed it

Qiniu supports grabbing remote image API, use access_key + secret_key + url to generate access_token, add access_token to the header, and then upload it to the 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. * Test
  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. ?>  

    Source: >

The above introduces Qiniu's integration of PHP upload files, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template