require 'DocConf.php'; require 'BceSign.php'; global $g_doc_configs; global $my_credentials; //发布文档 $documentId = "doc-ggwsf1frcsqxfdb"; $host = $g_doc_configs['endpoint']; $path = "/v2/document/" . $documentId; $method = "PUT"; $parms = array("publish" => ""); date_default_timezone_set('UTC'); $timestamp = date("Y-m-d") . "T" . date("H:i:s") . "Z"; $Authorization = getSigner($host, $method, $path, $parms, $timestamp); $httputil = new HttpUtil(); $parms = $httputil->getCanonicalQueryString($parms); $url = "http://" . $host . $path . "?" . $parms; $head = array("Content-Type:text/plain", "Authorization:{$Authorization}", "x-bce-date:{$timestamp}"); $data = array(); $data_string = json_encode($data); //发送HTTP请求 $curlp = curl_init(); curl_setopt($curlp, CURLOPT_URL, $url); curl_setopt($curlp, CURLOPT_HTTPHEADER, $head); //curl_setopt($curlp, CURLOPT_POSTFIELDS, $data_string); //curl_setopt($curlp, CURLOPT_POST, 0); curl_setopt($curlp, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curlp, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curlp); curl_close($curlp); echo $output;
function getSigner($host, $httpMethod, $path, $parms, $timestamp) { // date_default_timezone_set('UTC'); $AK = "f7306cacee2942c98a715ddc0d5b3878"; //替换成你STS获取的AK $SK = "2cf0173d82624502bbd7150c03b5b236"; //替换成你STS获取的SK $expirationPeriodInSeconds = "3600"; //$timestamp = date("Y-m-d")."T".date("H:i:s")."Z"; $authStringPrefix = "bce-auth-v1" . "/" . $AK . "/" . $timestamp . "/" . $expirationPeriodInSeconds; $SigningKey = hash_hmac('SHA256', $authStringPrefix, $SK); $CanonicalHeaders1 = "host;" . "x-bce-date"; $CanonicalHeaders2 = "host:" . $host . "\n" . "x-bce-date:" . urlencode($timestamp); // //print "CanonicalHeaders2:" . $CanonicalHeaders2 . "\n"; $httputil = new HttpUtil(); $CanonicalString = $httputil->getCanonicalQueryString($parms); $CanonicalURI = $path; $Method = $httpMethod; $CanonicalRequest = $Method . "\n" . $CanonicalURI . "\n" . $CanonicalString . "\n" . $CanonicalHeaders2; //print "CanonicalRequest:" . $CanonicalRequest . "\n"; $Signature = hash_hmac('SHA256', $CanonicalRequest, $SigningKey); //print "Signature:" . $Signature . "\n"; $Authorization = "bce-auth-v1/{$AK}/" . $timestamp . "/{$expirationPeriodInSeconds}/{$CanonicalHeaders1}/{$Signature}"; print "Authorization:" . $Authorization . "\n"; return $Authorization; //$Authorization: //bce-auth-v1/97659fdf3ab547e2a9f71dfcd6659a8b2015-08-27T03:50:33Z/3600/content-length;host;x-bce-date;/5290e5669befd7f44dd362e00c2a4cc5edbee0bc925bceb15e8a9fcb91389201 }
public function sign(array $credentials, $httpMethod, $path, $headers, $params, $options = array()) { if (!isset($options[SignOption::EXPIRATION_IN_SECONDS])) { $expirationInSeconds = SignOption::DEFAULT_EXPIRATION_IN_SECONDS; } else { $expirationInSeconds = $options[SignOption::EXPIRATION_IN_SECONDS]; } $accessKeyId = $credentials['ak']; $secretAccessKey = $credentials['sk']; //Notice: timestamp should be UTC if (!isset($options[SignOption::TIMESTAMP])) { $timestamp = new \DateTime(); } else { $timestamp = $options[SignOption::TIMESTAMP]; } $timestamp->setTimezone(new \DateTimeZone("UTC")); //Generate authString $authString = SampleSigner::BCE_AUTH_VERSION . '/' . $accessKeyId . '/' . $timestamp->format("Y-m-d\\TH:i:s\\Z") . '/' . $expirationInSeconds; //Generate sign key with auth-string and SK using SHA-256 $signingKey = hash_hmac('sha256', $authString, $secretAccessKey); //Generate canonical uri $canonicalURI = HttpUtil::getCanonicalURIPath($path); //Generate canonical query string $canonicalQueryString = HttpUtil::getCanonicalQueryString($params); //Fill headersToSign to specify which header do you want to sign $headersToSign = null; if (isset($options[SignOption::HEADERS_TO_SIGN])) { $headersToSign = $options[SignOption::HEADERS_TO_SIGN]; } //Generate canonical headers $canonicalHeader = HttpUtil::getCanonicalHeaders(SampleSigner::getHeadersToSign($headers, $headersToSign)); $signedHeaders = ''; if ($headersToSign !== null) { $signedHeaders = strtolower(trim(implode(";", array_keys($headersToSign)))); } //Generate canonical request $canonicalRequest = "{$httpMethod}\n{$canonicalURI}\n" . "{$canonicalQueryString}\n{$canonicalHeader}"; //Generate signature with canonical request and sign key using SHA-256 $signature = hash_hmac('sha256', $canonicalRequest, $signingKey); //.Catenate result string $authorizationHeader = "{$authString}/{$signedHeaders}/{$signature}"; return $authorizationHeader; }