本方案为计划任务解决方案。
1、在文章表新建一个字段,如log_ytecn_bdsp,设置默认值为0。
2、在根目录创建一个文件,比如baidusearchpost.php,
点击复制代码 PHP
<?php
require 'zb_system/function/c_system_base.php';
$zbp->Load();
$client_id=$zbp->Config('ytecn_zhouyi')->appkey;
$client_secret=$zbp->Config('ytecn_zhouyi')->appsecret;
$url="https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$client_id."&client_secret=".$client_secret."&scope=smartapp_snsapi_base";
$w=array();
$w['count']=5;
$w['where_custom']=array(array('=', 'log_ytecn_bdsp', 0));
$w['order_custom']=array('log_ID' => 'ASC');
$array=GetList($w);
$as=array();
if(count($array)){
foreach ($array as $cc) {
$id=$cc->ID;
$title=$cc->Title;
$intro=$cc->Intro;
$intro = preg_replace('/[\r\n\s]+/', ' ', trim(SubStrUTF8(TransferHTML($intro,'[nohtml]'),135)).'...');
$ajax = Network::Create();
$ajax->open('GET', $url);
$ajax->enableGzip();
$ajax->setTimeOuts(60, 60, 0, 0);
$ajax->send();
$response = json_decode($ajax->responseText, true);
if (empty($response['access_token'])){
$as[]='推送搜索失败';
}else{
$access_token=$response['access_token'];
$post='[{
"path":"/pages/article/index?id='.$id.'",
"jump_app_key":"'.$client_id.'",
"title":"'.$title.'",
"mapp_type":2001,
"schema":"{\"datatype\":2,\"desc\":\"'.$intro.'\",\"detail\":{\"params\":{\"title\":\"'.$title.'\",\"desc\":\"'.$intro.'\",\"app_key\":\"'.$client_id.'\",\"jump_url\":\"/pages/article/index?id='.$id.'\",\"jump_type\":0}}}"
}]';
$header = array("Content-Type:application/json");
$api = "https://openapi.baidu.com/rest/2.0/smartapp/search/submit/schema?access_token=".$access_token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl);
$as[]='推送成功';
$sql = $zbp->db->sql->get()->update($zbp->table['Post'])
->where('=', 'log_ID', $id)
->data(array('log_ytecn_bdsp' => '1'))
->sql;
$zbp->db->Query($sql);
$zd=1;
}
}
}else{
$zd=0;
echo "推送完毕";
die();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>自动推送</title>
</head>
<body style="line-height:20px">
<?php
foreach ($as as $key =>$item) {
?>
<div><?php echo $item; ?>
</div>
<?php
}
if($zd){
?>
<script>
setTimeout(function() {
location.href = "<?php echo $zbp->host."baidusearchpost.php"; ?>";
}, 3000);
</script>
<span>暂停3秒后继续 >>> </span>
<a href="<?php echo $zbp->host.'baidusearchpost.php'; ?>">如果您的浏览器没有自动跳转,请点击这里</a><br>
<?php
}
?>
</body>
</html>
3、在浏览器里面执行下面的链接
点击复制代码 PHP
域名/baidusearchpost.php
等待出现“推送完毕”, 说明已推送结束。
4、完毕。