百度小程序搜索組件批量提交文章代碼
本方案為計劃任務解決方案。
1、在文章表新建一個字段,如log_ytecn_bdsp,設置默認值為0。
2、在根目錄創建一個文件,比如baidusearchpost.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、在瀏覽器里面執行下面的鏈接
域名/baidusearchpost.php
等待出現“推送完畢”, 說明已推送結束。
4、完畢。