php上传文件到七牛云


define('ApiUrl', 'https://up-z1.qiniup.com');
define('AccessKey', 'AK');
define('SecretKey', 'SK');
define('Bucket', 'bucket');

main();

function main() {
    Upload('01.zip', '03.zip');
}

function Upload($file, $path) {

    $data = [
        'token' => getSign($path),
        'file' => new CURLFile(realpath($file)),
        'key' => $path
    ];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, ApiUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $data = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);

    return $data;
}

function Encode($s) {
    return str_replace(['+', '/'], ['-', '_'], base64_encode($s));
}

function getSign($path) {
    $toSign = Encode(json_encode(['scope' => Bucket . ':' . $path, 'deadline' => time() + 1200]));
    $sign = Encode(hash_hmac('sha1', $toSign, SecretKey, true));
    return sprintf('%s:%s:%s', AccessKey, $sign, $toSign);
}