//填写微信分配的公众账号ID * 'mch_id' => //填写微信支付分配的商户号 * 'notify_url'=> //填写微信支付结果回调地址 * 'key' => //填写微信商户支付密钥 * ); * 统一下单方法 * $WechatAppPay = new wechatAppPay($options); * $params['body'] = '商品描述'; //商品描述 * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复 * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分 * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP * $wechatAppPay->unifiedOrder( $params ); */ class weixin_public { private $appid; private $seceret; private $uri; public function __construct($appid, $seceret, $uri) { $this->appid = $appid; $this->seceret = $seceret; $this->uri = $uri; } //获取code public function redirectWithCode() { $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid ."&redirect_uri=".urlencode($this->uri) ."&response_type=code&scope=snsapi_userinfo&state=".time()."#wechat_redirect"; return $url; } private function getAcessTokenAndOpenId($code) { $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid ."&secret=".$this->seceret."&code=".$code."&grant_type=authorization_code"; return file_get_contents($url); //return json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 } public function getAllInfo($code) { $info = self::getAcessTokenAndOpenId($code); return $info; if (empty($info['access_token']) || empty($info['openid'])) { return $info; } $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$info['access_token']."&openid=".$info['openid']."&lang=zh_CN"; $jsonInfo = file_get_contents($url); //$jsonInfo = iconv('ISO-8859-1', 'UTF-8', $jsonInfo); $ret = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 return $ret; } }