From e17e28d6e6c0e76f6bd22ed2f3e076ef2f92951e Mon Sep 17 00:00:00 2001 From: Ming Young Date: Mon, 22 Feb 2021 11:57:43 +0800 Subject: [PATCH] wip --- composer.json | 22 ++++++++++++++++++++++ src/AliyunSMS.php | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 composer.json create mode 100644 src/AliyunSMS.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0a098a6 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "mingyoung/guzzle-middleware", + "description": "Some middleware for Guzzle.", + "keywords": [ + "guzzle" + ], + "license": "MIT", + "authors": [ + { + "name": "Ming Young", + "email": "mingyoungcheung@gmail.com" + } + ], + "require": { + "php": "^8.0" + }, + "autoload": { + "psr-4": { + "GuzzleMiddleware\\": "src/" + } + } +} diff --git a/src/AliyunSMS.php b/src/AliyunSMS.php new file mode 100644 index 0000000..e813207 --- /dev/null +++ b/src/AliyunSMS.php @@ -0,0 +1,41 @@ + $this->accessKeyId, + 'Format' => 'JSON', + 'SignatureMethod' => 'HMAC-SHA1', + 'SignatureVersion' => '1.0', + 'SignatureNonce' => uniqid(), + 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'), + 'Action' => 'SendSms', + 'Version' => '2017-05-25', + ]; + + parse_str($request->getUri()->getQuery(), $query); + + $params = array_merge($params, $query); + $params['Signature'] = $this->generateSign($params); + + $request = $request->withUri($request->getUri()->withQuery(http_build_query($params))); + + return $handler($request, $options); + }; + } + + protected function generateSign($params) + { + ksort($params); + $stringToSign = 'GET&%2F&'.urlencode(http_build_query($params, null, '&', PHP_QUERY_RFC3986)); + + return base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret.'&', true)); + } +}