• 微信开发经常会用到的一些方法


     public $config = array(
                "AppID"         =>  "wxde30b2bd1ce49e47",               //应用ID
                "AppSecret"     =>  "07d26f5fdb30a6cf4a14ee18b7017eb7",//应用密钥
            );
        /**
         *  获取登陆url
         *  $redirect_uri           回调地址
         *  $scope                  是否授权(snsapi_userinfo:授权,snsapi_base:不授权)
        */
        public function LoginUri($redirect_uri = "",$scope = "snsapi_base",$state = "123"){
            if(!$redirect_uri){
                die("请填写回掉地址!");
            }
            return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->config['AppID']}&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
        }
        /**
         *  获取用户信息
         */
        function UsInfo(){
            $str = $this->get_curl("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->config['AppID']}&secret={$this->config['AppSecret']}&code={$_GET['code']}&grant_type=authorization_code");
            $arr = json_decode($str,true);
            $message = $this->get_curl("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->config['AppID']}&grant_type=refresh_token&refresh_token={$arr['refresh_token']}");
            $arr1 = json_decode($message,true);
            $us_info = $this->get_curl("https://api.weixin.qq.com/sns/userinfo?access_token={$arr1['access_token']}&openid={$arr1['openid']}&lang=zh_CN");
            return json_decode($us_info,true);
        }
        /**
         *    获取access_token
         */
        function get_access_token(){
            $path = $_SERVER['DOCUMENT_ROOT']."/volunteer_hatosy/access_token.txt";
            $arr = json_decode(file_get_contents($path),true);
            if($arr){
                $expires_in = $arr['expires_in'];
            }else{
                $expires_in = 0;
            }
            if(!$arr || time() >= $expires_in){
                $str = $this->get_curl("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->config['AppID']}&secret={$this->config['AppSecret']}");
                $info = json_decode($str,true);
                $info['expires_in'] = time() + 7000;
                $access_token = $info['access_token'];
                $this->fwrite_txt($path,json_encode($info));
            }else{
                $access_token = $arr['access_token'];
            }
            return $access_token;
        }
        /**
         *  获取随机数
         */
        function createNonceStr($length = 16) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                $str = "";
                for ($i = 0; $i < $length; $i++) {
                $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            }
            return $str;
         }
        /**
         *  获取ticket
         */
        function get_ticket(){
            $path = $_SERVER['DOCUMENT_ROOT']."/volunteer_hatosy/tocket.txt";
            $arr = json_decode(file_get_contents($path),true);
            if(!$arr || time() >= $arr['expires_in']){
                $access_token = $this->get_access_token();
                $ticket = $this->get_curl("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi");
                $info = json_decode($ticket,true);
                $info['expires_in'] = time() + 7000;
                $this->fwrite_txt($path,json_encode($info));
                return $info['ticket'];
            }else{
                return $arr['ticket'];
            }
        }
        /**
         *  获取signature
         */
        function signature(){
            $ticket = $this->get_ticket();
            $timestamp = time();
            $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $noncestr = $this->createNonceStr(10);
            $string = "jsapi_ticket={$ticket}&noncestr=$noncestr&timestamp=$timestamp&url=$url";
            $signature = sha1($string);
            return array(
                'timestamp' => $timestamp,
                'noncestr' => $noncestr,
                'signature' => $signature
            );
        }
        /**
         *    获取二维码
         */
        function get_ercode($us_id = 0){
            $access_token = $this->get_access_token();
            $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$access_token}";
            $param = array(
                    "action_name"=>"QR_LIMIT_SCENE",
                    "action_info"=>array("scene"=>array("scene_id"=>intval($us_id)))
                );
            $xml = $this->post_curl($url,json_encode($param));
            return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode(json_decode($xml)->ticket);
        }
        function post_curl($url,$data,$agreement = 0){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            if($agreement == 0){//0 https   1   http
                unset($_REQUEST['agreement']);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            }
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
        }
        function get_curl($url,$agreement = 0){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            if($agreement == 0){//0 https   1   http
                unset($_REQUEST['agreement']);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            }
            if(defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
                curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
            }
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
        }
        function uploadMedia(){
            $access_token = $this->get_access_token();
            $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=voice";
            $file = realpath('1482826121.amr'); //要上传的文件
            $fields['media'] = '@'.$file;
            $ch = curl_init($url) ;
            curl_setopt($ch, CURLOPT_POST, 1);
            if($agreement == 0){//0 https   1   http
                unset($_REQUEST['agreement']);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            }
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch) ;
            if (curl_errno($ch)) {
                dump(curl_errno($ch));
            }
            curl_close($ch);
            dump($result);
        }
        /**
         *    获取语音
         */
        public function getVoice(){
            $media_id = '_pnBKlv6Xp7VzI28UEpxYQaEknMI7mWZ4tROP9Bvpc7QjkC9GbDUy3Yuma4J7TgT';
            $access_token = $this->get_access_token();
            $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
            $fileInfo = $this->downloadWeixinFile($url);
            $fileName = time().".amr";
            $this->saveWeixinFile($fileName,$fileInfo['body']);
        }
        function downloadWeixinFile($url){
            $ch = curl_init($url);
            curl_setopt($ch,CURLOPT_HEADER,0);
            curl_setopt($ch,CURLOPT_NOBODY,0);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            $package = curl_exec($ch);
            curl_close($ch);
            $imageAll = array_merge(array('header'=>$httpinfo),array('body'=>$package));
            return $imageAll;
        }
        function saveWeixinFile($filename,$filecontent){
            $local_file = fopen($filename,'w');
            if(local_file !== false){
                if(fwrite($local_file,$filecontent) !== false){
                    fclose($local_file);
                }
            }
        }
        
        /*文件写入*/
        function fwrite_txt($path,$txt){
            $myfile = fopen($path,"w") or die("Unable to open file!");
            fwrite($myfile,$txt);
            fclose($myfile);
        }
        /*文件读取*/
        function fread_txt($path){
            $txt = file_get_contents($path);
            return $txt;
        }


    转载自:https://blog.csdn.net/qq_34629975/article/details/53906144 

  • 相关阅读:
    linux批量远程多服务器FTP并下载文件的脚本
    NPM更换国内源
    Win10禁用无用的服务
    JS测试
    FastAdmin导出
    VScode全局设置
    Vue路由history模式
    kill_devtmpfsi
    获取域名URL
    Axios去除Respones中的config、headers、request
  • 原文地址:https://www.cnblogs.com/myJuly/p/10077800.html
Copyright © 2020-2023  润新知