#说明:我这使用的是类,可根据自己需求进行拆分为发方法,我使用的是authorizationV2使用v1的可以自行去看文档或者sdk
#七牛云官方文档路径:[七牛云资源列举文档](https://developer.qiniu.com/kodo/1284/list)
以下是代码:
class Auth
{
private $accessKey;
private $secretKey;
public $options;
public function __construct($accessKey, $secretKey, $options = null)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$defaultOptions = array(
'disableQiniuTimestampSignature' => null
);
if ($options == null) {
$options = $defaultOptions;
}
$this->options = array_merge($defaultOptions, $options);
}
//对数据进行编码
public function base64_urlSafeEncode($data)
{
$find = array('+', '/');
$replace = array('-', '_');
return str_replace($find, $replace, base64_encode($data));
}
public function sign($data)
{
$hmac = hash_hmac('sha1', $data, $this->secretKey, true);
return $this->accessKey . ':' . $this->base64_urlSafeEncode($hmac);
}
/**
* @param string $urlString
* @param string $method
* @param string $body
* @param null|Header $headers
*/
public function signQiniuAuthorization($urlString, $method = "GET", $body = "", $headers = null)
{
$url = parse_url($urlString);
if (!$url) {
return array(null, new \Exception("parse_url error"));
}
// append method, path and query
if ($method === "") {
$data = "GET ";
} else {
$data = $method . " ";
}
if (isset($url["path"])) {
$data .= $url["path"];
}
if (isset($url["query"])) {
$data .= "?" . $url["query"];
}
// append Host
$data .= "\n";
$data .= "Host: ";
if (isset($url["host"])) {
$data .= $url["host"];
}
if (isset($url["port"]) && $url["port"] > 0) {
$data .= ":" . $url["port"];
}
// try append content type
if ($headers != null && isset($headers["Content-Type"])) {
// append content type
$data .= "\n";
$data .= "Content-Type: " . $headers["Content-Type"];
}
// try append xQiniuHeaders
if ($headers != null) {
$headerLines = array();
$keyPrefix = "X-Qiniu-";
foreach ($headers as $k => $v) {
if (strlen($k) > strlen($keyPrefix) && strpos($k, $keyPrefix) === 0) {
array_push(
$headerLines,
$k . ": " . $v
);
}
}
if (count($headerLines) > 0) {
$data .= "\n";
sort($headerLines);
$data .= implode("\n", $headerLines);
}
}
// append body
$data .= "\n\n";
if (!is_null($body)
&& strlen($body) > 0
&& isset($headers["Content-Type"])
&& $headers["Content-Type"] != "application/octet-stream"
) {
$data .= $body;
}
return array($this->sign($data), null);
}
public function authorizationV2($url, $method, $body = null, $contentType = null)
{
$headers = '';
$result = array();
if ($contentType != null) {
$headers['Content-Type'] = $contentType;
$result['Content-Type'] = $contentType;
}
$signDate = gmdate('Ymd\THis\Z', time());
if ($this->options['disableQiniuTimestampSignature'] !== null) {
if (!$this->options['disableQiniuTimestampSignature']) {
$headers['X-Qiniu-Date'] = $signDate;
$result['X-Qiniu-Date'] = $signDate;
}
} elseif (getenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE")) {
if (strtolower(getenv("DISABLE_QINIU_TIMESTAMP_SIGNATURE")) !== "true") {
$headers['X-Qiniu-Date'] = $signDate;
$result['X-Qiniu-Date'] = $signDate;
}
} else {
$headers['X-Qiniu-Date'] = $signDate;
$result['X-Qiniu-Date'] = $signDate;
}
list($sign) = $this->signQiniuAuthorization($url, $method, $body, $headers);
$result['Authorization'] = 'Qiniu ' . $sign;
return $result;
}
}
//需要的两个key(秘钥)需要到七牛云后台获取
$accessKey = '';
$secretKey = '';
$Obj = new Auth($accessKey,$secretKey);
//根据文档描述这里只传入一个比传的参数bucket
$bucket = "";
$query = [
'bucket' => $bucket,
];
//定义curl请求的路径
$url = "http://rsf.qiniuapi.com/v2/list?".http_build_query($query);
//获取管理凭证
$res = $Obj->authorizationV2($url,'POST', null, 'application/x-www-form-urlencoded');
$Authorization = $res['Authorization'];
$date= $res['X-Qiniu-Date'];
//定义header头部
$header =[
'Content-Type:application/x-www-form-urlencoded',
'X-Qiniu-Date:'.$date,
'Authorization: '.$Authorization,
];
function http_post($sUrl, $aHeader){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$sResult = curl_exec($ch);
if($sError=curl_error($ch)){
die($sError);
}
curl_close($ch);
return $sResult;
}
$res = http_post($url,$header);
#此处的res就是结果了
版权声明:本文为ddliyoutang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。