phpcms文章列表增加一键推送到百度功能

2022-12-08 浏览:768
phpcms文章列表增加一键推送到百度功能
评论:(0)复制地址

phpcms文章列表增加一键推送到百度功能


(1)打开phpcms\modules\content\content.php 在最下边增加推送方法

/** 
     * 一键推送百度 
     */ 
    public function push_bd() { 
        $ids = $_POST['ids']; 
        $ids = implode(',', $ids);//合并子栏目id 
        if(!$ids) showmessage(L('you_do_not_check')); 
        $catid = intval($_GET['catid']); 
        if(!$catid) showmessage(L('missing_part_parameters')); 
        $modelid = $this->categorys[$catid]['modelid']; 
        $siteid = $this->categorys[$catid]['siteid']; 
         
        $data = $urls = array(); 
        $this->db = pc_base::load_model('content_model');//引入content模型 
        $this->db->set_model($modelid); 
        $sql = "status=99 AND id IN ($ids)"; 
        $order = "listorder desc, id desc"; 
        $data = $this->db->select($sql, '*', '', $order, '', 'id'); 
        foreach($data as $v){ 
             $urls[] = $v['url']; 
        }; 
         
        /*获取appid和appsecret*/ 
        $this->site = pc_base::load_model('site_model'); 
        $siteinfo   = $this->site->get_one(array('siteid'=>$siteid)); 
        $setting    = json_decode($siteinfo['setting'], true); 
        $bdtoken    = $setting['bdtoken'];//站长token 
        $token      = $setting['xtoken'];//熊掌token 
        $appid      = $setting['appid']; 
        $domain     = parse_url($siteinfo['domain']); 
        $siteurl    = $domain['host']; 
         
        $res1 = $res2 = ''; 
        if(!empty($bdtoken)){ 
            $bdapi =  'http://data.zz.baidu.com/urls?site='.$siteurl.'&token='.$bdtoken; 
            $res1 = json_decode(self::baidu_push($bdapi, $urls), true); 
        } 
        if(!empty($token) && !empty($appid)){ 
            $api = 'http://data.zz.baidu.com/urls?appid='.$appid.'&token='.$token.'&type=realtime'; 
            $res2 = json_decode(self::baidu_push($api, $urls), true); 
        } 
        if($res1 || $res2){ 
            $text = ''; 
            if($res1['success']){ 
                $text .= '站长推送成功,成功数量'.$res1['success']; 
            }else{ 
                $text .= '站长推送失败,失败数量'.$res1['success']; 
            } 
            if($res2['success']){ 
                $text .= '------熊掌推送成功,成功数量'.$res2['success_realtime']; 
            }else{ 
                if($res2['remain_realtime']){ 
                    $text .= '------熊掌推送失败,失败数量'.$res2['success_realtime']; 
                }else{ 
                    $text .= '------<font color="red">今日熊掌推送次数已用完</font>'; 
                }  
            } 
            showmessage($text,HTTP_REFERER,8000); 
        }else { 
            showmessage(L('operation_failure')); 
        } 
    } 
     
    /* 
    ** 百度推送处理函数 
    */ 
    public function baidu_push($api, $urls){ 
        $ch = curl_init(); 
        $options =  array( 
            CURLOPT_URL => $api, 
            CURLOPT_POST => true, 
            CURLOPT_RETURNTRANSFER => true, 
            CURLOPT_POSTFIELDS => implode("\n", $urls), 
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), 
        ); 
        curl_setopt_array($ch, $options); 
        $result = curl_exec($ch); 
        return $result; 
    }


(2)phpcms\modules\content\templates\content_list.tpl.php  找到大概141行代码:

<input type="button" class="button" value="<?php echo L('remove');?>" onclick="myform.action='?m=content&c=content&a=remove&catid=<?php echo $catid;?>';myform.submit();"/>

在下边增加代码:

<input type="button" class="button" value="一键推送百度" onclick="myform.action='?m=content&c=content&a=push_bd&catid=<?php echo $catid;?>';myform.submit();"/>


(3)phpcms\modules\admin\templates\site_add.tpl.php 找到大概135代码:

<tr> 
   <th width="130" valign="top"><?php echo L('site_att_allow_ext')?></th> 
   <td class="y-bg"><input type="text" class="input-text" name="setting[upload_allowext]" id="upload_allowext" size="50" value="<?php echo $setting['upload_allowext']?>"/></td> 
 </tr>

在下边新增代码:

<tr> 
    <th width="130" valign="top">百度推送Token</th> 
    <td class="y-bg"><input type="text" class="input-text" name="setting[bdtoken]" id="bdtoken" size="50" value="<?php echo $setting['bdtoken']?>"/></td> 
  </tr>  
  <tr> 
    <th width="130" valign="top">百度熊掌号设置</th> 
    <td class="y-bg">Appid:<input type="text" class="input-text" name="setting[appid]" id="appid" size="20" value="<?php echo $setting['appid']?>"/> 
    &nbsp;&nbsp;Toekn:<input type="text" class="input-text" name="setting[xtoken]" id="xtoken" size="20" value="<?php echo $setting['xtoken']?>"/> 
    </td> 
  </tr>


(4)phpcms\modules\admin\templates\site_edit.tpl.php找到125行之后增加如上代码:

<tr> 
    <th width="130" valign="top">百度推送Token</th> 
    <td class="y-bg"><input type="text" class="input-text" name="setting[bdtoken]" id="bdtoken" size="50" value="<?php echo $setting['bdtoken']?>"/></td> 
  </tr>  
  <tr> 
    <th width="130" valign="top">百度熊掌号设置</th> 
    <td class="y-bg">Appid:<input type="text" class="input-text" name="setting[appid]" id="appid" size="20" value="<?php echo $setting['appid']?>"/> 
    &nbsp;&nbsp;Toekn:<input type="text" class="input-text" name="setting[xtoken]" id="xtoken" size="20" value="<?php echo $setting['xtoken']?>"/> 
    </td> 
  </tr>

最后保存,更新后台缓存,即可实现对百度的手动推送

评论:(0)复制地址
发布:苗景云 | 分类:IT技术&设计 | Tags:phpcms

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。