百度现在更换了链接提交方式,总共三种:
1、主动推送:最为快速的提交方式,推荐您将站点当天新产出链接立即通过此方式推送给百度,以保证新链接可以及时被百度收录。
2、sitemap:您可以定期将网站链接放到sitemap中,然后将sitemap提交给百度。百度会周期性的抓取检查您提交的sitemap,对其中的链接进行处理,但收录速度慢于主动推送。
3、手工提交:一次性提交链接给百度,可以使用此种方式。
其中第2种需要百度邀请才能提交,第3种比较简单,直接生成一个网站地图就可以了,现在说说第1种,直接上代码:
- public function bdUrls() {
- $urls = array();
- //$map[],这里写上查找条件,此处省了。。
- $dats = M('news') - >where($map) - >order('createtime desc') - >select();
- if (is_array($dats)) {
- foreach($dats as $keys = >$vals) {
- $urls[] ='';//此处写上新闻链接地址
- }
- }
- }
- //以下代码直接从百度站长工具里复制过来
- $api = 'http://data.zz.baidu.com/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);
- $res = json_decode($result, true);
- //以上代码直接从百度站长工具里复制过来
- if (isset($res['error'])) {
- $this - >error("推送失败:".$res['message']." 错误代码:".$res['error']);
- } else {
- $this - >success("成功推送".$res['success']."条,今天还可推送".$res['remain']."条");
- }
- }
方法写好后,只要在模板中加个链接就可以了,如
- <a href="{:U('other/bdUrls')}">点击推送</a>
此处U方法里的'other',就是other控制器,须将上面那个方法写入到other控制器内即可。
到这里就好了,希望对大家有所帮助。
转载自:http://www.codes.ren/index.php/Article/detail/id/27/cid/5.html