在WordPress程序里部署百度收录检测,代码本身及部署无误。但总是显示已收录(实际上未收录)
代码在本地phpstudy里已测试可用,推测是环境的问题。
PS.已在php.ini里去掉curl前的;
代码在本地phpstudy里已测试可用,推测是环境的问题。
PS.已在php.ini里去掉curl前的;
3 Answers
嗯,phpstudy里也是设置php7,代码如下
function baidu_check($url) {
global $wpdb;
$post_id = (null === $post_id) ? get_the_ID() : $post_id;
$baidu_record = get_post_meta($post_id, 'baidu_record', true);
if ($baidu_record != 1) {
$url = 'http://www.baidu.com/s?wd=' . $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($curl);
curl_close($curl);
//如果抓取到的百度结果中不存在【提交网址】这个关键词,则认为该页面已被百度收录
if(!preg_match_all('/提交网址/u',$rs,$matches) && preg_match_all('/百度为您找到相关结果/u',$rs, $matches)){
update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
function baidu_record() {
if (baidu_check(get_permalink()) == 1) {
echo '<a target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd=' . get_the_title() . '">百度已收录</a>';
} else {
echo '<a style="color:red;" rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename=' . get_permalink() . '">百度未收录</a>';
}
}
Please login or Register to submit your answer