public function actionStationShare(){ $appId = "wxd9d820ef5950a00a"; $Secret = "addfa2636c1e9a0ee63e89fe2e5c9b0b"; $url = Yii::$app->request->get(‘url‘); $token = $this->getAccessToken($appId,$Secret); $ticket = $this->getJsApiTicket($token); $timestamp = time(); $nonceStr = $this->createNonceStr(); $string = "jsapi_ticket=".$ticket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url; $signature = sha1($string); $signPackage = array( "appId" => $appId, "noncestr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return Helper::format_data ( SUCC, $signPackage ); }
private function getAccessToken($appId,$Secret) { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 // 如果是企业号用以下URL获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$Secret; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; return $access_token; }
private function getJsApiTicket($token) { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 // 如果是企业号用以下 URL 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$token"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$token; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; return $ticket; }
private 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; }
时间: 2024-11-02 23:28:52