作者:天涯s1_278 | 来源:互联网 | 2023-09-18 21:17
function curlSend($url,$post_data=""){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
if($post_data != ""){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_accesstoken($APP_ID,$APP_SECRET){
$ACCESS_TOKEN = $APP_ID;
if($ACCESS_TOKEN ){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APP_ID."&secret=".$APP_SECRET;
$json = curlSend($url);
$data=json_decode($json,true);
$ACCESS_TOKEN = $data[access_token];
}
return $ACCESS_TOKEN;
}
function get_jsapi_ticket($ACCESS_TOKEN){
$jsapi_ticket = $ACCESS_TOKEN;
if($jsapi_ticket){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$ACCESS_TOKEN."&type=jsapi";
$json = curlSend($url);
$data = json_decode($json,true);
$jsapi_ticket = $data[ticket];
}
return $jsapi_ticket;
}
function get_js_sdk($APP_ID,$APP_SECRET){
$protocol = (!empty($_SERVER[HTTPS]) && $_SERVER[HTTPS] !== off || $_SERVER[SERVER_PORT] == 443) ? "https://" : "http://";
$url = $protocol.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$argu = array();
$argu[appId] = $APP_ID;
$argu[url] = $url;
$argu[nonceStr] = createNonceStr();
$argu[timestamp] = time();
$ACCESS_TOKEN = get_accesstoken($APP_ID, $APP_SECRET);
$argu[jsapi_ticket] = get_jsapi_ticket($ACCESS_TOKEN);
$string = "jsapi_ticket=".$argu[jsapi_ticket]."&nOncestr=".$argu[nonceStr]."×tamp=".$argu[timestamp]."&url=".$argu[url];
$argu[signature] = sha1(trim($string));
return $argu;
}
function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i <$length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
$confinfo =get_js_sdk("appid","APP_SECRET");
?>