PHP application seven Qiniu redirect upload and callback upload instance sharing

Source: Internet
Author: User
The actual project is limited by the server capacity and bandwidth impact, upload resources often need to use the third-party object storage services, domestic more mainstream service providers have seven Qiniu, Ali and so on. This article takes seven Qiniu as an example to explain how to put the user's video upload into the space of seven Qiniu and other service providers.

There are two main types of uploads for seven Qiniu:

Service-side uploads

Front-end upload, the front end is also divided into two ways:

1). Redirect return, can solve the problem of Ajax cross-domain

2). Callback returns, seven Qiniu first to the server to return data, and then seven Qiniu back to the front end, to resolve the request not to support redirection, such as small program upload


This time use is the seven Qiniu PHP SDK;

Composer require QINIU/PHP-SDK

This time the Kindeditor editor is used.

Adding config.php under kindeditor/php is primarily a configuration parameter

<?phperror_reporting (0); Defined (' Root_path ') | | Define (' Root_path ', DirName (__dir__). ' /');d efined (' Qiniu_access_key ') | | Define (' Qiniu_access_key ', ');d efined (' Qiniu_secret_key ') | | Define (' Qiniu_secret_key ', ');d efined (' qiniu_test_bucket ') | | Define (' Qiniu_test_bucket ', ' Seven Qiniu Space names ');d efined (' Qiniu_bucket_domain ') | | Define (' Qiniu_bucket_domain ', ' VII qiniu Space website '); Defined (' Callback_url ') | | Define (' Callback_url ', ' Domain name/kindeditor/php/callback.php ');d efined (' Return_url ') | | Define (' Return_url ', ' Domain name/kindeditor/php/returnback.php '); Require_once Root_path. " Vendor/autoload.php ";

 

Adding qiniu_token.php under kindeditor/php is mainly to generate tokens for uploading

<?phpuse Qiniu\auth; Require_once __dir__. " /config.php "; Building the Authentication object $auth = new Auth (Qiniu_access_key, qiniu_secret_key); $data = [    ' returnUrl ' + return_url,];if (isset ($_request[' Is_call '))} {   $data = [      ' callbackurl ' = =] Callback_url,      ' callbackbody ' = ' key=$ ' (key) &hash=$ (ETag) &w=$ (imageinfo.width) &h=$ ( imageinfo.height) '   ];} Generate Upload Token$token = $auth->uploadtoken (qiniu_test_bucket, NULL, 3600, $data); Echo Json_encode ([   ' ERROR ' = 0,   ' token ' = + $token]);


Adding callback.php under kindeditor/php is primarily called back

<?phpuse Qiniu\auth; Require_once __dir__. " /config.php "; $_body = file_get_contents (' php://input '); $auth = new Auth (Qiniu_access_key, qiniu_secret_key);// Callback Contenttype$contenttype = ' application/x-www-form-urlencoded ';//callback signature information, you can verify that the callback is from seven kn $authorization = $_server[ ' Http_authorization ']; $isQiniuCallback = $auth->verifycallback ($contentType, $authorization, Callback_url, $_ if (! $isQiniuCallback) {    echo json_encode ([        ' ERROR ' = 2,        ' message ' = ' verification failed '    ]);    Die ();} $body = $_post; $qiniu _url = Qiniu_bucket_domain;if (!empty ($body [' key ')]) {    echo json_encode ([        ' ERROR ' = 0,        ' url ' = $qiniu _url. $body [' key ']    ];    Die ();} Echo Json_encode ([    ' ERROR ' = 1,    ' message ' = ' video upload error ']);


Adding returnback.php under kindeditor/php is primarily to redirect the receive address

<?phpuse Qiniu\auth; Require_once __dir__. " /config.php "; $upload _ret = Base64_decode ($_get[' Upload_ret ']); $upload _ret = Json_decode ($upload _ret, True); $qiniu _ url = qiniu_bucket_domain;if (!empty ($upload _ret[' key ')) {    echo json_encode ([        ' error ' + 0,        ' url ' = > $qiniu _url. $upload _ret[' key ']    );    Die ();} Echo Json_encode ([    ' ERROR ' = 1,    ' message ' = ' video upload error ']);


Next is the front-end change, I changed the video upload

Kindeditor/plugins/media/media.js

Kindeditor.plugin (' Media ', function (K) {var = this, name = ' media ', lang = Self.lang (name + '. '), allowmed Iaupload = K.undef (Self.allowmediaupload, true), Allowfilemanager = K.undef (Self.allowfilemanager, false), F            Ormatuploadurl = K.undef (Self.formatuploadurl, true), Extraparams = K.undef (Self.extrafileuploadparams, { ' token ': '//Add token} ', Filepostname = K.undef (self.filepostname, ' file '),//change filename upload name Uploadjson = K.undef (Self.uploadjson, ' https://up.qbox.me '); Change the upload address, I use the space in the east of the east using HTTPS .... function Getqtoken () {$.getjson ('/includes/ki                Ndeditor/php/qiniu_token.php ', function (data) {K (' [name= ' token "] ', Div). val (Data.token);            });             }//Get settings Upload token getqtoken (); if (allowmediaupload) {var Uploadbutton = K.uploadbutton ({button:k ('. Ke-upload-butto N ',DIV) [0], fieldname:filepostname, extraparams:extraparams, url : uploadjson,//Remove Add parameter afterupload:function (data) {...});

 

This will allow you to upload your video to the seven bull Cloud.


Related recommendations:

How a small program implements an instance of docking seven Qiniu storage

Laravel pictures using seven cow cloud upload example Introduction

Example code for yii2.0 OSS

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.