diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c09411 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/ +/composer.lock +/.idea +/demo.php \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c52e353 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 lushaoming + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b066cd8 --- /dev/null +++ b/README.md @@ -0,0 +1,85 @@ +## About Open Api + +​ Through the open interface, developers can easily and quickly access functions such as sending text messages or sending emails. + +## Existing features + +- Send SMS +- Send email + +## How to use? + +### Install + +```shell +composer repuire bryce/openapi +``` + +### Send SMS verification code + +```php + + * @date 2019/12/12 + */ +require_once 'vendor/autoload.php'; + +$sms = new \Bryce\OpenApi\Sms(); + +$sms->setClientId('Your client id'); +$sms->setClientKey('Your client key'); + +if ($sms->send('188****1019')) exit('Success'); +else exit('Failure'); +``` + +> - You should have permission to send sms +> - Each mobile phone number can only send 1 text message per minute, and up to 5 text messages per day. + +## Verify SMS Verification Code + +```php + + * @date 2019/12/12 + */ +require_once 'vendor/autoload.php'; + +$mailer = new \Bryce\OpenApi\Sms(); + +$sms->setClientId('Your client id'); +$sms->setClientKey('Your client key'); + +if ($mailer->verifyCode('188****1019', 434307)) exit('Success'); +else exit('Failure'); +``` + + + +## Send email + +```php + + * @date 2019/12/12 + */ +require_once 'vendor/autoload.php'; + +$mailer = new \Bryce\OpenApi\Mailer(); + +$mailer->setClientId('Your client id'); +$mailer->setClientKey('Your client key'); + +if ($mailer->send('To email', 'subject', 'body')) exit('Success'); +else exit('Failure'); +``` + +> - You should have permission to send mail +> - Sending attachments is currently not supported + +## How to get Client ID? + +[Get client ID now](http://core.lushaoming.site/apply) \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0320a4a --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "bryce/openapi", + "description": "Open API", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Bryce", + "email": "lushaoming6@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "php": ">=7.1.1" + }, + "autoload": { + "psr-4": { + "Bryce\\OpenApi\\": "src/Bryce/OpenApi" + } + } +} diff --git a/src/Bryce/OpenApi/BaseApi.php b/src/Bryce/OpenApi/BaseApi.php new file mode 100644 index 0000000..9ed19fe --- /dev/null +++ b/src/Bryce/OpenApi/BaseApi.php @@ -0,0 +1,35 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi; + +use Bryce\OpenApi\Common\Tools; + +class BaseApi +{ + protected $params = []; + protected $clientKey; + + public function __construct() + { + $this->params = [ + 'timestamp' => Tools::getCurrentTime(), + 'nonce' => Tools::createNonce(), + ]; + } + + public function setClientId($clientId) + { + $this->params['client_id'] = $clientId; + } + + public function setClientKey($key) + { + $this->clientKey = $key; + } + + +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Common/Config.php b/src/Bryce/OpenApi/Common/Config.php new file mode 100644 index 0000000..5fdcf66 --- /dev/null +++ b/src/Bryce/OpenApi/Common/Config.php @@ -0,0 +1,25 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi\Common; + +class Config +{ + const CONFIGS = [ + 'domain' => 'http://core.lushaoming.site', + 'permissions' => ['send_sms', 'send_email'], + 'sign_method' => 'md5', + 'send_email_url' => 'http://core.lushaoming.site/api/mailer/send', + 'send_sms_url' => 'http://core.lushaoming.site/api/sms/send-verify-code', + 'verify_sms_code_url' => 'http://core.lushaoming.site/api/sms/verify-code', + ]; + + public static function getConfigs() + { + return self::CONFIGS; + } +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Common/Tools.php b/src/Bryce/OpenApi/Common/Tools.php new file mode 100644 index 0000000..793ff3e --- /dev/null +++ b/src/Bryce/OpenApi/Common/Tools.php @@ -0,0 +1,131 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi\Common; + +class Tools +{ + + /** + * @return int + * @author Bryce + * @date 2019/12/12 + */ + public static function getCurrentTime() + { + return time(); + } + + /** + * @param int $length Nonce length, default 16 + * @return string + * @author Bryce + * @date 2019/12/12 + */ + public static function createNonce($length = 16): string + { + $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + $return_str = ''; + for ($i = 0; $i < $length; $i ++) { + $rant = mt_rand(0, mb_strlen($str) - 1); + $return_str .= mb_substr($str, $rant, 1); + } + return $return_str; + } + + + /** + * @param array $data Data involved in signing + * @param string $key Client secret, must be kept secret + * @return string + * @author Bryce + * @date 2019/12/12 + */ + public static function sign(array $data, string $key): string + { + if (isset($data['sign'])) unset($data['sign']); + + ksort($data); + $arr = []; + foreach ($data as $k => $v) { + $arr[] = "{$k}={$v}"; + } + + $str = implode('&', $arr); + $str .= "&key={$key}"; + + return strtoupper(md5($str)); + } + + /** + * @param array $params + * @return bool + * @author Bryce + * @date 2019/12/12 + */ + public static function checkCommonParams(array $params): bool + { + if (!isset($params['client_id']) + || !isset($params['timestamp']) + || !isset($params['nonce']) + || !isset($params['sign'])) + return false; + return true; + } + + /** + * @param array $params + * @return bool + * @author Bryce + * @date 2019/12/12 + */ + public static function checkSmsParams($params = []): bool + { + if (!isset($params['mobile'])) return false; + return true; + } + + public static function checkSmsVerifyData($params = []): bool + { + if (!isset($params['mobile']) || !isset($params['code'])) return false; + + return true; + } + + /** + * @param array $params + * @return bool + * @author Bryce + * @date 2019/12/12 + */ + public static function checkEmailParams($params = []): bool + { + if (!isset($params['to']) || !isset($params['subject']) || !isset($params['body'])) return false; + return true; + } + + public static function curlPost(string $url, array $params) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + + curl_setopt($ch, CURLOPT_POST, TRUE); + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + + $output = curl_exec($ch); + curl_close($ch); + return $output; + } + + public static function dealCurlResult($result) + { + return json_encode($result); + } +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Exception/MailException.php b/src/Bryce/OpenApi/Exception/MailException.php new file mode 100644 index 0000000..3a8c1fd --- /dev/null +++ b/src/Bryce/OpenApi/Exception/MailException.php @@ -0,0 +1,12 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi\Exception; + +class MailException extends \Exception +{ + +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Exception/SmsException.php b/src/Bryce/OpenApi/Exception/SmsException.php new file mode 100644 index 0000000..13c9fd7 --- /dev/null +++ b/src/Bryce/OpenApi/Exception/SmsException.php @@ -0,0 +1,12 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi\Exception; + +class SmsException extends \Exception +{ + +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Mailer.php b/src/Bryce/OpenApi/Mailer.php new file mode 100644 index 0000000..6c84189 --- /dev/null +++ b/src/Bryce/OpenApi/Mailer.php @@ -0,0 +1,29 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi; + +use Bryce\OpenApi\Common\Config; +use Bryce\OpenApi\Common\Tools; + +class Mailer extends BaseApi +{ + public function send(string $to, string $subject, string $body, $from = null) + { + $this->params['to'] = $to; + $this->params['subject'] = $subject; + $this->params['body'] = $body; + if (!is_null($from)) $this->params['from'] = $from; + + $this->params['sign'] = Tools::sign($this->params, $this->clientKey); + + $res = Tools::curlPost(Config::CONFIGS['send_email_url'], $this->params); + + $res = Tools::dealCurlResult($res); + + return $res->status === 0; + } +} \ No newline at end of file diff --git a/src/Bryce/OpenApi/Sms.php b/src/Bryce/OpenApi/Sms.php new file mode 100644 index 0000000..ac4652f --- /dev/null +++ b/src/Bryce/OpenApi/Sms.php @@ -0,0 +1,41 @@ + + * @date 2019/12/12 + */ +namespace Bryce\OpenApi; + +use Bryce\OpenApi\Common\Config; +use Bryce\OpenApi\Common\Tools; + +class Sms extends BaseApi +{ + public function send(string $mobile) + { + $this->params['mobile'] = $mobile; + + $this->params['sign'] = Tools::sign($this->params, $this->clientKey); + + $res = Tools::curlPost(Config::CONFIGS['send_sms_url'], $this->params); + + $res = Tools::dealCurlResult($res); + + return $res->status === 0; + } + + public function verifyCode(string $mobile, $code) + { + $this->params['mobile'] = $mobile; + $this->params['code'] = $code; + + $this->params['sign'] = Tools::sign($this->params, $this->clientKey); + var_dump($this->params); + $res = Tools::curlPost(Config::CONFIGS['verify_sms_code_url'], $this->params); + + var_dump($res); + $res = Tools::dealCurlResult($res); + + return $res->status === 0; + } +} \ No newline at end of file