PHP整合七牛實(shí)現(xiàn)上傳文件
更新時(shí)間:2015年07月03日 12:11:45 投稿:hebedich
這篇文章主要介紹了PHP整合七牛實(shí)現(xiàn)上傳文件的相關(guān)資料,需要的朋友可以參考下
七牛支持抓取遠(yuǎn)程圖片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后向 post url 就完成上傳了。
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 簽名運(yùn)算
* @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 = ''){
$parsed_url = parse_url($url);
$path = $parsed_url['path'];
$access = $path;
if (isset($parsed_url['query'])) {
$access .= "?" . $parsed_url['query'];
}
$access .= "\n";
if($params){
if (is_array($params)){
$params = http_build_query($params);
}
$access .= $params;
}
$digest = hash_hmac('sha1', $access, $secret_key, true);
return $access_key.':'.urlsafe_base64_encode($digest);
}
/**
* 測(cè)試
*/
$access_key = '''your access_key';
$secret_key = 'your secret_key';
$fetch = urlsafe_base64_encode('http://203.208.46.200/images/srpr/logo11w.png');
$to = urlsafe_base64_encode('ibeircn:11.jpg');
$url = 'http://iovip.qbox.me/fetch/'. $fetch .'/to/' . $to;
$access_token = generate_access_token($access_key, $secret_key, $url);
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: QBox '. $access_token;
$con = send('iovip.qbox.me/fetch/'.$fetch.'/to/'.$to, $header);
var_dump($con);
function send($url, $header = '') {
$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 'CURL ERROR: ' . curl_error($curl);
} else {
return $con;
}
}
?>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
相關(guān)文章
php中\(zhòng)r \r\n \t的區(qū)別示例介紹
這篇文章主要介紹了php中\(zhòng)r \r\n \t的區(qū)別,需要的朋友可以參考下2014-02-02
php中實(shí)現(xiàn)記住密碼自動(dòng)登錄的代碼
記得登錄QQ的時(shí)候,我們可以選記住密碼自動(dòng)登錄,這個(gè)功能確實(shí)很方便、實(shí)用。其實(shí)在我們登錄網(wǎng)站后臺(tái)的時(shí)候,瀏覽器就會(huì)有提示是否記住登錄狀態(tài)。2011-03-03

