当前位置:Gxlcms > PHP教程 > 七牛整合PHP上传文件,_PHP教程

七牛整合PHP上传文件,_PHP教程

时间:2021-07-01 10:21:17 帮助过:16人阅读

七牛整合PHP上传文件,


七牛支持抓取远程图片 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. function urlsafe_base64_encode($str){
  8. $find = array("+","/");
  9. $replace = array("-", "_");
  10. return str_replace($find, $replace, base64_encode($str));
  11. }
  12. /**
  13. * generate_access_token
  14. *
  15. * @desc 签名运算
  16. * @param string $access_key
  17. * @param string $secret_key
  18. * @param string $url
  19. * @param array $params
  20. * @return string
  21. */
  22. function generate_access_token($access_key, $secret_key, $url, $params = ''){
  23. $parsed_url = parse_url($url);
  24. $path = $parsed_url['path'];
  25. $access = $path;
  26. if (isset($parsed_url['query'])) {
  27. $access .= "?" . $parsed_url['query'];
  28. }
  29. $access .= "\n";
  30. if($params){
  31. if (is_array($params)){
  32. $params = http_build_query($params);
  33. }
  34. $access .= $params;
  35. }
  36. $digest = hash_hmac('sha1', $access, $secret_key, true);
  37. return $access_key.':'.urlsafe_base64_encode($digest);
  38. }
  39. /**
  40. * 测试
  41. */
  42. $access_key = '''your access_key';
  43. $secret_key = 'your secret_key';
  44. $fetch = urlsafe_base64_encode('http://203.208.46.200/images/srpr/logo11w.png');
  45. $to = urlsafe_base64_encode('ibeircn:11.jpg');
  46. $url = 'http://iovip.qbox.me/fetch/'. $fetch .'/to/' . $to;
  47. $access_token = generate_access_token($access_key, $secret_key, $url);
  48. $header[] = 'Content-Type: application/json';
  49. $header[] = 'Authorization: QBox '. $access_token;
  50. $con = send('iovip.qbox.me/fetch/'.$fetch.'/to/'.$to, $header);
  51. var_dump($con);
  52. function send($url, $header = '') {
  53. $curl = curl_init($url);
  54. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  55. curl_setopt($curl, CURLOPT_HEADER,1);
  56. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  57. curl_setopt($curl, CURLOPT_POST, 1);
  58. $con = curl_exec($curl);
  59. if ($con === false) {
  60. echo 'CURL ERROR: ' . curl_error($curl);
  61. } else {
  62. return $con;
  63. }
  64. }
  65. ?>
    来源: >

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1025224.htmlTechArticle七牛整合PHP上传文件, 七牛支持抓取远程图片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后向 post url 就...

人气教程排行