<?php
error_reporting(E_ALL^E_NOTICE^E_WARNING);
$longopts = [
	"key:",
	"callback:",
	"signature:",
	"OSSAccessKeyId:",
	"policy:",
	"success_action_status::",
	"file:",
	"url:",
];
$opt = getopt("shortopt",$longopts);
if(!$opt['success_action_status']){
	$opt['success_action_status'] = 200;
}

$post_data = [
	'key'=>$opt['key'],
	'callback'=>$opt['callback'],
	'signature'=>$opt['signature'],
	'OSSAccessKeyId'=>$opt['OSSAccessKeyId'],
	'policy'=>$opt['policy'],
	'success_action_status'=>$opt['success_action_status'],
	'file'=>new \CURLFile($opt['file']),
];
$ch = curl_init();
$encode = mb_detect_encoding($post_data['key'], array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
$post_data['key'] = iconv($encode,'utf-8',$post_data['key']);
//$str_encode = mb_convert_encoding($post_data['key'], $encode,'UTF-8'); 

//$url =  $opt['url'];//'http://securebox.oss-cn-beijing.aliyuncs.com';
    curl_setopt($ch, CURLOPT_URL, $opt['url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	$ssl = preg_match('/^https:\/\//i',$opt['url']) ? TRUE : FALSE;
    if($ssl){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
	$output = curl_exec($ch);
	$info = curl_getinfo($ch);
	$error = curl_error($ch);
	curl_close($ch);
	if($info['http_code'] == 0){
		echo "{\"Status\": \"http_code error0\"}";
	}else{
		echo $output;
	}