吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8962|回复: 79
收起左侧

[其他转载] 改【ARtcgb 企业微信机器人V2.0源码及自动化部署教程】帖子代码为PHP版本

[复制链接]
北极meng 发表于 2021-5-27 16:03
本帖最后由 北极meng 于 2021-10-19 05:01 编辑

@ARtcgb  Python】企业微信机器人V2.0源码及自动化部署教程

自动化爬取每日天气、热搜、每日一句并通过企业微信机器人发送至群聊

为php 版本 忘记是根据这两个那个帖子改的了
效果图
e2211df8d4dd752c973c2c512d714e3.jpg

[PHP] 纯文本查看 复制代码
<?php
#地区编号 默认天津
$cityid = isset($_GET['cityid']) ? $_GET['cityid'] : '101030100'; 
//这里配置机器人url;
$jiqiren_url = '';

$contents = get_sendContent($cityid);
#外网访问地址 为 域名+/xxx.php?cityid=''
//最终调用
print_r(send($contents, $jiqiren_url));
//发送内容
function get_sendContent($cityid = '101030100')
{
    $sendContent = "##" . greetings() . "\n" . get_week_day() . "\n\n" . get_weather($cityid) . "\n\n" . get_top_list() . "\n\n" . get_daily_sentence();
    return $sendContent;

}
//执行发送操作
function send($content, $url)
{

    $headers = '{"Content-Type": "text/plain"}';
    $data = [
        "msgtype" => "markdown",
        "markdown" =>
        ["content" => $content,
            "mentioned_list" => ["@all"],
        ],

    ];
    $res = request_post($url, json_encode($data, '320'), 'json');

    if ($res == '{"errcode":0,"errmsg":"ok"}') {
        return "发送成功";
    } else {
        return "发送失败" . $res;
    }
}
//获取天气信息
function get_weather($areaid = '')
{
    $url = "http://wthrcdn.etouch.cn/weather_mini?citykey=".$areaid;

    $requests_url = file_get_contents($url);//获取天气情况
    $requests_url = gzdecode($requests_url);

    $return = json_decode($requests_url,true);//去掉dataSk 转成数组

    $message=$return['data']['forecast'][0];
    $cityname = $return['data']['city'];

    $date = date('Y/m/').$message['date'];
    $sd = $message['SD'];
    $wd = $message['low'].'/'.$message['high'];

    $fengli = str_replace(['<![CDATA[',']]>'],'',$message['fengli']);

    $fengsu = $message['fengxiang'].$fengli;
    $temp = $message['temp'];
    $weather = $message['type'];

    $res_weather = $cityname.PHP_EOL.'今天是'.$date.PHP_EOL.'天气:'.$weather.PHP_EOL.'温度:'.$wd.$ws.PHP_EOL.'风速:'.$fengsu.PHP_EOL;
    return $res_weather;
 
}//获取毫秒时间戳
function getCurrentMilis()
{
    $mill_time = microtime();
    $timeInfo = explode(' ', $mill_time);
    $milis_time = sprintf('%d%03d', $timeInfo[1], $timeInfo[0] * 1000);
    return $milis_time;
}
//获取百度头条旧版(已失效)
function get_top_list_old(){
    $baidustr1 = file_get_contents("http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b1");    
    $baidustr = mb_convert_encoding($baidustr1, 'UTF-8', 'GB2312'); 
    preg_match_all('/<a class=\"list-title\".*?>.*?<\/a>/ism', $baidustr, $matches);
    $strchuli='';
    for($i=0;$i<10;$i++){

        preg_match('#<a.+?href="(.+?)".*?>(.+?)</a>#', $matches[0][$i], $str);

        $strchuli .= '['.$str[2].']('.$str[1].')'.PHP_EOL;
    }
    return $strchuli;
}
//获取百度头条新版
function get_top_list(){        
    $baidustr1 = file_get_contents("https://top.baidu.com/board?tab=realtime");  
    $baidustr = mb_convert_encoding($baidustr1,"UTF-8", "auto"); 
    preg_match_all('/<div class=\"content_.*?>.*?<\/a>/ism', $baidustr, $matches);  
    $strchuli='';
    //这里的count($matches[0]) 获取的是总数 可以指定条数 10条左右  防止 文字内容超长
    for($i=0;$i<count($matches[0]);$i++){
        preg_match('#<a href="(.+?)".*?>(.+?)</a>#', $matches[0][$i], $str);
        $title = trim(strip_tags($str[2])); 
        $url = $str[1];
         $strchuli .= '['.$title.']('.$url.')'.PHP_EOL;
    }
 
    return $strchuli;
}

//获取金山词霸每日一句
function get_daily_sentence()
{

    $url = "http://open.iciba.com/dsapi/";
    $r = file_get_contents($url);
    $r = json_decode($r, true);
    $content = $r["content"];
    $note = $r["note"];
    $daily_sentence = "> " . $content . "\n" . "> " . $note;
    return $daily_sentence;
}

