function getSignature($jsapi_ticket, $url, $timestamp = 0, $noncestr = '') { if (!$timestamp) { $timestamp = time(); } if (!$noncestr) { $noncestr = createNonceStr(); } $ret = strpos($url, '#'); if ($ret) { $url = substr($url, 0, $ret); } $url = trim($url); if (empty($url)) { return false; } $arrdata = array("timestamp" => $timestamp, "noncestr" => $noncestr, "url" => $url, "jsapi_ticket" => $jsapi_ticket); ksort($arrdata); $paramstring = ""; foreach ($arrdata as $key => $value) { if (strlen($paramstring) == 0) { $paramstring .= $key . "=" . $value; } else { $paramstring .= "&" . $key . "=" . $value; } } $sign = sha1($paramstring); if (!$sign) { return false; } return $sign; }
function getSignPackage($appId, $appSecret, $url) { $jsapiTicket = getJsApiTicket($appId, $appSecret); //$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = createNonceStr(); $url = str_replace('@', '&', $url); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket={$jsapiTicket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}"; $signature = sha1($string); $signPackage = array("appId" => $appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string); return $signPackage; }
public function share() { $time_stamp = time(); $jsapi_ticket = $this->wxinterface->getJsApiTicket(APPID, APPSECRET); $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://'; $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $nonce_str = createNonceStr(); $signature_str = getSignature($jsapi_ticket, $url, $time_stamp, $nonce_str); $data['appid'] = APPID; $data['timestamp'] = $time_stamp; $data['noncestr'] = $nonce_str; $data['signature'] = $signature_str; $data['url'] = $url; $data['title'] = '分享'; $data['template'] = 'wx/wxtest'; $this->data = array_merge($data, $this->_share()); $this->_show_page(); }
/** * 微信网页JSSDK 获取签名字符串 * 所有参数名均为小写字符 * @param unknown $nonceStr 随机字符串 * @param unknown $timestamp 时间戳 * @param unknown $jsapi_ticket * @param unknown $url 调用JS接口页面的完整URL,不包含#及其后面部分 */ 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 = $this->get_accesstoken($APP_ID, $APP_SECRET); $argu['jsapi_ticket'] = $this->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)); $argu['token'] = $ACCESS_TOKEN; return $argu; }
function getSignPackage() { $wx = C('weixin'); $jsapiTicket = getJsApiTicket(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $url = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $timestamp = time(); $nonceStr = createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket={$jsapiTicket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}"; $signature = sha1($string); $signPackage = array("appId" => $wx['appid'], "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string); return $signPackage; }
$checkRetJson = https_request("https://api.weixin.qq.com/sns/auth?access_token={$userBase['access_token']}&openid={$userBase['openid']}"); //error_log("checkRetJson:".var_export($checkRetJson,1)." \n\n",3,"/tmp/wm_debug.log"); $checkRet = json_decode($checkRetJson, true); //error_log("checkRet:".var_export($checkRet,1)." \n\n",3,"/tmp/wm_debug.log"); //刷新token if ($checkRet['errcode'] != 0) { $userBase = json_decode($this->https_request("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$weixin_appId}&grant_type=refresh_token&refresh_token={$userBase['refresh_token']}"), true); } //拉取用户信息(需scope为 snsapi_userinfo) $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$userBase['access_token']}&openid={$userBase['openid']}&lang=zh_CN"; $userJson = https_request($url); //error_log("userJson:".var_export($userJson,1)." \n\n",3,"/tmp/wm_debug.log"); $user = json_decode($userJson, true); //设置js参数 $timeSt = time(); $randomstr = createNonceStr(); $jsapiTicket = getJSApiTicket(getAccess($weixin_appId, $weixin_appSecret, 1)); //error_log("jsapiTicket:".var_export($jsapiTicket,1)." \n\n",3,"/tmp/wm_debug.log"); $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $signUrl = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; //error_log($signUrl."---\n",3,"/tmp/sing.txt"); $signature1 = sha1("jsapi_ticket={$jsapiTicket}&noncestr={$randomstr}×tamp={$timeSt}&url={$signUrl}"); //error_log("user:"******" \n\n",3,"/tmp/wm_debug.log"); //$this->display('weixinApi.showShareGame.html'); //} elseif($state == 3) { $name = $user['nickname']; $headImg = $user['headimgurl']; $openId = $user['openid']; //生成三张牌 if (rand(1, 100) <= 3) { $card = rand(0, 33);