七牛云 PHP SDK服务器鉴权失败!参数解释

时间:2024-10-19 22:07:14

昨天搞了一下午,用7牛官方的SDK demo

1.上传凭证

$policy = array(
'callbackUrl' => 'http://api.example.com/qiniu/upload/callback',
'callbackBody' => '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}',
'callbackBodyType' => 'application/json'
);
$upToken = $auth->uploadToken($bucket, null, $expires, $policy, true);
print($upToken . "\n");

2.回调鉴权方法

<?php
require_once __DIR__ . '/../autoload.php';
use Qiniu\Auth;
$accessKey = getenv('QINIU_ACCESS_KEY');
$secretKey = getenv('QINIU_SECRET_KEY');
$bucket = getenv('QINIU_TEST_BUCKET');
$auth = new Auth($accessKey, $secretKey);
//获取回调的body信息
$callbackBody = file_get_contents('php://input');
//回调的contentType
$contentType = 'application/x-www-form-urlencoded';
//回调的签名信息,可以验证该回调是否来自七牛
$authorization = $_SERVER['HTTP_AUTHORIZATION'];
//七牛回调的url,具体可以参考:http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
$url = 'http://172.30.251.210/upload_verify_callback.php';
$isQiniuCallback = $auth->verifyCallback($contentType, $authorization, $url, $callbackBody);
if ($isQiniuCallback) {
$resp = array('ret' => 'success');
} else {
$resp = array('ret' => 'failed');
}
echo json_encode($resp);

服务器已经收到信息 了验证总是failed!

关键说下:

$url 参数是你的服务器 回调的地址:就是上面生成token 你填的回调服务器 地址就是下面的:callbackUrl

$policy = array(
'callbackUrl' => 'http://api.example.com/qiniu/upload/callback',
'callbackBody' => '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}',
'callbackBodyType' => 'application/json'
);

MADE 文档里都不解释下 各个参数的含义

我还以为是 “http://172.30.251.210/upload_verify_callback.php” 是固定值?