北极meng 发表于 2021-5-27 16:03

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

本帖最后由 北极meng 于 2021-10-19 05:01 编辑

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

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

为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'];
    $cityname = $return['data']['city'];

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

    $fengli = str_replace(['<!]>'],'',$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, $timeInfo * 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[$i], $str);

      $strchuli .= '['.$str.']('.$str.')'.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) 获取的是总数 可以指定条数 10条左右防止 文字内容超长
    for($i=0;$i<count($matches);$i++){
      preg_match('#<a href="(.+?)".*?>(.+?)</a>#', $matches[$i], $str);
      $title = trim(strip_tags($str));
      $url = $str;
         $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请求
* @Param string $url
* @Return 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() 函数

北极meng 发表于 2021-10-19 05:03

☆千年琥珀☆ 发表于 2021-10-18 13:22
天气一直获取不了,接口一直报403错误,不知道什么原因
天气接口挂了 换成万年历接口了
部分代码
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'];
    $cityname = $return['data']['city'];

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

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

    $fengli = str_replace(['<!]>'],'',$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 编辑

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



<?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, $timeInfo * 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[$i], $str);

      $strchuli .='['.$str.']('.$str.')'.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请求
* @Param string $url
* @Return 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;
}


北极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

感谢楼主分享
页: [1] 2 3 4 5 6 7 8
查看完整版本: 改【ARtcgb 企业微信机器人V2.0源码及自动化部署教程】帖子代码为PHP版本