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

PHP integrates Qiniu to upload files

不言
Release: 2023-03-30 06:22:02
Original
2176 people have browsed it

This article mainly introduces the relevant information about integrating Qiniu with PHP to realize uploading files. Friends in need can refer to it

Qiniu supports grabbing remote image API, use access_key secret_key url to generate access_token, and put access_token Add it to the header and upload it to the post url to complete the upload.

Sample code:

<?php 
/* 
 * 
 * @desc URL安全形式的base64编码 
 * @param string $str 
 * @return string 
 */ 
 
 
function urlsafe_base64_encode($str){ 
  $find = array("+","/"); 
  $replace = array("-", "_"); 
  return str_replace($find, $replace, base64_encode($str)); 
} 
 
/** 
 * generate_access_token 
 * 
 * @desc 签名运算 
 * @param string $access_key 
 * @param string $secret_key 
 * @param string $url 
 * @param array $params 
 * @return string 
 */ 
function generate_access_token($access_key, $secret_key, $url, $params = &#39;&#39;){ 
  $parsed_url = parse_url($url); 
  $path = $parsed_url[&#39;path&#39;]; 
  $access = $path; 
  if (isset($parsed_url[&#39;query&#39;])) { 
    $access .= "?" . $parsed_url[&#39;query&#39;]; 
  } 
  $access .= "\n"; 
  if($params){ 
    if (is_array($params)){ 
      $params = http_build_query($params); 
    } 
    $access .= $params; 
  } 
  $digest = hash_hmac(&#39;sha1&#39;, $access, $secret_key, true); 
  return $access_key.&#39;:&#39;.urlsafe_base64_encode($digest); 
} 
 
/** 
 * 测试 
 */ 
 
$access_key = &#39;&#39;&#39;your access_key&#39;; 
$secret_key = &#39;your secret_key&#39;; 
 
$fetch = urlsafe_base64_encode(&#39;http://203.208.46.200/images/srpr/logo11w.png&#39;); 
$to = urlsafe_base64_encode(&#39;ibeircn:11.jpg&#39;); 
 
$url = &#39;http://iovip.qbox.me/fetch/&#39;. $fetch .&#39;/to/&#39; . $to; 
 
$access_token = generate_access_token($access_key, $secret_key, $url); 
 
$header[] = &#39;Content-Type: application/json&#39;; 
$header[] = &#39;Authorization: QBox &#39;. $access_token; 
 
 
$con = send(&#39;iovip.qbox.me/fetch/&#39;.$fetch.&#39;/to/&#39;.$to, $header); 
var_dump($con); 
 
function send($url, $header = &#39;&#39;) { 
  $curl = curl_init($url); 
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($curl, CURLOPT_HEADER,1); 
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
  curl_setopt($curl, CURLOPT_POST, 1); 
 
  $con = curl_exec($curl); 
 
  if ($con === false) { 
    echo &#39;CURL ERROR: &#39; . curl_error($curl); 
  } else { 
    return $con; 
  } 
} 
?>
Copy after login

The above is the entire content of this article, I hope you all like it.

The above is the detailed content of PHP integrates Qiniu to upload files. For more information, please follow other related articles on the PHP Chinese website!

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