1. 不用php怎麼調用微信jssdk
使用姿勢
^ajax(Common.ServerUrl+"GetWX.php",{
data:{
Type:"config",
url:location.href.split('#')[0]
},
dataType:'json',
type:'get',
timeout:5000,
success:function(data){
wx.config({
debug:true,//開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會列印。
appId:'……',//必填,公眾號的唯一標識
timestamp:data.timestamp,//必填,生成簽名的時間戳
nonceStr:data.nonceStr,//必填,生成簽名的隨機串
signature:data.signature,//必填,簽名,見附錄1
jsApiList:["getLocation"]//必填,需要使用的JS介面列表,所有JS介面列表見附錄2
});
}
})
wx.ready(function(){
wx.getLocation({
type:'wgs84',//默認為wgs84的gps坐標,如果要返回直接給openLocation用的火星坐標,可傳入'gcj02'
success:function(res){
varlatitude=res.latitude;//緯度,浮點數,范圍為90~-90
varlongitude=res.longitude;//經度,浮點數,范圍為180~-180。
plus2.storage.setItem("latitude",latitude);
plus2.storage.setItem("longitude",longitude);
}
});
});
服務端GetWX.PHP
<?php
include"lib/Cache.php";
define($APPID,"……");
define($SECRET,"……")
if($_GET['Type']=="access_token"){//echogetAccess_token();
}
elseif($_GET['Type']=="jsapi_ticket"){//echogetJsapi_ticket();
}
elseif($_GET['Type']=="config"){
$jsapi_ticket=getJsapi_ticket();
$nonceStr="x".rand(10000,100000)."x";//隨機字元串
$timestamp=time();//時間戳
$url=$_GET['url'];
$signature=getSignature($jsapi_ticket,$nonceStr,$timestamp,$url);
$result=array("jsapi_ticket"=>$jsapi_ticket,"nonceStr"=>$nonceStr,"timestamp"=>$timestamp,"url"=>$url,"signature"=>$signature);
echojson_encode($result);
}
functiongetSignature($jsapi_ticket,$noncestr,$timestamp,$url){
$string1="jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url;
$sha1=sha1($string1);
return$sha1;
}
functiongetJsapi_ticket(){
$cache=newCache();
$cache=newCache(7000,'cache/');//需要創建cache文件夾存儲緩存文件。
//從緩存從讀取鍵值$key的數據
$jsapi_ticket=$cache->get("jsapi_ticket");
$access_token=getAccess_token();
//如果沒有緩存數據
if($jsapi_ticket==false){
$access_token=getAccess_token();
$url='https://api.weixin.qq.com/cgi-bin/ticket/getticket';
$data=array('type'=>'jsapi','access_token'=>$access_token);
$header=array();
$response=json_decode(curl_https($url,$data,$header,5));
$jsapi_ticket=$response->ticket;
//寫入鍵值$key的數據
$cache->put("jsapi_ticket",$jsapi_ticket);
}
return$jsapi_ticket;
}
functiongetAccess_token(){
$cache=newCache();
$cache=newCache(7000,'cache/');
//從緩存從讀取鍵值$key的數據
$access_token=$cache->get("access_token");
//如果沒有緩存數據
if($access_token==false){
$url='https://api.weixin.qq.com/cgi-bin/token';
$data=array('grant_type'=>'client_credential','appid'=>$APPID,'secret'=>$SECRET);
$header=array();
$response=json_decode(curl_https($url,$data,$header,5));
$access_token=$response->access_token;
//寫入鍵值$key的數據
$cache->put("access_token",$access_token);
}
return$access_token;
}
/**curl獲取https請求
*@paramString$url請求的url
*@paramArray$data要發送的數據
*@paramArray$header請求時發送的header
*@paramint$timeout超時時間,默認30s
*/
functioncurl_https($url,$data=array(),$header=array(),$timeout=30){
$ch=curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//跳過證書檢查
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
$response=curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
return$response;
}
?>
Cache.php
<?phpclassCache{
private$cache_path;
//pathforthecache
private$cache_expire;
//secondsthatthecacheexpires
//cacheconstructor,
publicfunctionCache($exp_time=3600,$path="cache/"){
$this->cache_expire=$exp_time;
$this->cache_path=$path;
}
//returnsthefilenameforthecache
privatefunctionfileName($key){
return$this->cache_path.md5($key);
}
//,$key==nameofthecache,datatheinfo/valuestostore
publicfunctionput($key,$data){
$values=serialize($data);
$filename=$this->fileName($key);
$file=fopen($filename,'w');
if($file){//abletocreatethefile
fwrite($file,$values);
fclose($file);
}else
returnfalse;
}
//returnscacheforthegivenkey
publicfunctionget($key){
$filename=$this->fileName($key);
if(!file_exists($filename)||!is_readable($filename)){//can'treadthecache
returnfalse;
}
if(time()<(filemtime($filename)+$this->cache_expire)){//cacheforthekeynotexpired
$file=fopen($filename,"r");
//readdatafile
if($file){//abletoopenthefile
$data=fread($file,filesize($filename));
fclose($file);
returnunserialize($data);
//returnthevalues
}else
returnfalse;
}else
returnfalse;
//wasexpiredyouneedtocreatenew
}
}?>
2. PHP 的API介面
使用PHP寫api介面是經常做的,PHP寫好介面後,前台就可以通過鏈接獲取介面提供的數據,而返回的數據一般分為兩種情況,xml和json,在這個過程中,伺服器並不知道,請求的來源是什麼,有可能是別人非法調用我們的介面,獲取數據,因此就要使用安全驗證
原理
從圖中可以看得很清楚,前台想要調用介面,需要使用幾個參數生成簽名。
時間戳:當前時間
隨機數:隨機生成的隨機數
口令:前後台開發時,一個雙方都知道的標識,相當於暗號
演算法規則:商定好的運算規則,上面三個參數可以利用演算法規則生成一個簽名。前台生成一個簽名,當需要訪問介面的時候,把時間戳,隨機數,簽名通過URL傳遞到後台。後台拿到時間戳,隨機數後,通過一樣的演算法規則計算出簽名,然後和傳遞過來的簽名進行對比,一樣的話,返回數據。
演算法規則
在前後台交互中,演算法規則是非常重要的,前後台都要通過演算法規則計算出簽名,至於規則怎麼制定,看你怎麼高興怎麼來。
我這個演算法規則是
時間戳,隨機數,口令按照首字母大小寫順序排序
然後拼接成字元串
進行sha1加密
再進行MD5加密
轉換成大寫。