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加密
转换成大写。