weixin_public.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * 微信支付服务器端下单
  4. * 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
  5. * 使用示例
  6. * 构造方法参数
  7. * 'appid' => //填写微信分配的公众账号ID
  8. * 'mch_id' => //填写微信支付分配的商户号
  9. * 'notify_url'=> //填写微信支付结果回调地址
  10. * 'key' => //填写微信商户支付密钥
  11. * );
  12. * 统一下单方法
  13. * $WechatAppPay = new wechatAppPay($options);
  14. * $params['body'] = '商品描述'; //商品描述
  15. * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复
  16. * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
  17. * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
  18. * $wechatAppPay->unifiedOrder( $params );
  19. */
  20. class weixin_public
  21. {
  22. private $appid;
  23. private $seceret;
  24. private $uri;
  25. public function __construct($appid, $seceret, $uri)
  26. {
  27. $this->appid = $appid;
  28. $this->seceret = $seceret;
  29. $this->uri = $uri;
  30. }
  31. //获取code
  32. public function redirectWithCode()
  33. {
  34. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  35. ."&redirect_uri=".urlencode($this->uri)
  36. ."&response_type=code&scope=snsapi_userinfo&state=".time()."#wechat_redirect";
  37. return $url;
  38. }
  39. private function getAcessTokenAndOpenId($code)
  40. {
  41. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  42. ."&secret=".$this->seceret."&code=".$code."&grant_type=authorization_code";
  43. $jsonInfo = file_get_contents($url);
  44. return json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  45. }
  46. private function getBaseToken()
  47. {
  48. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->seceret;
  49. $jsonInfo = file_get_contents($url);
  50. return json_decode($jsonInfo, true);
  51. }
  52. public function getAllInfo($code)
  53. {
  54. $reader = array();
  55. $info = self::getAcessTokenAndOpenId($code);
  56. if (empty($info['access_token']) || empty($info['openid'])) {
  57. return $reader;
  58. }
  59. $baseTokenInfo = self::getBaseToken();
  60. if (empty($baseTokenInfo['access_token'])) {
  61. return $baseTokenInfo;
  62. }
  63. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$baseTokenInfo['access_token']."&openid=".$info['openid']."&lang=zh_CN";
  64. $jsonInfo = file_get_contents($url);
  65. //$jsonInfo = iconv('ISO-8859-1', 'UTF-8', $jsonInfo);
  66. $rs = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  67. if(empty($rs['openid'])) {
  68. return $rs;
  69. }
  70. $attr['country'] = $rs['country'];
  71. $attr['province'] = $rs['province'];
  72. $attr['city'] = $rs['city'];
  73. $attr['headimgurl'] = $rs['headimgurl'];
  74. $attr['openid'] = $rs['openid'];
  75. $attr['nickname'] = $rs['nickname'];
  76. $attr['sex'] = $rs['sex'];
  77. $attr['subscribe'] = $rs['subscribe'];
  78. $attr['subscribe_time'] = $rs['subscribe_time'];
  79. $reader = Reader::getInfoByOpenId($info['openid']);
  80. Reader::addOrUpdate($reader['id'], $attr);
  81. return Reader::getInfoByOpenId($info['openid']);
  82. }
  83. }