/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $params = array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl()); $query = urldecode(http_build_query($params)); $config['signature'] = sha1($query); $this->config = $config; }
/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $signGenerator = new SignGenerator(array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl())); $signGenerator->setHashType('sha1'); $signGenerator->setUpper(false); $config['signature'] = $signGenerator->getResult(); $this->config = $config; }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $options) { if (!$wechat->offsetExists('mchid')) { throw new \InvalidArgumentException('The required options "mch_id" are missing.'); } if (!$wechat->offsetExists('mchkey')) { throw new \InvalidArgumentException('The required options "mch_key" are missing.'); } $this->wechat = $wechat; parent::__construct($options); }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $options) { if (!$wechat->offsetExists('mchid')) { throw new \InvalidArgumentException('The required options "mch_id" are missing.'); } if (!$wechat->offsetExists('mchkey')) { throw new \InvalidArgumentException('The required options "mch_key" are missing.'); } if (!$wechat->offsetExists('authenticate_cert')) { throw new \InvalidArgumentException('The required options "authenticate_cert" are missing.'); } $defaults = array('wxappid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'nonce_str' => uniqid(), 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'); $validator = new OptionValidator(); $validator->setDefined($this->required)->setRequired($this->required)->setDefaults($defaults); $validated = $validator->validate($options); $this->wechat = $wechat; $this->options = $validated; }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $options) { if (!$wechat->offsetExists('mchid')) { throw new \InvalidArgumentException('The required options "mchid" are missing.'); } if (!$wechat->offsetExists('mchkey')) { throw new \InvalidArgumentException('The required options "mchkey" are missing.'); } $defaults = array('appid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'time_stamp' => time(), 'nonce_str' => uniqid()); $resolver = new OptionsResolver(); $resolver->setDefined($this->required)->setRequired($this->required)->setDefaults($defaults); $options = $resolver->resolve($options); // 按 ASCII 码排序 ksort($options); $signature = http_build_query($options); $signature = urldecode($signature); $signature = strtoupper(md5($signature . '&key=' . $wechat['mchkey'])); $options['sign'] = $signature; $this->options = $options; }
/** * 构造方法 */ public function __construct(Wechat $wechat, array $options) { if (!$wechat->offsetExists('mchid')) { throw new \InvalidArgumentException('The required options "mch_id" are missing.'); } if (!$wechat->offsetExists('mchkey')) { throw new \InvalidArgumentException('The required options "mch_key" are missing.'); } $normalizer = function ($options, $value) { if ($value === 'JSAPI' && !isset($options['openid'])) { throw new \InvalidArgumentException('The required options "openid" are missing.'); } return $value; }; $defaults = array('appid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'spbill_create_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0', 'trade_type' => current($this->tradeType), 'nonce_str' => uniqid()); $validator = new OptionValidator(); $validator->setDefined($this->defined)->setRequired($this->required)->setAllowedValues('trade_type', $this->tradeType)->setNormalizer('trade_type', $normalizer)->setDefaults($defaults); $validated = $validator->validate($options); $this->wechat = $wechat; $this->options = $validated; }
public static function getJssdk(Wechat $wechat, $apis, $debug = false, $asArray = false) { try { $ticket = $wechat->getTicket('jsapi'); } catch (TicketException $e) { exit($e->getMessage()); } $bag = new Bag(); $bag->set('jsapi_ticket', $ticket); $bag->set('timestamp', time()); $bag->set('noncestr', Util::randomString()); $bag->set('url', Util::currentUrl()); $signGenerator = new SignGenerator($bag); $signGenerator->setUpper(false); $signGenerator->setHashType('sha1'); $config = array('appId' => $wechat->getAppid(), 'nonceStr' => $bag->get('noncestr'), 'timestamp' => $bag->get('timestamp'), 'signature' => $signGenerator->getResult(), 'jsApiList' => $apis); if ($debug) { $config['debug'] = true; } return $asArray ? $config : JSON::encode($config); }
<?php require './config.php'; use Thenbsp\Wechat\Wechat; use Thenbsp\Wechat\Exception\AccessTokenException; /** * 获取 AccessToken */ $o = new Wechat(APPID, APPSECRET); $accessToken = $o->getAccessToken(); var_dump($accessToken); /** * AccessToken 异常 */ // try { // $accessToken = $o->getAccessToken(); // } catch (AccessTokenException $e) { // exit($e->getMessage()); // } // var_dump('AccessToken: '.$accessToken);
<?php require './config.php'; use Thenbsp\Wechat\Wechat; use Thenbsp\Wechat\Exception\TicketException; /** * 获取众公号 Ticket(ticket 分为 jsapi 和 wx_card,getTicket 方法可传入一个可选参数) */ $o = new Wechat(APPID, APPSECRET); try { $ticket = $o->getTicket(); } catch (TicketException $e) { exit($e->getMessage()); } var_dump($ticket);
<?php require './config.php'; use Thenbsp\Wechat\Wechat; use Thenbsp\Wechat\Exception\TicketException; use Thenbsp\Wechat\Exception\AccessTokenException; /** * 获取 Ticket */ $o = new Wechat(APPID, APPSECRET); // 公众号 ticket 分为 jsapi 和 wx_card,getTicket 方法可传入一个可选参数 $ticket = $o->getTicket('jsapi'); var_dump($ticket); /** * Ticket 异常 */ // try { // $ticket = $o->getTicket(); // } catch (AccessTokenException $e) { // exit($e->getMessage()); // } catch (TicketException $e) { // exit($e->getMessage()); // } // var_dump$ticket);
<?php require './config.php'; use Thenbsp\Wechat\Wechat; /** * 获取微信服务器 IP */ $o = new Wechat(APPID, APPSECRET); try { $ip = $o->getServerIp(); } catch (AccessTokenException $e) { exit($e->getMessage()); } var_dump($ip);