七牛整合PHP上傳檔案,_PHP教程

來源:互聯網
上載者:User

七牛整合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. ?>
    來源: >

http://www.bkjia.com/PHPjc/1025224.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1025224.htmlTechArticle七牛整合PHP上傳檔案, 七牛支援抓取遠程圖片 API,用 access_key + secret_key + url 產生 access_token, 把 access_token 加在 header 裡,然後向 post url 就...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.