前段时间我大学的辅导员过生日,实在不知道送她什么了,然后看到在网上看到最近很火的微信推送,就想着送这个给她把,他最喜欢明星就周杰伦、李健、玄彬等,每天都会随机推送一张图片给她,还有每天随机一首个的名称,最终效果是这样的:

image.png

点击详情就可以看到那几个明星中的随机一张图片。下面分享的则是去掉了这些功能的:

image.png

PHP代码为:

index.php文件为:

<?php
/*
 * @Descripttion: 微信推送
 * @version: 1.0
 * @Author: 默然
 * @Date: 2022-10-04 01:35:27
 * @LastEditors: 默然
 * @LastEditTime: 2022-10-05 08:00:33
 */
include_once "function.php";

/** 微信公众号信息配置 */
$WXconfig = array(
    'app_id' => '1', //公众号appId
    'app_secret' => '1', //公众号appSecret
    'touser' => '1', //公众号appSecret
    'template_id' => '1', //公众号appSecret
);

//获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $WXconfig['app_id'] . "&secret=" . $WXconfig['app_secret'];
$returnJson = json_decode(curl_http($url), true);
$token = $returnJson["access_token"];
if (!$token) {
    $return = array(
        "code" => $returnJson['errcode'],
        "msg" => "获取access_token失败,请检查app_id和app_secret是否正确",
    );
    exit(json_encode($returnJson));
}

//获取天气预报的所有信息
$weatherinfo = getweather(0);

//发送消息
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
$posts = array(
    "touser" => $WXconfig['touser'],
    "template_id" => $WXconfig['template_id'],
    "url" => "",
    "topcolor" => getcolor(),
    "data" => array(
        "date" => array(
            "value" => "今天是" . date("Y年m月d日", time()) . $weatherinfo["week"],
            "color" => getcolor(),
        ),
        "region" => array(
            "value" => "昆明市",
            "color" => getcolor(),
        ),
        "weather" => array(
            "value" => $weatherinfo["weather"],
            "color" => getcolor(),
        ),
        "high" => array(
            "value" => $weatherinfo["high"],
            "color" => getcolor(),
        ),
        "low" => array(
            "value" => $weatherinfo["low"],
            "color" => getcolor(),
        ),
        "fortoday" => array(
            "value" => $weatherinfo["notice"],
            "color" => getcolor(),
        ),
        "vhanzh" => array(
            "value" => getian()[0]["mingrenmingyan"],
            "color" => getcolor(),
        ),
        "zhufu" => array(
            "value" => $zhufu,
            "color" => getcolor(),
        ),
    ),
);

$rs = curl_http($url, 1, $posts);
echo $rs;

function.php文件为:

<?php
/*
 * @Descripttion: 公共方法合集
 * @version: 1.0
 * @Author: 默然
 * @Date: 2022-10-04 01:35:27
 * @LastEditors: 默然
 * @LastEditTime: 2022-10-04 21:04:12
 */

/**
 * curl 函数
 * @param string $url 请求的地址
 * @param string $type 1 post传输  0为get
 * @param array $data 要传输的数据
 */
function curl_http($url, $type = 0, $data = [])
{
    // 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    //是否设置为post请求和请求参数
    if ($type == 1) {
        curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Post提交的数据包
    }
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源,并且释放系统资源
    curl_close($ch);
    return $output;
}

//获取随机颜色
function getcolor()
{
    $colors = array();
    for ($i = 0; $i < 6; $i++) {
        $colors[] = dechex(rand(0, 15));
    }
    return "#" . implode('', $colors);
}

//获取天气预报
function getweather($day)
{
    //这里city后面得数字具体换成自己当地的
    $url = "http://t.weather.sojson.com/api/weather/city/101290101";
    $returnJson = json_decode(curl_http($url), true);
    $rs = $returnJson["data"]["forecast"][$day];
    //重新组装数据
    $data["week"] = $rs["week"];
    $data["weather"] = $rs["type"];
    $data["high"] = substr(strstr($rs["high"], " "), 1);
    $data["low"] = substr(strstr($rs["low"], " "), 1);
    $data["notice"] = $rs["notice"];
    return $data;
}

//获取一言
function getian()
{
    $url = "https://v.api.aa1.cn/api/api-wenan-mingrenmingyan/index.php?aa1=json";
    $returnJson = json_decode(curl_http($url), true);
    return $returnJson;
}

模板文件为:

{{date.DATA}} 
地区:{{region.DATA}} 
天气:{{weather.DATA}} 
最高气温:{{high.DATA}} 
最低气温:{{low.DATA}} 
今日建议:{{fortoday.DATA}} 
{{vhanzh.DATA}} 
{{zhufu.DATA}}
最后修改:2022 年 12 月 04 日
如果觉得我的文章对你有用,请随意赞赏