Integrate PHP to upload files. _ PHP Tutorial

Source: Internet
Author: User
Qi Niu Integrated PHP to upload files ,. Qiniu integrates PHP to upload files. Qiniu supports the remote image capture API. it uses access_key + secret_key + url to generate access_token and adds access_token to the header, then integrate PHP with posturl to upload files,
Qiniu supports the remote image capture API. it uses access_key + secret_key + url to generate access_token, adds access_token to the header, and then uploads the image to the post url. Sample code:
  1. /*
  2. *
  3. * @ Desc base64 encoding in the URL safe format
  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 signature calculation
  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. * Test
  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/logo111_png ');
  45. $ To = urlsafe_base64_encode ('ibeircn: 11.jpg ');
  46. $ Url = 'http: // iovip. qbox. me/fetch/'. $ fetch.'/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. ?>
    Source: >

Token, Qiniu supports the API for capturing remote images. use access_key + secret_key + url to generate access_token, add access_token to the header, and then add it to the post url...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.