Ejemplo n.º 1
0
 /**
  * 输出
  *
  * @param \spWxResponse $response
  */
 public static function Output(spWxResponse $response)
 {
     if (isset($_GET['encrypt_type']) && $_GET['encrypt_type']) {
         $output = spWxTransportEncrypted::Output($response);
     } else {
         $output = spWxTransportPlain::Output($response);
     }
     spWxLogger::Log('spWxTransport::Output', ['output' => $output]);
     echo $output;
     exit;
 }
Ejemplo n.º 2
0
 /**
  * Default Exception Handler
  *
  * @param $exception
  */
 public static function ExceptionHandler($exception)
 {
     try {
         throw $exception;
     } catch (Exception $e) {
         if (php_sapi_name() == 'cli') {
             echo $e->getMessage() . '#' . $e->getCode() . "\n";
             exit;
         } else {
             spWxLogger::Log('Exception', ['Message' => $e->getMessage(), 'Code' => $e->getCode()]);
             echo json_encode(['code' => $e->getCode(), 'msg' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
             exit;
         }
     }
 }
Ejemplo n.º 3
0
 public static function Forward($url)
 {
     if (strpos($url, '?')) {
         $url .= '&';
     } else {
         $url .= '?';
     }
     foreach (self::$GET_FIELDS as $f) {
         if (isset($_GET[$f])) {
             $url .= $f . '=' . urlencode($_GET[$f]) . '&';
         }
     }
     $post = spWxTransport::Input();
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
     curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type' => $_SERVER['HTTP_CONTENT_TYPE']]);
     $s = curl_exec($ch);
     $i = curl_getinfo($ch);
     curl_close($ch);
     spWxLogger::Log('spWxRequestForwarder', ['url' => $url, 'data' => $post, 'result' => $s, 'info' => $i]);
     return $s;
 }
Ejemplo n.º 4
0
 /**
  * JS SDK ticket
  *
  * @param string $request_url
  * @param string $type             jsapi, wx_card
  * @return bool|mixed|string
  * @throws \spWxException
  */
 public function getJSSDKTicket($request_url = '', $type = 'jsapi')
 {
     $token = $this->getAccessToken();
     $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=' . $type . '&access_token=' . $token;
     $cache_key = 'wx:ticket:' . $type . ':' . md5($url);
     $jsapi_ticket = spWxCache::get($cache_key);
     $jsapi_ticket = json_decode($jsapi_ticket, true);
     if (!$jsapi_ticket) {
         $jsapi_ticket = spWxHttpUtil::http_get($url);
         spWxLogger::LogHttpGet($url, $jsapi_ticket);
         if (isset($jsapi_ticket['errcode']) && $jsapi_ticket['errcode'] == '40001') {
             $this->delAccessToken();
             return $this->getJSSDKTicket($request_url);
         }
         if (!isset($jsapi_ticket['ticket']) || !isset($jsapi_ticket['expires_in'])) {
             throw new spWxException('服务器故障,请稍后重试', 9210201);
         }
         spWxCache::set($cache_key, json_encode($jsapi_ticket), $jsapi_ticket['expires_in'] - 300);
     }
     $noncestr = md5(uniqid('sskaje', true));
     $jsapi_ticket['noncestr'] = $noncestr;
     $jsapi_ticket['timestamp'] = time();
     $signstr = 'jsapi_ticket=' . $jsapi_ticket['ticket'] . '&noncestr=' . $jsapi_ticket['noncestr'] . '&timestamp=' . $jsapi_ticket['timestamp'];
     if (empty($request_url)) {
         $signstr .= '&url=' . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     } else {
         $signstr .= '&url=' . $request_url;
     }
     $jsapi_ticket['signature'] = sha1($signstr);
     $jsapi_ticket['signstr'] = $signstr;
     $jsapi_ticket['appid'] = $this->app_id;
     return $jsapi_ticket;
 }