Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyoung committed Feb 22, 2021
0 parents commit e17e28d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions composer.json
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/"
}
}
}
41 changes: 41 additions & 0 deletions src/AliyunSMS.php
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));
}
}

0 comments on commit e17e28d

Please sign in to comment.