借助workerman定时给在线用户推送百度热搜榜

查看 7|回复 0
作者:美文苑文学网   
首先创建一个定时器
[ol]
  • $crontabb=new Crontab('30 30 * * * *', function(){        //每30分钟30秒执行一次
  • self::Baidtsong();//百度热搜推送
  • });[/ol]复制代码[ol]
  • //给用户推送百度热门新闻
  • public static function Baidtsong(){
  • global $public_r;        
  • $baidunewsaryy = self::getBaiduresou();
  • if ($baidunewsaryy === null) {
  • self::debug('chatlogerror.log', "未能成功获取百度热搜榜数据");
  • return;
  • }
  • $cardsData = $baidunewsaryy['data']['cards'];
  • $allContents = [];
  • foreach ($cardsData as $card) {
  • $contents = $card['content'];
  • foreach ($contents as $content) {
  • $allContents[] = $content;
  • }
  • }
  • $countToShow = 15;
  • $noticemsg = '';
  • for ($i = 0; $i ';
  • }
  • $diync='😊'.date('Y-m-d H:i:s').'热门新闻😊'.RepPostStr2($noticemsg).'';
  • $new_message = array(
  • 'type' => 'other_notice',
  • 'from_chatuname' =>"百度热搜",
  • 'headerimg' => $public_r['add_pcurl']."/e/extend/chat/img/a0.jpg",
  • 'userid' =>"1",
  • 'time' => date('Y-m-d H:i:s'),
  • 'noticemsg' =>$diync,
  • );
  • Gateway::sendToAll(json_encode($new_message));
  • }[/ol]复制代码[ol]
  • //获取百度热搜内容
  • public static function getBaiduresou(){
  • $url = 'https://top.baidu.com/api/board?platform=wise&tab=realtime'; // 百度热搜榜地址
  • $serializedData = self::$redis->get('baiduresou');
  • if ($serializedData === false) {
  • $dataa = self::curlRequest($url);
  • $data = json_decode($dataa, true);
  • if (is_array($data)) {
  • $serializedData = serialize($data);
  • self::$redis->set('baiduresou', $serializedData, 3600 * 3); // 设置缓存,时间为3小时
  • }
  • } else {
  • $data = unserialize($serializedData);
  • if (!is_array($data)) {
  • return null;
  • }
  • }
  • return $data;
  • }
  • //封装curl请求
  • public static function curlRequest($url, $method = 'GET', $data = [], $headers = []) {  
  • $ch = curl_init($url);  
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  • curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
  • curl_setopt($ch, CURLOPT_MAXREDIRS, 10);  
  • curl_setopt($ch, CURLOPT_TIMEOUT, 30);  
  • curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  • curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  • if ($method === 'POST') {  
  • curl_setopt($ch, CURLOPT_POST, true);  
  • if (is_array($data) || is_object($data)) {  
  • curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  
  • } else {  
  • curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  • }  
  • } elseif ($method !== 'GET') {  
  • curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);  
  • if (!empty($data)) {  
  • curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  
  • }  
  • }   
  • if (!empty($headers)) {  
  • curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
  • }     
  • $response = curl_exec($ch);  
  • $error = curl_error($ch);   
  • curl_close($ch);  
  • if ($error) {  
  • self::debug('chatlogerror.log', "CURL Error: " . $error);
  • }   
  • return $response;  
  • }[/ol]复制代码其他的就是JS了哈。链接websocket 自行脑补了!

    代码, 用户

  • 您需要登录后才可以回帖 登录 | 立即注册

    返回顶部