-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e17e28d
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "mingyoung/guzzle-middleware", | ||
"description": "Some middleware for Guzzle.", | ||
"keywords": [ | ||
"guzzle" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ming Young", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"GuzzleMiddleware\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace GuzzleMiddleware; | ||
|
||
class AliyunSMS | ||
{ | ||
public function __construct(protected string $accessKeyId, protected string $accessKeySecret) {} | ||
|
||
public function __invoke($handler) | ||
{ | ||
return function ($request, $options) use ($handler) { | ||
$params = [ | ||
'AccessKeyId' => $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)); | ||
} | ||
} |