thinkphp百度主动推送方法

百度现在更换了链接提交方式,总共三种:

  1、主动推送:最为快速的提交方式,推荐您将站点当天新产出链接立即通过此方式推送给百度,以保证新链接可以及时被百度收录。

  2、sitemap:您可以定期将网站链接放到sitemap中,然后将sitemap提交给百度。百度会周期性的抓取检查您提交的sitemap,对其中的链接进行处理,但收录速度慢于主动推送。

  3、手工提交:一次性提交链接给百度,可以使用此种方式。

其中第2种需要百度邀请才能提交,第3种比较简单,直接生成一个网站地图就可以了,现在说说第1种,直接上代码:

Php代码 
  1. public function bdUrls() {  
  2.     $urls = array();  
  3.     //$map[],这里写上查找条件,此处省了。。  
  4.     $dats = M('news') - >where($map) - >order('createtime desc') - >select();  
  5.         if (is_array($dats)) {  
  6.             foreach($dats as $keys = >$vals) {  
  7.                 $urls[] ='';//此处写上新闻链接地址  
  8.             }  
  9.         }  
  10.     }  
  11.       
  12.     //以下代码直接从百度站长工具里复制过来  
  13.     $api = 'http://data.zz.baidu.com/urls?######';  
  14.     $ch = curl_init();  
  15.     $options = array(CURLOPT_URL = >$api, CURLOPT_POST = >true, CURLOPT_RETURNTRANSFER = >true, CURLOPT_POSTFIELDS = >implode("\n"$urls), CURLOPT_HTTPHEADER = >array('Content-Type: text/plain'), );  
  16.     curl_setopt_array($ch$options);  
  17.     $result = curl_exec($ch);  
  18.     $res = json_decode($result, true);  
  19.     //以上代码直接从百度站长工具里复制过来  
  20.       
  21.       
  22.     if (isset($res['error'])) {  
  23.         $this - >error("推送失败:".$res['message']." 错误代码:".$res['error']);  
  24.     } else {  
  25.         $this - >success("成功推送".$res['success']."条,今天还可推送".$res['remain']."条");  
  26.     }  
  27. }  

方法写好后,只要在模板中加个链接就可以了,如

Html代码 
  1. <a href="{:U('other/bdUrls')}">点击推送</a>  

此处U方法里的'other',就是other控制器,须将上面那个方法写入到other控制器内即可。

到这里就好了,希望对大家有所帮助。

转载自:http://www.codes.ren/index.php/Article/detail/id/27/cid/5.html

以下是基于ThinkPHP框架整合极光推送的示例代码: 1. 安装JPush SDK 使用composer安装JPush SDK: ``` composer require jpush/jpush ``` 2. 配置JPush 在ThinkPHP框架中,可以将JPush的配置信息写入到config目录下的jpush.php文件中,示例代码如下: ```php return [ 'app_key' => 'YOUR_APP_KEY', 'master_secret' => 'YOUR_MASTER_SECRET', ]; ``` 3. 创建JPushService类 在app/service目录下创建JPushService类,该类用于封装极光推送的相关操作。 ```php <?php namespace app\service; use JPush\Client as JPush; class JPushService { protected $jpush; public function __construct() { $config = config('jpush'); $this->jpush = new JPush($config['app_key'], $config['master_secret']); } // 发送通知 public function sendNotification($title, $content, $extras = [], $audience = 'all') { $notification = [ 'title' => $title, 'alert' => $content, ]; $message = [ 'title' => $title, 'msg_content' => $content, 'extras' => $extras, ]; $options = [ 'apns_production' => false, ]; $response = $this->jpush->push() ->setPlatform(['ios', 'android']) ->setAudience($audience) ->setNotification($notification) ->setMessage($message) ->setOptions($options) ->send(); return $response; } } ``` 4. 使用JPushService类发送推送 在控制器中使用JPushService类发送推送,示例代码如下: ```php <?php namespace app\controller; use app\service\JPushService; class PushController { public function send() { $title = '测试推送'; $content = '这是一条测试推送'; $extras = ['key1' => 'value1', 'key2' => 'value2']; $jpushService = new JPushService(); $response = $jpushService->sendNotification($title, $content, $extras); if ($response['http_code'] === 200) { return json(['code' => 0, 'message' => '推送成功']); } else { return json(['code' => -1, 'message' => '推送失败']); } } } ``` 以上就是基于ThinkPHP框架整合极光推送的示例代码。需要注意的是,示例代码中使用了JPush的免费版服务,如果需要使用更高级别的服务,需要进行相应的付费。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值