123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- declare (strict_types=1);
- namespace library\lib;
- class weixina {
-
- private $appid = '';
-
- private $secret = "";
-
- private $redirect_uri = '';
-
- private $response_type = 'code';
-
- private $scope ='snsapi_userinfo';
- private $state = 'STATE';
-
- public $error="";
-
- public function __construct($info = null) {
- if(empty($info)) $info = config('weixin');
- $this->appid = $info['APPID'];
- $this->secret = $info['APPSECRET'];
- $this->redirect_uri = $info['REDIRECT'] ;
- }
-
-
- public function auth_code2Session($jsCode){
- $oauth_url = "https://api.weixin.qq.com/sns/jscode2session?"
- ."appid=".$this->appid
- ."&secret=".$this->secret
- ."&js_code=".$jsCode
- ."&grant_type=authorization_code";
- $data = $this->Get($oauth_url);
- if(empty($data)){
- return false;
- }
- return json_decode($data,true);
- }
-
- public function getPhoneNumber($jsCode){
- $accessData = $this->getAccessToken();
- if(empty($accessData) || empty($accessData["access_token"])){
- return false;
- }
- $access_token = $accessData["access_token"];
- $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?"
- ."access_token=".$access_token;
- $postData = json_encode(array(
- "code"=>$jsCode
- ));
- $data = $this->Post($postData,$url);
- $res = $data;
- if(empty($data)){
- return false;
- }
- $data = json_decode($data,true);
- if(empty($data) || empty($data["phone_info"]) || empty($data["phone_info"]["purePhoneNumber"])){
- $this->error="返回参数名称错误".$res;
- return false;
- }
- return $data["phone_info"];
- }
-
-
-
- public function getQRCode($path,$width){
-
- }
-
-
- public function getUnlimitedQRCode($page,$scene,$width=430){
- $accessData = $this->getAccessToken();
- if(empty($accessData) || empty($accessData["access_token"])){
- return false;
- }
- $access_token = $accessData["access_token"];
- $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?"
- ."access_token=".$access_token;
- $postData = json_encode(array(
- "page"=>$page,
- "scene"=>$scene,
- "width"=>$width,
- "is_hyaline" => false,
- ));
- $data = $this->Post($postData,$url,1);
- $res = $data;
- if(empty($data)){
- return false;
- }
- if(empty($data) || empty($data["contentType"]) || empty($data["buffer"])){
- $this->error="返回参数错误".$res;
- return false;
- }
- return $data;
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
- public function createQRCode($path,$width){
-
- }
-
-
- public function getAccessToken(){
- $url = "https://api.weixin.qq.com/cgi-bin/token?"
- ."appid=".$this->appid
- ."&secret=".$this->secret
- ."&grant_type=client_credential";
- $data = $this->Get($url);
- if(empty($data)){
- return false;
- }
- return json_decode($data,true);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function oauth($state = '', $scope = ''){
- if($state == '') $state = $this->state;
- if($scope == '') $scope = $this->scope;
- $oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
- ."&redirect_uri=".$this->redirect_uri
- ."&response_type=".$this->response_type
- ."&scope=".$scope.
- "&state=".$state."#wechat_redirect";
- return redirect($oauth_url);
- }
-
- public function oauth_reuslt($code){
- $oauth_reuslt_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
- ."&secret=".$this->secret
- ."&code=".$code
- ."&grant_type=authorization_code";
- $data = $this->Post("",$oauth_reuslt_url);
- return json_decode($data,true);
- }
-
- public function userinfo($access_token){
- $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$this->appid."&lang=zh_CN";
- $data = $this->Post("",$userinfo_url);
- return json_decode($data,true);
- }
-
- public function auth($access_token,$openid){
- $r = false;
- $auth_url = "https://api.weixin.qq.com/sns/auth?access_token=&openid=".$openid;
- $data = $this->Post("",$auth_url);
- $d = json_decode($data,true);
- if($d['errcode'] == 0){
- $r = true;
- }
- return $r;
- }
-
- function Post($curlPost, $url, $isres=0) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
- $return_str = curl_exec($curl);
- $err = "";
- if(empty($return_str)){
- $err = "系统错误:".curl_error($curl);
- }
-
- $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- $content_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
- curl_close($curl);
- if(!empty($err)){
- $this->error = $err;
- return false;
- }
- if($isres==1){
- if($httpcode!=200){
- return false;
- }
- return [
- "contentType"=>$content_type,
- "buffer"=>$return_str
- ];
- }
- return $return_str;
- }
-
-
- function Get($url) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $return_str = curl_exec($curl);
- $err = "";
- if(empty($return_str)){
- $err = "系统错误:".curl_error($curl);
- }
- curl_close($curl);
- if(!empty($err)){
- $this->error = $err;
- return false;
- }
- return $return_str;
- }
-
- function log_result($word, $name) {
- $fp = fopen($name . "_log.txt", "a");
- flock($fp, LOCK_EX);
- fwrite($fp, $word . "\n");
- flock($fp, LOCK_UN);
- fclose($fp);
- }
- }
|