给女友的微信消息推送

  • 一、注册百度云账号
  • 1.搜索云函数
  • 2.立即体验
  • 3.创建函数
  • 4.选择模板
  • 5.进入代码编辑页
  • 二、微信公众号测试平台
  • 1.复制appID和appsecret到php代码里进行替换(单引号不能丢)
  • 2.创建模板消息
  • 3.填写好标题后提交
  • 4.将模板id复制到代码里
  • 5.然后让你女朋友关注这个微信公众号测试号,获取她的微信号,然后复制到代码里
  • 6.修改为自己日期
  • 7.修改为自己的城市
  • 8.代码修改完保存 测试 执行
  • 9.测试成功
  • 三、定时运行php


一、注册百度云账号


订阅subscribe expires_json

1.搜索云函数

订阅subscribe expires_json_02

2.立即体验

订阅subscribe expires_json_03

3.创建函数

订阅subscribe expires_订阅subscribe expires_04

4.选择模板

空白函数,函数名称自定义,运行时选 PHP7.2(其他默认),下一步,提交。

订阅subscribe expires_php_05

订阅subscribe expires_json_06

5.进入代码编辑页

订阅subscribe expires_php_07

订阅subscribe expires_订阅subscribe expires_08

<?php
$appId = 'ghjdghjnghm'; //对应自己的appId
$appSecret = 'rtyhrtehrthrthtrhtr'; //对应自己的appSecret
$wxgzhurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$access_token_Arr = https_request($wxgzhurl);
$access_token = json_decode($access_token_Arr, true);
$ACCESS_TOKEN = $access_token['access_token']; //ACCESS_TOKEN

// 什么时候恋爱的(格式别错)
$lovestart = strtotime('0000-00-00');
$end = time();
$love = ceil(($end - $lovestart) / 86400);

// 下一个生日是哪一天(格式别错)
$birthdaystart = strtotime('0000-00-00');
$end = time();
$diff_days = ($birthdaystart - $end);
$birthday = (int)($diff_days/86400);
$birthday = str_replace("-", "", $birthday);

$tianqiurl = 'https://api.youguo56.com/api/wether/?city=兰州'; //修改为自己的测试
$tianqiapi = https_request($tianqiurl);
$tianqi = json_decode($tianqiapi, true);
$qinghuaqiurl = 'https://api.youguo56.com/api/chp/'; 
$qinghuaapi = https_request($qinghuaqiurl);
#$qinghua = json_decode($qinghuaapi, true);

$yjh = ''; 
$touser = 'fgnhfdgnfghn';  //这个填你女朋友的openid
$data = array(
    'touser' => $touser,
    'template_id' => "fdbnnfgdnfgnfghng", //改成自己的模板id,在微信后台模板消息里查看
    'data' => array(
        'first' => array(
            'value' => $yjh,
            'color' => "#00BFFF"
        ),
       'keyword8' => array(
            'value' => $tianqi['data']['forecast']['0']['date'],
            'color' => "#1E90FF"
        ),
        'keyword1' => array(
            'value' => $tianqi['data']['city'],
            'color' => "#7B68EE"
        ),
        'keyword2' => array(
            'value' => $tianqi['data']['forecast']['0']['type'],
            'color' => "#FFA07A"
        ),
        'keyword3' => array(
            'value' => $tianqi['data']['forecast']['0']['high'],
            'color' => "#98FB98"
        ),
        'keyword4' => array(
            'value' => $tianqi['data']['forecast']['0']['low'],
            'color' => "#F4A460"
        ),
        'keyword5' => array(
            'value' => $love . '天',
            'color' => "#FA8072"
        ),
        'keyword6' => array(
            'value' => $birthday . '天',
            'color' => "#F5DEB3"
        ),       
        'keyword7' => array(
            'value' => $tianqi['data']['ganmao'],
            'color' => "#FFB6C1"
        ),
        'remark' => array(
           'value' => $qinghuaapi,
            'color' => "#6495ED"
        ),
    )
);

$json_data = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$res = https_request($url, urldecode($json_data));
$res = json_decode($res, true);

if ($res['errcode'] == 0 && $res['errcode'] == "ok") {
    echo "发送成功!<br/>";
}else {
        echo "发送失败!请检查代码!!!<br/>";
}
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
function handler($context) {

# 啥都没有   就是为了填执行失败的坑
    # return $ret;
}

二、微信公众号测试平台

注册地址: http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

订阅subscribe expires_微信_09

1.复制appID和appsecret到php代码里进行替换(单引号不能丢)

订阅subscribe expires_php_10

2.创建模板消息

订阅subscribe expires_json_11


将下面的代码复制到模板内容里

今天是{{keyword8.DATA}}
城市:{{keyword1.DATA}}
天气:{{keyword2.DATA}}
最高温度:{{keyword3.DATA}}
最低温度:{{keyword4.DATA}}
今天是我们在一起的第{{keyword5.DATA}}
距离宝贝的生日还有{{keyword6.DATA}}
今日建议:{{keyword7.DATA}}
每日一句:
{{remark.DATA}}

3.填写好标题后提交

订阅subscribe expires_json_12

4.将模板id复制到代码里

订阅subscribe expires_微信_13


订阅subscribe expires_php_14

5.然后让你女朋友关注这个微信公众号测试号,获取她的微信号,然后复制到代码里

订阅subscribe expires_微信_15


订阅subscribe expires_订阅subscribe expires_16

6.修改为自己日期

订阅subscribe expires_php_17

7.修改为自己的城市

订阅subscribe expires_订阅subscribe expires_18

8.代码修改完保存 测试 执行

订阅subscribe expires_json_19


订阅subscribe expires_php_20

9.测试成功

订阅subscribe expires_微信公众号_21

三、定时运行php

订阅subscribe expires_php_22

定时触发器以UTC时间运行,即北京时间减去8个小时

订阅subscribe expires_订阅subscribe expires_23

这里的计划表达式为 cron(0 23 * * ?)

每天下午的 23:00 (UTC) 触发;对应北京为每天早上07:00执行

订阅subscribe expires_微信公众号_24


开启定时触发器,这样就完成啦!