Example #1
0
 /**
  * 检验消息的真实性,并且获取解密后的明文.
  * <ol>
  *    <li>利用收到的密文生成安全签名,进行签名验证</li>
  *    <li>若验证通过,则提取xml中的加密消息</li>
  *    <li>对消息进行解密</li>
  * </ol>
  *
  * @param string $msgSignature  签名串,对应URL参数的msg_signature
  * @param string $timestamp     时间戳 对应URL参数的timestamp
  * @param string $nonce         随机串,对应URL参数的nonce
  * @param string $postXML       密文,对应POST请求的数据
  * @param string &$msg          解密后的原文,当return返回0时有效
  *
  * @return array
  */
 public function decryptMsg($msgSignature, $nonce, $timestamp, $postXML)
 {
     //提取密文
     $array = XML::parse($postXML);
     if (empty($array)) {
         throw new Exception('Invalid xml.', self::ERROR_PARSE_XML);
     }
     $encrypted = $array['Encrypt'];
     //验证安全签名
     $signature = $this->getSHA1(Wechat::getOption('token'), $timestamp, $nonce, $encrypted);
     if ($signature != $msgSignature) {
         throw new Exception('Invalid Signature.', self::ERROR_INVALID_SIGNATURE);
     }
     return XML::parse($this->decrypt($encrypted, Wechat::getOption('appId')));
 }
Example #2
0
 /**
  * 设置缓存文件前缀
  *
  * @return void
  */
 public function boot()
 {
     $this->filePrefix = Wechat::getOption('appId');
 }
Example #3
0
 /**
  * 通过code授权
  *
  * @param string $code
  *
  * @return array
  */
 protected function authorize($code)
 {
     if ($this->authResult) {
         return $this->authResult;
     }
     // 关闭自动加access_token参数
     Wechat::autoRequestToken(false);
     $params = array('appid' => Wechat::getOption('appId'), 'secret' => Wechat::getOption('secret'), 'code' => $code, 'grant_type' => 'authorization_code');
     $authResult = Wechat::request('GET', self::API_TOKEN_GET, $params);
     // 开启自动加access_token参数
     Wechat::autoRequestToken(true);
     //TODO:refresh_token机制
     return $this->authResult = $authResult;
 }