Beispiel #1
0
 /**
  * 发送content
  */
 public function send($callback = null)
 {
     if ($callback && is_callable($callback)) {
         $callback($this);
     }
     if (is_array($this->content) || is_object($this->content)) {
         $output = '<xml>' . self::buildXML((array) $this->content) . '</xml>';
     } else {
         $output = (string) $this->content;
     }
     if ($this->encrypted) {
         try {
             $prpcrypt = new Prpcrypt($this->wechat->getConfig('aeskey'));
             $output = $prpcrypt->encrypt($output, $this->wechat->getConfig('appid'));
             $output = $this->encrypt($output);
         } catch (Exception $e) {
         }
     }
     echo $output;
     return $this;
 }
Beispiel #2
0
 /**
  * 执行
  */
 public function handle($request)
 {
     $useEncode = true;
     $encrypted = $request->getParameter('Encrypt');
     //检查签名
     $signResult = $this->checkSignature($encrypted);
     if ($signResult === false) {
         exit('Error - WeChat Signature');
     } else {
         if ($signResult !== true) {
             echo $signResult;
             exit;
         }
     }
     //解密数据
     try {
         $prpcrypt = new Prpcrypt($this->getConfig('aeskey'));
         $xmlData = $prpcrypt->decrypt($encrypted, $this->getConfig('appid'));
     } catch (Exception $e) {
         exit('Error - ' . $e->getCode() . $e->getMessage());
     }
     $request->initialize($xmlData);
     $request->wechat = $this;
     //拼装事件
     $event = self::getHandleEvent($request);
     //执行回调
     $agentid = $request->getParameter('AgentID');
     $callback = isset($this->events[$event]) ? $this->events[$event] : false;
     $callback = isset($this->events[$agentid][$event]) ? $this->events[$agentid][$event] : $callback;
     if ($callback && is_object($callback) && get_class($callback) == 'Closure') {
         $response = $callback($request);
     } elseif ($callback) {
         $response = call_user_func($callback, $request);
     } elseif ($this->fallbacks) {
         $response = '';
         foreach ($this->fallbacks as $fallback) {
             if (is_callable(array($fallback, $event))) {
                 $response = call_user_func(array($fallback, $event), $request);
                 break;
             }
         }
     } else {
         $response = '';
     }
     //返回Response
     if (!$response instanceof Response) {
         $response = new Response($response);
     }
     $response->wechat = $this;
     $response->request = $request;
     $response->setEncrypt($useEncode);
     return $response;
 }