-
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
VAGH
committed
Feb 24, 2020
1 parent
ceab2e0
commit 2bcdb70
Showing
2 changed files
with
153 additions
and
129 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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
# hello-my-honey | ||
定期给女朋友发送问候短信(直男版) | ||
|
||
## 安装本地环境 | ||
|
||
将项目部署在你的服务器上后,运行以下代码安装库。 | ||
```shell | ||
$ composer install | ||
``` | ||
|
||
## 使用方法 |
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 |
---|---|---|
|
@@ -6,61 +6,68 @@ | |
use AlibabaCloud\Client\Exception\ServerException; | ||
use GuzzleHttp\Client; | ||
|
||
// 阿里云接口秘钥信息 | ||
const accessKeyId = '*********'; | ||
const accessKeySecret = '*********'; | ||
|
||
// 新知天气秘钥信息 | ||
const seniverseKey = '******'; | ||
const seniverseUid = '******'; | ||
|
||
// 初始化阿里云 SDK 实例 | ||
// 参数分别是你阿里云的 accessKeyId 和 accessKeySecret | ||
AlibabaCloud::accessKeyClient('**********', '**********') | ||
->regionId('cn-hangzhou') | ||
->asDefaultClient(); | ||
AlibabaCloud::accessKeyClient(accessKeyId, accessKeySecret) | ||
->regionId('cn-hangzhou') | ||
->asDefaultClient(); | ||
|
||
try { | ||
// 你对小可爱的昵称 有多少就列多少 | ||
$name_arr = [ | ||
'小彭彭', | ||
'小傻瓜', | ||
'彭大长腿', | ||
'小桂花', | ||
'小短腿', | ||
'小瑀瑀', | ||
'彭怼怼', | ||
'LiquidNitrogen', | ||
'彭老板', | ||
'老彭', | ||
$name_arr = [ | ||
'小彭彭', | ||
'小傻瓜', | ||
'彭大长腿', | ||
'小桂花', | ||
'小短腿', | ||
'小瑀瑀', | ||
'彭怼怼', | ||
'LiquidNitrogen', | ||
'彭老板', | ||
'老彭', | ||
'搞黄彭', | ||
]; | ||
|
||
// 随机取一个昵称 | ||
$name_str = $name_arr[array_rand($name_arr)]; | ||
// format lucky number between 0~9 | ||
$number_str = rand(0,9); | ||
$sms_content = [ | ||
'name' => $name_str, | ||
'day' => getDays(), | ||
'weather' => getWeather() .' '. getAirNow(), | ||
'number' => $number_str, | ||
'sender_name' => getFromName(), | ||
]; | ||
]; | ||
|
||
// 随机取一个昵称 | ||
$name_str = $name_arr[array_rand($name_arr)]; | ||
// format lucky number between 0~9 | ||
$number_str = rand(0, 9); | ||
|
||
$sms_content = [ | ||
'name' => $name_str, | ||
'day' => getDays(), | ||
'weather' => getWeather() . ' ' . getAirNow(), | ||
'number' => $number_str, | ||
'sender_name' => getFromName(), | ||
]; | ||
|
||
$result = AlibabaCloud::rpc() | ||
->product('Dysmsapi') | ||
->scheme('https') | ||
// use current version | ||
->version('2017-05-25') | ||
->action('SendSms') | ||
->method('POST') | ||
->host('dysmsapi.aliyuncs.com') | ||
->options([ | ||
'query' => [ | ||
'RegionId' => "cn-hangzhou", | ||
// 以下短信模板请自己申请 | ||
'PhoneNumbers' => "17627828449", | ||
// 签名也需要申请 | ||
'SignName' => "余大能耐", | ||
'TemplateCode' => "SMS_176539584", | ||
'TemplateParam' => json_encode($sms_content), | ||
], | ||
]) | ||
->request(); | ||
->product('Dysmsapi') | ||
->scheme('https') | ||
// use current version | ||
->version('2017-05-25') | ||
->action('SendSms') | ||
->method('POST') | ||
->host('dysmsapi.aliyuncs.com') | ||
->options([ | ||
'query' => [ | ||
'RegionId' => "cn-hangzhou", | ||
// 以下短信模板请自己申请 | ||
'PhoneNumbers' => "17627828449", | ||
// 签名也需要申请 | ||
'SignName' => "余大能耐", | ||
'TemplateCode' => "SMS_176539584", | ||
'TemplateParam' => json_encode($sms_content), | ||
], | ||
]) | ||
->request(); | ||
print_r($result->toArray()); | ||
} catch (ClientException $e) { | ||
echo $e->getErrorMessage() . PHP_EOL; | ||
|
@@ -70,31 +77,28 @@ | |
|
||
/** | ||
* 获取天气信息 | ||
* https://www.seniverse.com/ | ||
* @return string | ||
* @author yuzhihao <[email protected]> | ||
* @since 2020/2/24 | ||
*/ | ||
function getWeather() | ||
{ | ||
$location = "Beijing"; | ||
$key = "******"; | ||
$uid = "******"; | ||
$keyname = "ts=".time()."&ttl=300&uid=".$uid; | ||
// 使用 HMAC-SHA1 方式,以 API 密钥(key)对上一步生成的参数字符串(raw)进行加密 | ||
$sig = base64_encode(hash_hmac('sha1', $keyname, $key, true)); | ||
// 将上一步生成的加密结果用 base64 编码,并做一个 urlencode,得到签名 sig | ||
$signedkeyname = $keyname."&sig=".urlencode($sig); | ||
$url = "https://api.seniverse.com/v3/weather/daily.json?location=".$location."&days=1&".$signedkeyname; | ||
$client = new Client(); | ||
$response = $client->get($url); | ||
$body = $response->getBody()->getContents(); | ||
|
||
$result = json_decode($body, true); | ||
$daily = $result['results'][0]['daily'][0]; | ||
|
||
$str = $daily['text_day'].'('.$daily['low'].'~'.$daily['high'].'℃)'; | ||
|
||
return $str; | ||
// 更改为你要获取天气的城市名称 | ||
$location = "Beijing"; | ||
|
||
$signed_key_name = getSignedKeyName(); | ||
$url = "https://api.seniverse.com/v3/weather/daily.json?location=" . $location . "&days=1&" . $signed_key_name; | ||
$client = new Client(); | ||
$response = $client->get($url); | ||
$body = $response->getBody()->getContents(); | ||
|
||
$result = json_decode($body, true); | ||
$daily = $result['results'][0]['daily'][0]; | ||
|
||
$str = $daily['text_day'] . '(' . $daily['low'] . '~' . $daily['high'] . '℃)'; | ||
|
||
return $str; | ||
} | ||
|
||
/** | ||
|
@@ -105,50 +109,46 @@ function getWeather() | |
*/ | ||
function getAirNow() | ||
{ | ||
$location = "Beijing"; | ||
$key = "*******"; | ||
$uid = "*******"; | ||
$keyname = "ts=".time()."&ttl=300&uid=".$uid; | ||
// 使用 HMAC-SHA1 方式,以 API 密钥(key)对上一步生成的参数字符串(raw)进行加密 | ||
$sig = base64_encode(hash_hmac('sha1', $keyname, $key, true)); | ||
// 将上一步生成的加密结果用 base64 编码,并做一个 urlencode,得到签名 sig | ||
$signedkeyname = $keyname."&sig=".urlencode($sig); | ||
$url = "https://api.seniverse.com/v3/life/suggestion.json?location=".$location."&language=zh-Hans&".$signedkeyname; | ||
$client = new Client(); | ||
$response = $client->get($url); | ||
$body = $response->getBody()->getContents(); | ||
$result = json_decode($body, true); | ||
|
||
$air_str = $result['results'][0]['suggestion']['dressing']; | ||
|
||
switch ($air_str['brief']) { | ||
case '炎热': | ||
$mess = '外面太热啦!'; | ||
break; | ||
case '热': | ||
$mess = '外面比较热~'; | ||
break; | ||
case '舒适': | ||
$mess = '天气太好啦!'; | ||
break; | ||
case '较舒适': | ||
$mess = '气温还行~'; | ||
break; | ||
case '较冷': | ||
$mess = '较冷别感冒~'; | ||
break; | ||
case '冷': | ||
$mess = '很冷多穿衣服~'; | ||
break; | ||
case '寒冷': | ||
$mess = '超冷一定穿厚些~'; | ||
break; | ||
default: | ||
$mess = ''; | ||
break; | ||
} | ||
|
||
return $mess; | ||
// 更改为你要获取天气的城市名称 | ||
$location = "Beijing"; | ||
|
||
$signed_key_name = getSignedKeyName(); | ||
$url = "https://api.seniverse.com/v3/life/suggestion.json?location=" . $location . "&language=zh-Hans&" . $signed_key_name; | ||
$client = new Client(); | ||
$response = $client->get($url); | ||
$body = $response->getBody()->getContents(); | ||
$result = json_decode($body, true); | ||
|
||
$air_str = $result['results'][0]['suggestion']['dressing']; | ||
|
||
switch ($air_str['brief']) { | ||
case '炎热': | ||
$mess = '外面太热啦!'; | ||
break; | ||
case '热': | ||
$mess = '外面比较热~'; | ||
break; | ||
case '舒适': | ||
$mess = '天气太好啦!'; | ||
break; | ||
case '较舒适': | ||
$mess = '气温还行~'; | ||
break; | ||
case '较冷': | ||
$mess = '较冷别感冒~'; | ||
break; | ||
case '冷': | ||
$mess = '很冷多穿衣服~'; | ||
break; | ||
case '寒冷': | ||
$mess = '超冷一定穿厚些~'; | ||
break; | ||
default: | ||
$mess = ''; | ||
break; | ||
} | ||
|
||
return $mess; | ||
} | ||
|
||
/** | ||
|
@@ -160,8 +160,8 @@ function getAirNow() | |
function getDays() | ||
{ | ||
// 在一起的时间 | ||
$d1 = strtotime("2019-10-30 00:00:00"); | ||
return ceil((time()-$d1)/60/60/24) - 1; | ||
$d1 = strtotime("2019-10-30 00:00:00"); | ||
return ceil((time() - $d1) / 60 / 60 / 24) - 1; | ||
} | ||
|
||
/** | ||
|
@@ -172,20 +172,35 @@ function getDays() | |
*/ | ||
function getFromName() | ||
{ | ||
$day = getDays(); | ||
|
||
if ($day == 2) { | ||
return '余大长腿'; | ||
} | ||
|
||
$name_arr = [ | ||
'余大长腿', | ||
'余壮壮', | ||
'余大能耐', | ||
'爸爸', | ||
'余老板', | ||
'老余', | ||
]; | ||
|
||
return $name_arr[array_rand($name_arr)]; | ||
$day = getDays(); | ||
|
||
if ($day == 2) { | ||
return '余大长腿'; | ||
} | ||
|
||
$name_arr = [ | ||
'余大长腿', | ||
'余壮壮', | ||
'余大能耐', | ||
'爸爸', | ||
'余老板', | ||
'老余', | ||
]; | ||
|
||
return $name_arr[array_rand($name_arr)]; | ||
} | ||
|
||
/** | ||
* 获取新知天气签名 | ||
* @return string | ||
* @author yuzhihao <[email protected]> | ||
* @since 2020/2/24 | ||
*/ | ||
function getSignedKeyName() | ||
{ | ||
$key_name = "ts=" . time() . "&ttl=300&uid=" . seniverseUid; | ||
// 使用 HMAC-SHA1 方式,以 API 密钥(key)对上一步生成的参数字符串(raw)进行加密 | ||
$sign = base64_encode(hash_hmac('sha1', $key_name, seniverseKey, true)); | ||
// 将上一步生成的加密结果用 base64 编码,并做一个 urlencode,得到签名 sig | ||
return $key_name . "&sig=" . urlencode($sign); | ||
} |