Skip to content

Commit

Permalink
移除多余文件
Browse files Browse the repository at this point in the history
  • Loading branch information
VAGH committed Feb 24, 2020
1 parent ceab2e0 commit 2bcdb70
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 129 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# hello-my-honey
定期给女朋友发送问候短信(直男版)

## 安装本地环境

将项目部署在你的服务器上后,运行以下代码安装库。
```shell
$ composer install
```

## 使用方法
273 changes: 144 additions & 129 deletions sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

0 comments on commit 2bcdb70

Please sign in to comment.