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

转载于:https://www.cnblogs.com/myJuly/p/10077800.html

微信开发经常会用到的一些方法相关推荐

  1. php+微信开发+解绑,微信开发之解绑设备通知的方法

    本篇文章一起来了解微信开发之解绑设备通知的方法 一开始进行解绑的时候,我一直用的强制解绑设备通知.微信在开发者文档中和接口权限中都没有说明这个接口有次数限制,直到最近两天,解绑一直失败才发现,原来这个 ...

  2. 微信开发“TOKEN验证失败”根源和解决方法

    引子 微信公众号开启开发者模式时,需要配置"服务器配置",但在配置这个东西时有很多坑需要注意. 网上解决的问题这里就不再赘述,下面说的是我碰到的问题,同时也叙述了"TOK ...

  3. java微信附件下载_WxJava微信开发工具包

    下面我们对WxJava微信开发工具包文件阐述相关使用资料和WxJava微信开发工具包文件的更新信息. WxJava微信开发工具包 WxJava微信开发Java开发工具包(SDK),支持包括微信支付.微 ...

  4. Java企业微信开发_00_源码及资源汇总贴

    一.源码 此系列教程的源码我都放在了github上,欢迎fork以及关注. 传送门:https://github.com/shirayner/WeiXin_QiYe_Demo/tree/master ...

  5. 微信开发:微信js_sdk 分享,前端部分(二)

    微信开发:微信js-sdk前端分享,代码如下: <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> ...

  6. 微信开发-ACCESS TOKEN 过期失效解决方案

    微信开发-ACCESS TOKEN 过期失效解决方案 参考文章: (1)微信开发-ACCESS TOKEN 过期失效解决方案 (2)https://www.cnblogs.com/wicub/p/58 ...

  7. 微信小程序在开发中遇到的问题与解决方法

    微信小程序在开发中遇到的问题与解决方法 参考文章: (1)微信小程序在开发中遇到的问题与解决方法 (2)https://www.cnblogs.com/zjjDaily/p/8032142.html ...

  8. 用thinkphp进行微信开发的整体设计思考

    用thinkphp进行微信开发的整体设计思考 http://www.2cto.com/weixin/201504/388423.html 2015-04-09      0个评论       作者:明 ...

  9. 微信开发之自动回复图文消息

    最近开始做公司的微信项目,深切的感受到了微信的不同,做微信是需要从头开始好好学的,在此记录一下学习微信的过程,也希望以后能根据这些文章迅速掌握微信开发的知识.少踩坑,,(laravel框架) 1.首先 ...

最新文章

  1. Windows遭遇史上最大攻击:微软却在疯狂圈粉
  2. swoole-co-pool v1.0.1,Swoole 协程工作池
  3. .net的retrofit--WebApiClient底层篇
  4. java换水_java-交流灌水之谁是水王?
  5. mysql读写分离 存储过程_基于maxscale的读写分离部署笔记
  6. Java Stream Collectors.groupingBy()实现分组(单字段分组,多字段分组)
  7. 求出给定节点在二叉排序树中的层次
  8. PCL—低层次视觉—点云滤波(基于点云频率)
  9. [译] 在远程工作中领悟到的 10 件事
  10. CentOS7安装KVM、KVM安装CentOS7
  11. 判断一个字符串是否在一个数组中
  12. C# 调用office 2007 及 SaveAsPDFandXPS.exe 将Word、Excel、PPT转换为PDF文件
  13. 笨办法学Python习题11 提问
  14. 尚硅谷周阳老师 - Docker课程学习
  15. 42u的机柜供服务器安装位置,一个42U标准服务器机柜能放多少台服务器
  16. 字节跳动2019春招算法题
  17. mysql不能存字母_jdbc - 无法在mysql中存储俄语中文阿拉伯语字母
  18. linux zip 加密
  19. 计算机程序设计理论知识,计算机程序设计员理论知识试卷
  20. 看门狗喂狗实验(有问题)

热门文章

  1. lync登录时一直停留在登录界面
  2. IE浏览器通过代码控制文档对象模式
  3. 社保基金入市规模或达3000亿元
  4. 创建指南针View的例子
  5. 浅析托管与非托管C++代码(转)
  6. 如何使用VS2019编译QT项目
  7. Ubuntu Server 16.04.x进入中文安装界面无法安装busybox-initramfs
  8. 对付洗稿者的一个脑洞
  9. PowerDesigner基础使用教程
  10. 客观评价golang的优缺点