标签:
<?php header("Content-type: text/html; charset=utf-8"); if($_GET[‘appId‘] && $_GET[‘appSecret‘]){ $appId=$_GET[‘appId‘] ; $appSecret=$_GET[‘appSecret‘]; }else{ echo "请输入appid和appSecret"; exit; } $jsapiTicket = getJsApiTicket($appId,$appSecret); $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); $data_json=json_encode($signPackage,true); echo $data_json; function getJsApiTicket($appId,$appSecret) { $memcache_obj = memcache_connect("localhost", 11211); $jsapi_ticket=$memcache_obj->get(‘jsapi_ticket‘); if(!$jsapi_ticket){ $accessToken = getAccessToken($appId,$appSecret); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode(httpGet($url)); $jsapi_ticket = $res->ticket; if ($jsapi_ticket) { $memcache_obj->set(‘jsapi_ticket‘, $jsapi_ticket, false, 7000); } } return $jsapi_ticket; } function getAccessToken($appId,$appSecret) { $memcache_obj = memcache_connect("localhost", 11211); $access_token=$memcache_obj->get(‘access_token‘); if(!$access_token){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret"; $res = json_decode(httpGet($url)); $access_token = $res->access_token; if ($access_token) { $memcache_obj->set(‘access_token‘, $ticket, false, 7000); } } return $access_token; } 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; } function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; }
标签:
原文地址:http://www.cnblogs.com/yutaoyao/p/4286448.html