function greetings()
{
    $hour = intval(date('i'));
    if ($hour == 6) {
        return "早上好!\n";
    } elseif ($hour == 12) {
        return "中午好!";
    } elseif ($hour == 21) {
        return "晚上好!";
    } else {
        return "今日推荐";
    }

}
//获取日期
function get_week_day()
{
    $wk_day = date('w'); //得到今天是星期几
    $wkday_ar = array('日', '一', '二', '三', '四', '五', '六'); //规范化周日的表达
    $str = '今天是' . date("Y年n月j日") . " 星期" . $wkday_ar[$wk_day];
    return $str;
}


/**
 * 模拟get进行url请求
 * [url=home.php?mod=space&uid=952169]@Param[/url] string $url
 * [url=home.php?mod=space&uid=155549]@Return[/url] json
 */
function httpGet($url)
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}

/**
 * 模拟post进行url请求
 * @param string $url
 * @param array $post_data
 * @param string $dataType
 * @return bool|mixed
 */
function request_post($url = '', $post_data = array(), $dataType = '')
{
    if (empty($url) || empty($post_data)) {
        return false;
    }
    $curlPost = $post_data;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($dataType == 'json') {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
            'Content-Length: ' . strlen($curlPost),
        )
        );
    }
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    return $data;
}


本来代码整合了
https://j.i8tq.com/weather2020/search/city.js
这个js文件里面有每个城市的编码
结果一度达到了近400kb  导致无法添发表 无奈又删除了


由于百度头条改版导致获取失效 ,现已更新 get_top_list() 函数  请及时更新
天气接口挂了 ,现已更新为万年历天气接口 get_weather() 函数  

1111111111.jpg

免费评分

参与人数 9吾爱币 +13 热心值 +7 收起 理由
lovlin999 + 1 + 1 谢谢@Thanks!
QZGF + 1 + 1 热心回复!
wazh + 1 + 1 强啊老哥!
Linkasyan + 1 + 1 感谢老铁~
blindcat + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
木木吃番茄 + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
ARtcgb + 1 + 1 用心讨论,共获提升!
2595453382 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 北极meng 发表于 2021-10-19 05:03
☆千年琥珀☆ 发表于 2021-10-18 13:22
天气一直获取不了,接口一直报403错误,不知道什么原因

天气接口挂了 换成万年历接口了
部分代码
[PHP] 纯文本查看 复制代码
function get_weather($areaid = '')
{
    $url = "http://wthrcdn.etouch.cn/weather_mini?citykey=".$areaid;

    $requests_url = file_get_contents($url);//获取天气情况
    $requests_url = gzdecode($requests_url);

    $return = json_decode($requests_url,true);//去掉dataSk 转成数组

    $message=$return['data']['forecast'][0];
    $cityname = $return['data']['city'];

    $date = date('Y/m/').$message['date'];

    $wd = $message['low'].'/'.$message['high'];

    $fengli = str_replace(['<![CDATA[',']]>'],'',$message['fengli']);

    $fengsu = $message['fengxiang'].$fengli;
    $temp = $message['temp'];
    $weather = $message['type'];

    $res_weather = $cityname.PHP_EOL.'今天是'.$date.PHP_EOL.'天气:'.$weather.PHP_EOL.'温度:'.$wd.$ws.PHP_EOL.'风速:'.$fengsu.PHP_EOL;
    return $res_weather;
 
}
azusys 发表于 2021-5-28 15:52
本帖最后由 azusys 于 2021-5-28 17:24 编辑

给楼主修改了一下代码
效果如下
image.png

[Asm] 纯文本查看 复制代码
<?php
#地区编号 默认天津
$cityid = isset($_GET['cityid']) ? $_GET['cityid'] : '101030100'; 
//这里配置机器人url;
$jiqiren_url = '';
 
$contents = get_sendContent($cityid);
#外网访问地址 为 域名+/xxx.php?cityid=''
//最终调用
print_r(send($contents, $jiqiren_url));
//发送内容
function get_sendContent($cityid = '101030100')

