[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;
}