weixin.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class weixin
  10. {
  11. //微信APPID
  12. var $app_id="";
  13. //微信秘钥
  14. var $app_secret="";
  15. //跳转链接
  16. var $redirect_url="";
  17. //传递的方式
  18. var $scope="";
  19. var $state=1;
  20. //用户同意授权,获取code
  21. var $code="";
  22. //授权通过后获取access_token openid
  23. var $access_token="";
  24. var $openid="";
  25. var $platform = "";
  26. var $component_appid = "";
  27. var $component_access_token = "";
  28. var $is_platform = 0;
  29. function __construct($app_id="",$app_secret="",$redirect_url="",$scope="snsapi_userinfo",$state=1)
  30. {
  31. $this->app_id=$app_id;
  32. $this->app_secret=$app_secret;
  33. $this->redirect_url=urlencode($redirect_url);
  34. $this->scope=$scope;
  35. $this->state=$state;
  36. $weixin_conf = load_auto_cache("weixin_conf");
  37. if($weixin_conf['platform_status']){
  38. $this->is_platform = 1;
  39. $this->component_appid = $weixin_conf['platform_appid'];
  40. $this->component_access_token = $weixin_conf['platform_component_access_token'];
  41. $this->option = array(
  42. 'platform_token'=>$weixin_conf['platform_token'], //填写你设定的token
  43. 'platform_encodingAesKey'=>$weixin_conf['platform_encodingAesKey'], //填写加密用的EncodingAESKey
  44. 'platform_appid'=>$weixin_conf['platform_appid'], //填写高级调用功能的app id
  45. 'platform_appsecret'=>$weixin_conf['platform_appsecret'], //填写高级调用功能的密钥
  46. 'platform_component_verify_ticket'=>$weixin_conf['platform_component_verify_ticket'], //第三方通知
  47. 'platform_component_access_token'=>$weixin_conf['platform_component_access_token'], //第三方平台令牌
  48. 'platform_pre_auth_code'=>$weixin_conf['platform_pre_auth_code'], //第三方平台预授权码
  49. 'platform_component_access_token_expire'=>$weixin_conf['platform_component_access_token_expire'],
  50. 'platform_pre_auth_code_expire'=>$weixin_conf['platform_pre_auth_code_expire'],
  51. 'logcallback'=>'log_result',
  52. 'debug'=>true,
  53. );
  54. $this->platform = new PlatformWechat($this->option);
  55. $new_token = $this->platform->check_platform_access_token();
  56. if($new_token!=$this->component_access_token){
  57. $this->component_access_token = $new_token;
  58. }
  59. }
  60. }
  61. public function scope_get_code($app_id){
  62. if($this->is_platform){
  63. //https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE&component_appid=component_appid#wechat_redirect
  64. $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$app_id."&redirect_uri=".($this->redirect_url)."&response_type=code&scope=".$this->scope."&state=".$this->state."&component_appid=".$this->component_appid."#wechat_redirect";
  65. }else{
  66. $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->app_id."&redirect_uri=".($this->redirect_url)."&response_type=code&scope=".$this->scope."&state=".$this->state."#wechat_redirect";
  67. }
  68. return $url;
  69. }
  70. public function scope_get_userinfo($code,$appid=""){
  71. if(!class_exists("transport"))
  72. require_once APP_ROOT_PATH."system/utils/transport.php";
  73. $tran = new transport();
  74. $this->code=$code;
  75. if($this->is_platform){
  76. //https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=APPID&code=CODE&grant_type=authorization_code&component_appid=COMPONENT_APPID&component_access_token=COMPONENT_ACCESS_TOKEN
  77. $get_token_url="https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=".$appid."&code=".$this->code."&grant_type=authorization_code&component_appid=".$this->component_appid."&component_access_token=".$this->component_access_token;
  78. }else{
  79. $get_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->app_id."&secret=".$this->app_secret."&code=".$this->code."&grant_type=authorization_code";
  80. }
  81. $token_info=$this->https_request($get_token_url);
  82. $token_info=json_decode($token_info['body'],true);
  83. if(intval($token_info['errcode'])!=0){
  84. return $token_info;
  85. }
  86. $this->access_token=$token_info['access_token'];
  87. $this->openid=$token_info['openid'];
  88. $get_userinfo="https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$this->openid."&lang=zh_CN";
  89. $user_info=$this->https_request($get_userinfo);
  90. $user_info=json_decode($user_info['body'],true);
  91. return $user_info;
  92. }
  93. public function sns_get_userinfo($openid,$access_token){
  94. $this->access_token=$access_token;
  95. $this->openid=$openid;
  96. $get_userinfo="https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$this->openid."&lang=zh_CN";
  97. $user_info=$this->https_request($get_userinfo);
  98. $user_info=json_decode($user_info['body'],true);
  99. return $user_info;
  100. }
  101. public function https_request($url){
  102. $curl = curl_init();
  103. curl_setopt($curl, CURLOPT_URL, $url);
  104. curl_setopt($curl, CURLOPT_HEADER, 1);
  105. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  106. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  107. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
  108. $http_response = curl_exec($curl);
  109. if (curl_errno($curl) != 0)
  110. {
  111. return false;
  112. }
  113. $separator = '/\r\n\r\n|\n\n|\r\r/';
  114. list($http_header, $http_body) = preg_split($separator, $http_response, 2);
  115. $http_response = array('header' => $http_header,//肯定有值
  116. 'body' => $http_body); //可能为空
  117. curl_close($curl);
  118. return $http_response;
  119. }
  120. }
  121. ?>