SignatureHelper.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Aliyun\DySDKLite;
  3. /**
  4. * 签名助手 2017/11/19
  5. *
  6. * Class SignatureHelper
  7. */
  8. class SignatureHelper {
  9. /**
  10. * 生成签名并发起请求
  11. *
  12. * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
  13. * @param $accessKeySecret string AccessKeySecret
  14. * @param $domain string API接口所在域名
  15. * @param $params array API具体参数
  16. * @param $security boolean 使用https
  17. * @param $method boolean 使用GET或POST方法请求,VPC仅支持POST
  18. * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
  19. */
  20. public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false, $method='POST') {
  21. $apiParams = array_merge(array (
  22. "SignatureMethod" => "HMAC-SHA1",
  23. "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
  24. "SignatureVersion" => "1.0",
  25. "AccessKeyId" => $accessKeyId,
  26. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  27. "Format" => "JSON",
  28. ), $params);
  29. ksort($apiParams);
  30. $sortedQueryStringTmp = "";
  31. foreach ($apiParams as $key => $value) {
  32. $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
  33. }
  34. $stringToSign = "${method}&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
  35. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
  36. $signature = $this->encode($sign);
  37. $url = ($security ? 'https' : 'http')."://{$domain}/";
  38. try {
  39. $content = $this->fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
  40. return json_decode($content);
  41. } catch( \Exception $e) {
  42. return false;
  43. }
  44. }
  45. private function encode($str)
  46. {
  47. $res = urlencode($str);
  48. $res = preg_replace("/\+/", "%20", $res);
  49. $res = preg_replace("/\*/", "%2A", $res);
  50. $res = preg_replace("/%7E/", "~", $res);
  51. return $res;
  52. }
  53. private function fetchContent($url, $method, $body) {
  54. $ch = curl_init();
  55. if($method == 'POST') {
  56. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  57. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  58. } else {
  59. $url .= '?'.$body;
  60. }
  61. curl_setopt($ch, CURLOPT_URL, $url);
  62. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  65. "x-sdk-client" => "php/2.0.0"
  66. ));
  67. if(substr($url, 0,5) == 'https') {
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  70. }
  71. $rtn = curl_exec($ch);
  72. if($rtn === false) {
  73. // 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
  74. // 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
  75. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  76. }
  77. curl_close($ch);
  78. return $rtn;
  79. }
  80. }