原創(chuàng)聲明:本文為作者原創(chuàng),未經(jīng)允許不得轉(zhuǎn)載,經(jīng)授權(quán)轉(zhuǎn)載需注明作者和出處
1:win2008+IIS7環(huán)境只能搭建一個(gè)https站點(diǎn)
2:小程序后臺(tái)必須用https搭建
ps:win2008+IIS有搭建多個(gè)https的偏方,不過得IIS修改配置文件,一重啟IIS得重新配置,很不方便`
正常來說:
一個(gè)win2008+IIS服務(wù)器只能讓一個(gè)小程序用
解決方法:讓這個(gè)唯一的https站點(diǎn)做路由
1:https站點(diǎn):接收小程序發(fā)來的請(qǐng)求參數(shù),根據(jù)參數(shù)重新組織請(qǐng)求url
2:https站點(diǎn):模擬http向真正的小程序后臺(tái)發(fā)送請(qǐng)求
3:http站點(diǎn):小程序后臺(tái)接收參數(shù)、返回請(qǐng)求的數(shù)據(jù)
4:https站點(diǎn):接收返回的數(shù)據(jù)
5:https站點(diǎn):把數(shù)據(jù)返回給小程序
url:https://www.https.com/api.php?app=zcml&action=post&cat=17&count=2&page=1
app:小程序縮寫
action:功能(獲取文章,獲取分類,獲取幻燈片數(shù)據(jù)等)
cat:文章分類ID
count:每次獲取數(shù)量
page:頁數(shù)
<?php
//每次請(qǐng)求必有參數(shù)(小程序名、功能)
$app = $_GET['app']; //應(yīng)用名稱縮寫
$action = $_GET['action']; //功能
/********************
* 1:拼裝url
********************/
switch($app){
//《軸承名錄》小程序
case "zcml":
//功能
switch($action){
//獲取文章數(shù)據(jù)
case "post":
$cat=$_GET['cat']; //分類id
$count=$_GET['count']; //每頁數(shù)據(jù)量
$page=$_GET['page']; //頁數(shù)
$url = "http://www.http.com/xcxapi.php?action={$action}&cat={$cat}&count={$count}&page={$page}";
break;
//獲取xx數(shù)據(jù)
case "index":
break;
}
break;
//其他小程序(待)
case "other":
break;
}
/********************
* 2:模擬http獲取數(shù)據(jù)
********************/
$timeout = 5;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //設(shè)置訪問的url地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //參數(shù)為1表示傳輸數(shù)據(jù),為0表示直接輸出顯示。
curl_setopt($ch, CURLOPT_HEADER, 0); //參數(shù)為0表示不帶頭文件,為1表示帶頭文件
/*curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //獲取https需要加上此
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //獲取https需要加上此*/
$output = curl_exec($ch); //執(zhí)行命令并把獲取的數(shù)據(jù)賦值給$output
curl_close($ch); //關(guān)閉URL請(qǐng)求
echo $output;
?>
<?php
/***wp框架************/
if ( !isset($wp_did_header) ) {
$wp_did_header = true;
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
}
/***wp框架************/
//獲取功能
$action = $_GET['action'];
switch($action){
//獲取文章數(shù)據(jù)
case "post":
//接收傳參
$cat=$_GET['cat'];
$count=$_GET['count'];
$page=$_GET['page'];
//拼裝篩選參數(shù)
$args = array(
'posts_per_page' => $count, //每頁數(shù)量
'paged' => $page, //第幾頁
'post_type' => 'dealer', //自定義文章類型名稱(可省略自動(dòng)識(shí)別)
'tax_query' => array( //自定義分類(指定)
array(
'taxonomy' => 'cdealer',//自定義分類法名稱
'terms' =>$cat //id為64的分類。也可是多個(gè)分類array(12,64)
),
)
);
//執(zhí)行時(shí)篩選
query_posts($args); //本頁不要這句,自定義分類才用
$arr=[];
//對(duì)文章數(shù)據(jù)進(jìn)行重新封裝
while(have_posts()){
the_post();
$title = get_the_title();
$sale = get_field("sale");
$tel = get_field("tel");
$addr = get_field("addr");
$arr[] = ["title"=>$title,"sale"=>$sale,"tel"=>$tel,"addr"=>$addr];
}
echo json_encode($arr);
break;
//其他
case "other":
break;
}