记住用户名密码
/** * PHP检测URL地址百度是否已经收录 * @param string $url 要检测的URL地址 */ function Baidu($url) { $url = 'http://www.baidu.com/s?wd=' . urlencode($url); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $content= curl_exec($curl); curl_close($curl); if (!strpos($content, '没有找到')) { //没有找到说明已被百度收录 return false; } else { return true; } }
使用方法
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//当前页面URL地址
然后我们就可以根据Baidu($url);返回值判断该URL是否已经收录再实行自主推送
以下是php百度主动推送示例,可直接推送百度mip
//引入测验百度是否收录并加载代码的函数 function baidu_php_push($url){ $check = Baidu($url); if($check==false){ $urls[]=$url; $api = 'http://data.zz.baidu.com/urls?site=你的推送接口'; $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); echo $result; } }
或者js自动推送
//引入测验百度是否收录并加载代码的函数 function baidu_js_push($url){ $check = Baidu($url); if($check==false){ echo "<script>(function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bp, s); })(); (function(){ var src = (document.location.protocol == 'http:') ? 'http://js.passport.qihucdn.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7':'https://jspassport.ssl.qhimg.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7'; document.write('<script src=\"' + src + '\" id=\"sozz\"><\/script>'); })();</script>"; } }
目前有 0 条留言 其中:访客:0 条, 博主:0 条