php上传图片到七牛云(base64)

/**
     * 上传图片到七牛云
     * @param $accessKey
     * @param $secretKey
     * @param $bucket
     * @param $filePath
     * @param $str
     * @return bool
     */
    public static function qiniu($accessKey, $secretKey, $bucket,$filePath, $str)
    {
        $auth = new Auth($accessKey, $secretKey);

        //生成图片key
        $rand = rand(1111, 9999);
        $now = time();
        $name = $filePath . $now . $rand;
        $key = base64_encode($name);

        $token = $auth->uploadToken($bucket);
        $num = strpos($str, ',');
        $image = substr($str, $num + 1);
        $res = self::phpCurlImg('http://up-z1.qiniup.com/putb64/-1/key/' . $key, $image, $token);

        $res = json_decode($res,true);

        if (!empty($res['key']) && $res['key'] == $name) {
            return $res['key'];
        } else {
            return false;
        }

    }

    private static function phpCurlImg($remote_server, $post_string, $upToken)
    {
        $headers = array();
        $headers[] = 'Content-Type:application/octet-stream';
        $headers[] = 'Authorization:UpToken ' . $upToken;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

 

你可能感兴趣的:(php)