Skip to content

Commit

Permalink
Enable send sms and email
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaoming committed Dec 12, 2019
0 parents commit b6f54ef
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/composer.lock
/.idea
/demo.php
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php
/**
* @author Bryce<lushaoming6@gmail.com>
* @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
<?php
/**
* @author Bryce<lushaoming6@gmail.com>
* @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
<?php
/**
* @author Bryce<lushaoming6@gmail.com>
* @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)
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
35 changes: 35 additions & 0 deletions src/Bryce/OpenApi/BaseApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Class BaseApi
* @author Bryce<lushaoming6@gmail.com>
* @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;
}


}
25 changes: 25 additions & 0 deletions src/Bryce/OpenApi/Common/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* All configs.
* Do not edit this file.
* @author Bryce<lushaoming6@gmail.com>
* @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;
}
}
131 changes: 131 additions & 0 deletions src/Bryce/OpenApi/Common/Tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Class Tools
* Common methods
* @author 卢绍明<lusm@sz-bcs.com.cn>
* @date 2019/12/12
*/
namespace Bryce\OpenApi\Common;

class Tools
{

/**
* @return int
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/12
*/
public static function getCurrentTime()
{
return time();
}

/**
* @param int $length Nonce length, default 16
* @return string
* @author Bryce<lushaoming6@gmail.com>
* @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<lushaoming6@gmail.com>
* @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<lushaoming6@gmail.com>
* @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<lushaoming6@gmail.com>
* @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<lushaoming6@gmail.com>
* @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);
}
}
12 changes: 12 additions & 0 deletions src/Bryce/OpenApi/Exception/MailException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Class ${NAME}
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/12
*/
namespace Bryce\OpenApi\Exception;

class MailException extends \Exception
{

}
12 changes: 12 additions & 0 deletions src/Bryce/OpenApi/Exception/SmsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Class SmsException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/12
*/
namespace Bryce\OpenApi\Exception;

class SmsException extends \Exception
{

}
29 changes: 29 additions & 0 deletions src/Bryce/OpenApi/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Class Mailer
* @author Bryce<lushaoming6@gmail.com>
* @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;
}
}
Loading

0 comments on commit b6f54ef

Please sign in to comment.