百度推送分为站长资源推送和熊掌号资源推送,api地址一样,参数略有不同,这里把两种都放进去了!
(1)打开phpcmsmodulescontentcontent.php
在最下边增加推送方法
(2)phpcmsmodulescontenttemplatescontent_list.tpl.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;
- }
找到大概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)phpcmsmodulesadmintemplatessite_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>
同理phpcmsmodulesadmintemplatessite_add.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']?>"/>
- Toekn:<input type="text" class="input-text" name="setting[xtoken]" id="xtoken" size="20" value="<?php echo $setting['xtoken']?>"/>
- </td>
- </tr>
第三步的具体位置可以根据自己需要放,找到对应的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']?>"/>
- Toekn:<input type="text" class="input-text" name="setting[xtoken]" id="xtoken" size="20" value="<?php echo $setting['xtoken']?>"/>
- </td>
- </tr>
保存,更新后台缓存,即可实现对百度的手动推送,