• 微信JS API PHP类


    CURL操作类:

    <?php
    namespace appcommon;
    
    class curl{
        public static function wxcurl($getUrl){
    
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $getUrl);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            $data = curl_exec($ch);
            $response = json_decode($data,true);
            return $response;
        }
    }
    

    PHP类
    直接写入的是缓存,如果你本地不支持redis缓存 需要修改存储方式

    <?php
    namespace appcommon;
    use appcommonCurl;
    
    /**
     * @property  secret
     */
    class Wechat{
    	private $appid;
    	private $secret;
            private $url;
    	private $urlaccesstoken="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    	private $urljsapi_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=";
    	function __construct($appid,$secret,$url){
    		$this->appid=$appid;
    		$this->secret =$secret;
                    $this->url = $url;
    	}
    	private function getToken(){
    		$access_token = json_decode(Yii::$app->redis->get('wechat_access_token'),true);
    		$effective = false;
    		if(!$access_token){
    			$effective=true;
    		}else{
    			//验证时间
    			if(time() -$access_token['times'] > 7200){
    				$effective=true;
    			}
    		}
    		if($effective){
    			$oauthUrl = $this->urlaccesstoken."&appid=".$this->appid."&secret=".$this->secret;
    			$codeinfo = curl::wxcurl($oauthUrl);
    			$access_token = ["times"=>time(),"access_token"=>$codeinfo['access_token']];
    			Yii::$app->redis->set('wechat_access_token',json_encode($access_token,true));
    		}
    		return $access_token['access_token'];
    	}
    	private function getTicket(){
    		$jsapi_ticket =json_decode(Yii::$app->redis->get('wechat_jsapi_ticket'),true);
    		$effective = false;
    		if(!$jsapi_ticket){
    			$effective=true;
    		}else{
    			//验证时间
    			if(time() -$jsapi_ticket['times'] > 7200){
    				$effective=true;
    			}
    		}
    		if($effective){
    			$access_token = $this->getToken();
    			$oauthUrl = $this->urljsapi_ticket.$access_token;
    			$codeinfo = curl::wxcurl($oauthUrl);
    			$jsapi_ticket = ["times"=>time(),"ticket"=>$codeinfo['ticket']];
    			
    			Yii::$app->redis->set('wechat_access_ticket',json_encode($jsapi_ticket,true));
    		}
    		return $jsapi_ticket['ticket'];
    	}
    	private function nonceStr($length=32){
    		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    		$str ="";
    		for ( $i = 0; $i < $length; $i++ ) {
    			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
    		}
    		return $str;
    	}
    	private function signature($Parameters){
    		//拼接参数
    		$arg  = "";
    		foreach($Parameters as $key => $val)
    		{
    
    			$arg.=$key."=".$val."&";
    		}
    		//去掉最后一个&字符
    		$arg = trim($arg,'&');
    		if(get_magic_quotes_gpc())
    		{
    			$arg = stripslashes($arg);
    		}
    		return sha1($arg);
    	}
    	private function getAppid(){
    		return $this->appid;
    	}
    	function getWechatJsConfig(){
    		$time = time();
    		$Parameters = [
    			"jsapi_ticket"=>$this->getTicket(),
    			"noncestr"=>$this->nonceStr(),
    			"timestamp"=>"$time",
    			"url"=>$this->url,
    		];
    		$return['appId'] = $this->getAppid();
    		$return['timestamp'] = $Parameters['timestamp'];
    		$return['nonceStr'] = $Parameters['noncestr'];
    		$return['signature'] = $this->signature($Parameters);
    		
    		
    		return $return;
    	}
    }
    

    返回格式:

    appId:"xxxxxxxxx",
    nonceStr:"xxxxxxxxxxx",
    signature:"xxxxxxxxxx",
    timestamp:"1522083380",
    

    JS页面

    wx.config({
                debug: true,
                appId:res.appId,
                timestamp:res.timestamp,
                nonceStr:res.nonceStr,
                signature:res.signature,
                jsApiList: [
                  'chooseImage',//拍照或从手机相册中选图接口
                  'onMenuShareTimeline',
                  'previewImage',//预览图片接口
                  'uploadImage',//上传图片接口
                  'downloadImage'//下载图片接口
                ]});
    
  • 相关阅读:
    项目架构开发:数据访问层之Cache
    微信公众号平台接口开发:菜单管理
    【软件工程】第0次个人作业
    OO第四次博客作业
    OO第三次博客作业
    OO第二次博客作业
    Java学习笔记
    SQLInjection 靶场配置
    OO第一次博客作业
    面向对象先修:Java入门
  • 原文地址:https://www.cnblogs.com/subtract/p/8655093.html
Copyright © 2020-2023  润新知