微信签名的设置

时间:2017-09-12 17:49:04   收藏:0   阅读:345
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."&timestamp=".$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;
      }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!