{

    $sendContent = "" . greetings() . "\n" . get_week_day() . "\n\n" . get_weather($cityid) . "\n\n" .'=========='. PHP_EOL .'今日热点:'. PHP_EOL . get_top_list() . "\n\n" .'=========='. PHP_EOL .'每日金句:'. PHP_EOL . get_daily_sentence();
    return $sendContent;
 
}
//执行发送操作
function send($content, $url)
{
 
    $headers = '{"Content-Type": "text/plain"}';
    $data = [
        "msgtype" => "markdown",
        "markdown" =>
        ["content" => $content,
            "mentioned_list" => ["@all"],
        ],
 
    ];
    $res = request_post($url, json_encode($data, '320'), 'json');
 
    if ($res == '{"errcode":0,"errmsg":"ok"}') {
        return "发送成功";
    } else {
        return "发送失败" . $res;
    }
}
//获取天气信息
function get_weather($areaid = '')
{
    $t = getCurrentMilis();
    $url = "https://d1.weather.com.cn/sk_2d/$areaid.html?_=$t";
 
    $requests_url = file_get_contents($url); //获取天气情况
    $message = json_decode(str_replace("var dataSK=", "", $requests_url), true); //去掉dataSk 转成数组
    $cityname = $message['cityname'];
    $aqi = intval($message['aqi']);
    $sd = $message['sd'];
    $wd = $message['WD'];
    $ws = $message['WS'];
    $temp = $message['temp'];
    $weather = $message['weather'];
    if ($aqi <= 50) {
        $airQuality = "优";
    } elseif ($aqi <= 100) {
        $airQuality = "良";
    } elseif ($aqi <= 150) {
        $airQuality = "轻度污染";
    } elseif ($aqi <= 200) {
        $airQuality = "中度污染";
    } elseif ($aqi <= 300) {
        $airQuality = "重度污染";
    } else {
        $airQuality = "严重污染";
    }
    $res_weather = $cityname . PHP_EOL . '今日天气:' . $weather . PHP_EOL . '温度:' . $temp . ' 摄氏度 ' . $wd . $ws . PHP_EOL . '相对湿度:' . $sd . PHP_EOL . '空气质量:' . $aqi . "(" . $airQuality . ")";
    return $res_weather;
    //print_r($res_weather);
 
}
//获取毫秒时间戳
function getCurrentMilis()
{
    $mill_time = microtime();
    $timeInfo = explode(' ', $mill_time);
    $milis_time = sprintf('%d%03d', $timeInfo[1], $timeInfo[0] * 1000);
    return $milis_time;
}
//获取百度头条
function get_top_list(){
    $baidustr1 = file_get_contents("http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b1");    
    $baidustr = mb_convert_encoding($baidustr1, 'UTF-8', 'GB2312'); 
    preg_match_all('/<a class=\"list-title\".*?>.*?<\/a>/ism', $baidustr, $matches);
    $strchuli='';
    for($i=0;$i<10;$i++){
 
        preg_match('#<a.+?href="(.+?)".*?>(.+?)</a>#', $matches[0][$i], $str);
 
        $strchuli .=  '['.$str[2].']('.$str[1].')'.PHP_EOL;
    }
    return $strchuli;
}//获取金山词霸每日一句
function get_daily_sentence()
{
 
    $url = "http://open.iciba.com/dsapi/";
    $r = file_get_contents($url);
    $r = json_decode($r, true);
    $content = $r["content"];
    $note = $r["note"];
    $daily_sentence = "> " . $content . "\n" . "> " . $note;
    return $daily_sentence;
}
function greetings()
{
    $hour = intval(date('i'));
    if ($hour == 1) {
        return "各位同事早上好!\n";
    } elseif ($hour == 12) {
        return "各位同事中午好!";
    } elseif ($hour == 21) {
        return "各位同事晚上好!";
    } else {
        return "每日分享";
    }
 
}
//获取日期
function get_week_day()
{
    $wk_day = date('w'); //得到今天是星期几
    $wkday_ar = array('日', '一', '二', '三', '四', '五', '六'); //规范化周日的表达
    $str = '今天是' . date("Y年n月j日") . " 星期" . $wkday_ar[$wk_day];
    return $str;
}
 
 
/**
 * 模拟get进行url请求
 * [url=home.php?mod=space&uid=952169]@Param[/url] string $url
 * [url=home.php?mod=space&uid=155549]@Return[/url] json
 */
function httpGet($url)
{
 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
 
    $res = curl_exec($curl);
    curl_close($curl);
 
    return $res;
}
 
/**
 * 模拟post进行url请求
 * [url=home.php?mod=space&uid=952169]@Param[/url] string $url
 * @param array $post_data
 * @param string $dataType
 * [url=home.php?mod=space&uid=155549]@Return[/url] bool|mixed
 */
function request_post($url = '', $post_data = array(), $dataType = '')
{
    if (empty($url) || empty($post_data)) {
        return false;
    }
    $curlPost = $post_data;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($dataType == 'json') {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
            'Content-Length: ' . strlen($curlPost),
        )
        );
    }
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    return $data;
}



 楼主| 北极meng 发表于 2021-5-27 16:07
代码在第一行 替换 地区编码 和  企业机器人 可直接 放 环境中运行
吾爱益达君 发表于 2021-5-27 16:26
感谢楼主分享!!
2595453382 发表于 2021-5-27 16:52
先收藏了
ppszxc 发表于 2021-5-27 16:54
可以直接点新闻跳转链接不,py版本好像点新闻跳转
 楼主| 北极meng 发表于 2021-5-27 19:28
ppszxc 发表于 2021-5-27 16:54
可以直接点新闻跳转链接不,py版本好像点新闻跳转

可以吧新闻链接弄出来  我这边过滤掉了  只保留了标题
 楼主| 北极meng 发表于 2021-5-27 20:31
ppszxc 发表于 2021-5-27 16:54
可以直接点新闻跳转链接不,py版本好像点新闻跳转

代码修改了一下  可以 带链接跳转了 markdown语法不熟
头像被屏蔽
大大连连 发表于 2021-5-28 08:18
提示: 作者被禁止或删除 内容自动屏蔽
pojieyeah 发表于 2021-5-28 09:35
上传到服务器PHP文件就管用了么,小白
blindcat 发表于 2021-5-28 10:37
感谢楼主分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 23:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表