php一個(gè)文件搞定微信jssdk配置_第1頁
php一個(gè)文件搞定微信jssdk配置_第2頁
php一個(gè)文件搞定微信jssdk配置_第3頁
php一個(gè)文件搞定微信jssdk配置_第4頁
php一個(gè)文件搞定微信jssdk配置_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

本文格式為Word版,下載可任意編輯——php一個(gè)文件搞定微信jssdk配置php一個(gè)文件搞定微信jssdk配置

學(xué)習(xí)PHP的小伙伴在處理微信jssdk配置的時(shí)候可能會(huì)遇到一些問題。以下是我用心為大家整理的php一個(gè)文件搞定微信jssdk配置,夢想對大家有所扶助!更多內(nèi)容請關(guān)注應(yīng)屆畢業(yè)生網(wǎng)!

包括緩存,包括https通訊,獲取微信access_token,簽名什么的都有。但是防范性編程做得對比少,商業(yè)用的話,需要完善下代碼。

使用模樣

?

^ajaxCommon.ServerUrl+GetWX.php,

data:

Type:config,

url:location.href.split#[0]

,

dataType:json,

type:get,

timeout:5000,

success:functiondata

wx.config

debug:true,//開啟調(diào)試模式,調(diào)用的全體api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端開啟,參數(shù)信息會(huì)通過log打出,僅在pc端時(shí)才會(huì)打印。

appId:,//必填,公眾號的唯一標(biāo)識

timestamp:data.timestamp,//必填,生成簽名的時(shí)間戳

nonceStr:data.nonceStr,//必填,生成簽名的隨機(jī)串

signature:data.signature,//必填,簽名,見附錄1

jsApiList:[getLocation]//必填,需要使用的JS接口列表,全體JS接口列表見附錄2

;

wx.readyfunction

wx.getLocation

type:wgs84,//默認(rèn)為wgs84的`gps坐標(biāo),假設(shè)要返回直接給openLocation用的火星坐標(biāo),可傳入gcj02

success:functionres

varlatitude=res.latitude;//緯度,浮點(diǎn)數(shù),范圍為90~-90

varlongitude=res.longitude;//經(jīng)度,浮點(diǎn)數(shù),范圍為180~-180。

plus2.storage.setItemlatitude,latitude;

plus2.storage.setItemlongitude,longitude;

;

;

服務(wù)端

GetWX.php

?

includelib/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.rand10000,100000.x;//隨機(jī)字符串

$timestamp=time;//時(shí)間戳

$url=$_GET[url];

$signature=getSignature$jsapi_ticket,$nonceStr,$timestamp,$url;

$result=arrayjsapi_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=newCache7000,cache/;//需要?jiǎng)?chuàng)造cache文件夾存儲緩存文件。

//從緩存從讀取鍵值$key的數(shù)據(jù)

$jsapi_ticket=$cache-getjsapi_ticket;

$access_token=getAccess_token;

//假設(shè)沒有緩存數(shù)據(jù)

if$jsapi_ticket==false

$access_token=getAccess_token;

$url=/cgi-bin/ticket/getticket;

$data=arraytype=jsapi,access_token=$access_token;

$header=array;

$response=json_decodecurl_https$url,$data,$header,5;

$jsapi_ticket=$response-ticket;

//寫入鍵值$key的數(shù)據(jù)

$cache-putjsapi_ticket,$jsapi_ticket;

return$jsapi_ticket;

functiongetAccess_token

$cache=newCache;

$cache=newCache7000,cache/;

//從緩存從讀取鍵值$key的數(shù)據(jù)

$access_token=$cache-getaccess_token;

//假設(shè)沒有緩存數(shù)據(jù)

if$access_token==false

$url=/cgi-bin/token;

$data=arraygrant_type=client_credential,appid=$APPID,secret=$SECRET;

$header=array;

$response=json_decodecurl_https$url,$data,$header,5;

$access_token=$response-access_token;

//寫入鍵值$key的數(shù)據(jù)

$cache-putaccess_token,$access_token;

return$access_token;

/**curl獲取https苦求

*@paramString$url苦求的url

*@paramArray$data要發(fā)送的數(shù)據(jù)

*@paramArray$header苦求時(shí)發(fā)送的header

*@paramint$timeout超時(shí)時(shí)間,默認(rèn)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

不知道哪位寫的源代碼~

?

classCache

private$cache_path;

//pathforthecache

private$cache_expire;

//secondsthatthecacheexpires

//cacheconstructor,optionalexpiringtimeandcachepath

publicfunctionCache$exp_time=3600,$path=cache/

$this-cache_expire=$exp_time;

$this-cache_path=$path;

//returnsthefilenameforthecache

privatefunctionfileName$key

return$this-cache_path.md5$key;

//createsnewcachefileswiththegivendata,$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//cantreadthecache

returnfalse;

iftimefilemtime$filename+$this-cache_expire//cacheforthekeynotexpired

$file=fopen$filename,r;

//readdata

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論