zblog主题网站自动推广到百度收录API的方法

我今天在做百度收录提交的时候突然发现PHP curl还不是太熟悉,于是一边测试,一边搞起来。经过半天的测试算是大体成功了。

大体思路是这样的:编辑文章提交时激发函数,通过判断文章是否被阅读来推送。

百度PHP示范代码:

 $urls = array(
    'http://www.example.com/1.html',
    'http://www.example.com/2.html',
);
$api = 'http://data.zz.baidu.com/urls?site=网址&token=你的token';
$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;

我的zblog主题是netblue,设置了一个BAIDUTOKEN选项用来填写对应的token:

function netblue_api_baidu(&$post){
	global $zbp;
	$baidu_token = $zbp->Config('netblue')->BAIDUTOKEN;
	$long = strlen($zbp->host)-1;
	$api_url = substr($zbp->host,0,$long);
	$post_url = $api_url.'?id='.$post->ID;
	$number = $post->ViewNums;
	if ($baidu_token && $number <= 0) {
		$urls = array($post_url);
		$api = 'http://data.zz.baidu.com/urls?site='.$api_url.'&token='.$baidu_token;
		$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);
		$returnArr = curl_exec($ch);
		curl_close($ch);
		if (isset($returnArr['error'])) {
			echo $returnArr['message'];
			exit;
		}
	}else{
		return;
	}
} 

然后在主题对应的include.php文件做一个挂钩:

function ActivePlugin_netblue(){
	$theme = "netblue";
	Add_Filter_Plugin('Filter_Plugin_PostArticle_Succeed', $theme.'_api_baidu');//百度API收录提交
}
RegisterPlugin('netblue','ActivePlugin_netblue');
留言评论
称呼
邮箱
网址
展开