微信公众号SDK开发(六)

 

获取并缓存access_token


|wechatSDK
|    |---|app
|    |---|--|Entry.php
|    |---|wechat
|    |---|--Wx.php
|    |---|bootstrap.php
|    |---|build
|    |---|---Message.php
|    |---|cache

Wx.php
 
<?php
namespace wechat;
 
class Wx{
    //微信配置项
    static $config=[];
    protected $config=[];
    protected $message;
	protected $accessToken;
    public function __construct(array $config=[]){
        //if(is_array($config)){
        //    self::$config=$config;
        //}
		if(!empty($config)){
            self::$config=$config;
        }
        $this->message=$this->parsePostRequestData();
    }
	//获取access_token
	public function getAccessToken(){
		$cacheName=md5(self::$config['appID'].self::$config['appsecret']);
		$file=__DIR__.'/cache/'.$cacheName.'.php';
		
		if(is_file($file) && filemtime($file)+7000>time()){
			//缓存没有过期
			$data=include $file ;
			///return $this->accessToken=$data['access_token'];
		}else{
			$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$config['appid']."&secret=".self::$config['appsecret'].";
			$access_token=$this->curl($url);
			$data=json_decode($access_token,true);
			
			//获取令牌失败
			if(isset($data['errorcode'])){
				return false;
			}
			//获取令牌成功
			//print_r($data);
			file_put_contents($file,'<?php return '.var_export($data,true).' ; ?>');
		}
		return $this->accessToken=$data['access_token'];
	}
    public function show(){
        echo 'wx';
    }
    //与微信服务器进行绑定
    public function valid(){
        $signature=$_GET["signature"];
        $timestamp=$_GET["timestamp"];
        $nonce=$_GET['nonce'];
        
        //$token='developer';
        $token=self::$config['token'];
        $tmpArr=array($token,$timestamp,$nonce);
        sort($tmpArr,SORT_STRING);
        $tmpStr=implode($tmpArr);
        $tmpStr=sha1($tmpStr);
        
        if($tmpStr==$signature){
            echo $_GET['echoStr'];
        }
    }
    //获取并解析用户发来的消息内容
    private function parsePostRequestData(){
        if(isset($_GLOBALS['HTTP_RAW_POST_DATA'])){
            //simplexml_load_string - 将XML字符串解释为对象
            return simplexml_load_string($_GLOBALS['HTTP_RAW_POST_DATA'],'SimpleXMLElement',LIBXML_NOCDATA);  // 将 CDATA 设置为文本节点
        }
    }
	
	public function curl($url,$fields=[]){
		$ch=curl_init();
		
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		
		if($fields){
			curl_setopt($ch,CURLOPT_TIMEOUT,30);
			curl_setopt($ch,CURLOPT_POST,1);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
		}
		
		$data='';
		if(curl_exec($ch)){
			$data=curl_multi_getcontent($ch);
		}
		curl_close($ch);
		return $data;
	}
    //获取粉丝发来的消息内容
    public function getMessage(){
        return $this->message;
    }
    //获取功能实例类,如:消息管理
    //使用wx->instance('message');
    public function instance($name){
        $class='\wechat\build\\'.ucfirst($name);
        return new $class;
    }
}
 
 
 
 
Entry.php
 
<?php
//业务代码,为了测试微信sdk的功能
//微信操作的基础类 
namespace app;
use wechat\Wx;
 
class Entry{
    protected $wx;
    public function __construct(){
        $config=[
            'token'=>'developer',
			'appID'=>'wxcf444c17dbd095b9',
			'appsecret'=>'6fd451c0cff05b6c39c51dd7f7bff562'
        ];
        $this->wx=new Wx($config);
        $this->wx->valid();
    }
    public function handler(){
		//$d=$this->wx->curl('http://voter.pw',['a'=>'232342']);	
		$this->wx->getAccessToken();
		echo $d;
	}
}
 
 
 
SimpleXMLElement::__set_state(array(
    'ToUserName'=>'gh_17962820bb47',
    'FromUserName'=>'odnoLwrZfu6WwdGhzoHYMhz-L8rc',
    'CreateTime'=>'1478718634',
    'MsgType'=>'text',
    'Content'=>'gghhh',
    'MsgId'=>'6351048173485048275'
))
 
 
<?php
namespace wechat\build;
 
//专门处理微信消息
use wechat\Wx;
 
class Message extends Wx{
    public function show(){
        echo __METHOD__;
    }
	public function text($content){
		//echo 3333;exit;
		$xml='
		<xml> 
            <ToUserName>< ![CDATA[{$message->FromUserName}] ]></ToUserName> 
            <FromUserName>< ![CDATA[{$message->ToUserName}] ]></FromUserName> 
            <CreateTime>{$time}</CreateTime> 
            <MsgType>< ![CDATA[text] ]></MsgType> 
            <Content>< ![CDATA[这是我回复的消息] ]></Content> 
        </xml>
		';
		$text=sprintf($xml,$this->message->FromUserName,$this->message->ToUserName,time(),$content);
		header('Content-Type:application/xml;Charset:utf-8');
		echo $text;
	}
}
 
?>	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值