<?php
include
"lib/Cache.php"
;
define(
$APPID
,
"……"
);
define(
$SECRET
,
"……"
)
if
(
$_GET
['Type'] ==
"access_token"
){
}
else
if
(
$_GET
['Type'] ==
"jsapi_ticket"
){
}
else
if
(
$_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
);
echo
json_encode(
$result
);
}
function
getSignature(
$jsapi_ticket
,
$noncestr
,
$timestamp
,
$url
){
$string1
=
"jsapi_ticket="
.
$jsapi_ticket
.
"&noncestr="
.
$noncestr
.
"×tamp="
.
$timestamp
.
"&url="
.
$url
;
$sha1
= sha1(
$string1
);
return
$sha1
;
}
function
getJsapi_ticket(){
$cache
=
new
Cache();
$cache
=
new
Cache(7000, 'cache/');
$jsapi_ticket
=
$cache
-> get(
"jsapi_ticket"
);
$access_token
= getAccess_token();
if
(
$jsapi_ticket
== false) {
$access_token
= getAccess_token();
$url
= 'https:
$data
=
array
('type'=>'jsapi','access_token'=>
$access_token
);
$header
=
array
();
$response
= json_decode(curl_https(
$url
,
$data
,
$header
, 5));
$jsapi_ticket
=
$response
->ticket;
$cache
-> put(
"jsapi_ticket"
,
$jsapi_ticket
);
}
return
$jsapi_ticket
;
}
function
getAccess_token(){
$cache
=
new
Cache();
$cache
=
new
Cache(7000, 'cache/');
$access_token
=
$cache
-> get(
"access_token"
);
if
(
$access_token
== false) {
$url
= 'https:
$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;
$cache
-> put(
"access_token"
,
$access_token
);
}
return
$access_token
;
}
function
curl_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
;
}
?>