後臺整合-使用百度小程式api提交小程式路徑
阿新 • • 發佈:2022-03-07
背景:解決手動提交小程式資源的繁瑣,直接在cms後臺進行提交.本文完全適用與使用pbootcms建站的網站小程式,本文采用的是php語言,所以針對php的站點也有較大的參考.其他語言的話只有一部分參考價值
主要修改的地方/檔案:
前端展示:
\apps\admin\view\default\content\content.html
後端方法:
\apps\admin\controller\content\ContentController.php
\apps\common\function.php
修改內容如下:
content.html中新增按鈕入口,這裡添加了三個,我們實際應用中視情況新增
<button type="submit" name="submit" value="baixcx_ri" class="layui-btn layui-btn-sm">百度天級推送</button>
<button type="submit" name="submit" value="baixcx_zhou" class="layui-btn layui-btn-sm">百度周級推送</button>
<button type="submit" name="submit" value="baixcx_chanpin" class="layui-btn layui-btn-sm">百度小程式_非新聞的推送</button>ss
ContentController.php中新增對應的處理邏輯:
1 case 'baixcx_ri': 2 $flag_num = 0; 3 $list = post('list'); 4 // $urls = post('urls'); 5 if (! $list) { 6 alert_back('請選擇要推送的內容!'); 7 }8 9 10 //$App_Key = $this->config('App_Key'); 11 12 // 開始推送 13 $domain = "/pages/news-detail/news-detail?id="; 14 15 foreach ($list as $key=> $value) { 16 17 $url =$domain . $value; 18 19 $this->log('百度小程式收錄推送:' . $url); 20 $post_urls .= $url . ','; 21 $flag_num++; 22 } 23 24 if(strlen($post_urls) > 5){ 25 $result = baiduMini(substr($post_urls,0,-1),1); 26 27 28 if ($result->errno == 0 || $result->errno == '0') { 29 alert_back('成功推送' . $flag_num . '條'); 30 31 } else { 32 alert_back('百度小程式收錄推送發生錯誤:' . $result->msg); 33 } 34 }else{ 35 alert_back('百度小程式收錄推送發生錯誤_連結獲取失敗'); 36 } 37 case 'baixcx_zhou': 38 $flag_num = 0; 39 $list = post('list'); 40 // $urls = post('urls'); 41 if (! $list) { 42 alert_back('請選擇要推送的內容!'); 43 } 44 45 46 //$App_Key = $this->config('App_Key'); 47 48 // 開始推送 49 $domain = "/pages/news-detail/news-detail?id="; 50 51 foreach ($list as $key => $value) { 52 53 $url =$domain . $value; 54 55 $this->log('百度小程式收錄推送:' . $url); 56 $post_urls .= $url . ','; 57 $flag_num++; 58 } 59 60 if(strlen($post_urls) > 5){ 61 $result = baiduMini(substr($post_urls,0,-1),0); 62 63 64 if ($result->errno == 0 || $result->errno == '0') { 65 alert_back('成功推送' . $flag_num . '條'); 66 67 } else { 68 alert_back('百度小程式收錄推送發生錯誤:' . $result->msg); 69 } 70 }else{ 71 alert_back('百度小程式收錄推送發生錯誤_連結獲取失敗'); 72 } 73 case 'baixcx_path': 74 $flag_num = 0; 75 $list = post('list'); 76 // $urls = post('urls'); 77 if (! $list) { 78 alert_back('請選擇要推送的內容!'); 79 } 80 81 82 //$App_Key = $this->config('App_Key'); 83 84 // 開始推送 85 $domain = "/pages/news-detail/news-detail?id="; 86 87 foreach ($list as $key => $value) { 88 89 $url =$domain . $value; 90 91 $this->log('百度小程式收錄推送:' . $url); 92 $post_urls .= $url . '<br/>'; 93 $flag_num++; 94 } 95 echo(substr($post_urls,0,-1)); 96 97 //$result = post_baidu($api, $post_urls); 98 case 'baixcx_chanpin': 99 $flag_num = 0; 100 $list = post('list'); 101 // $urls = post('urls'); 102 if (! $list) { 103 alert_back('請選擇要推送的內容!'); 104 } 105 106 107 // 開始推送 108 $domain = "/pages/product-detail/product-detail?id="; 109 110 foreach ($list as $key => $value) { 111 112 $url =$domain . $value; 113 114 $this->log('百度普通收錄推送:' . $url); 115 $post_urls .= $url . ','; 116 $flag_num++; 117 } 118 119 if(strlen($post_urls) > 5){ 120 $result = baiduMini(substr($post_urls,0,-1)); 121 122 if ($result->errno == 0 || $result->errno == '0') { 123 alert_back('成功推送' . $flag_num . '條'); 124 125 } else { 126 alert_back('百度小程式收錄推送發生錯誤:' . $result->msg); 127 } 128 }else{ 129 alert_back('百度小程式收錄推送發生錯誤_連結獲取失敗'); 130 } 131
function.php中新增提交方法,這裡可以多加一個引數,表示提交等級的引數
1 function baiduMini($url,$type_num=0){ 2 $str=file_get_contents('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='百度的key'&client_secret='百度的Secret'&scope=smartapp_snsapi_base'); 3 $array=json_decode($str,true); 4 $token=$array["access_token"]; 5 //$data['type'] = rand(0,1);//· 0:周級提交,一週左右生效; 1:天級提交,2~3 天生效; 2:小時級提交,1 小時內生效;3:優質資源提交,1 小時內生效 6 $data['type'] = $type_num; 7 $data['url_list'] = $url; 8 $api = 'https://openapi.baidu.com/rest/2.0/smartapp/access/submitsitemap/api?access_token='.$token; 9 $ch = curl_init(); 10 $options = array( 11 CURLOPT_URL => $api, 12 CURLOPT_POST => true, 13 CURLOPT_RETURNTRANSFER => true, 14 CURLOPT_NOBODY => false, 15 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, 16 CURLOPT_POSTFIELDS => http_build_query($data), 17 CURLOPT_SSL_VERIFYPEER => false, 18 CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'), 19 ); 20 curl_setopt_array($ch, $options); 21 $result = curl_exec($ch); 22 $data = json_encode($result, true); 23 24 return $data; 25 }
後臺樣式如下: