thinkphp3.2 开发微信接口1

1.thinkphp3.2 有封装Wechat


把微信sdk下载下来,复制Com文件夹放到 thinkphp3.2 文件夹

1  回复文本信息和获取用户信息

<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class IndexController extends Controller {
    public function index(){
    	$token="yida";
       	$wechat=new Wechat($token);

       	$data=$wechat->request();

       	if($data && is_array($data)){

       		switch($data['MsgType']){
       			case "text" :
       			$this->Text($wechat,$data);
       		}

       	}
    }

    //回复文本消息
    private function Text($wechat,$data){

    	if(strstr($data['Content'],"文本")){
    		$text="我正在使用Thinkphp开发微信";

    		$this->logger("发送消息:\n".$text);

    		$wechat->replyText($text);

    	}else if(strstr($data['Content'],"myself")){


        $this->users($wechat,$data);
      }


    }

    //获取用户信息
    private function users($wechat,$data){
      $openid=$data['FromUserName'];
      $appid="wx9e58";
      $appSecret="aa2794a1a3c555";
      $token=session('token') ;

      if($token){

      $WechatAuth=new WechatAuth($appid,$appSecret,$token);

      }else{
        $WechatAuth=new WechatAuth($appid,$appSecret);

        $accsseToken=$WechatAuth->getAccessToken();
        $token=$accsseToken['access_token'];
        session('token',$token);
      }
      $user=$WechatAuth->userInfo($openid);
      $text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n
      你的性别是:".$user['sex']."\n你的城市是:".$user['city']."\n你所在国家是".$user['country']."\n
      你在的省份是:".$user['province'];

      $this->logger("发送用户的信息".$text);
      $wechat->replyText($text);

      

    }


    //网页授权获取用户基本信息
    public function webUsers(){
     
      $appid="wx9e583a9d";
      $appSecret="aa2794a1a3c55";
      $WechatAuth=new WechatAuth($appid,$appSecret);
      if($_GET['iscode']){
         $url="http://lizhongyi.xd107.com/Home/Index/webUsers";
         $result=$WechatAuth->getRequestCodeURL($url);
         $result;
         header("Location:{$result}");

      }else if($_GET['code']){
          header('Content-type:text/html;charset=utf-8');
          $user=$WechatAuth->getAccessToken('code',$_GET['code']);
          $openid=$user['openid'];
          $users=$WechatAuth->getUserInfo($openid);
          $m=M('users');
          $data['openid']=$users['openid'];
          $data['nickname']=$users['nickname'];
          $result=$m->add($data);
          if($result){
            $text="你的openid是:".$users['openid']."\n你的昵称是:".$users['nickname']."\n
      你的性别是:".$users['sex']."\n你的城市是:".$users['city']."\n你所在国家是".$users['country']."\n
      你在的省份是:".$users['province'];
            echo $text;
          }
         
      }
     

      
      // $user=$WechatAuth->userInfo($openid);
      // $text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n
      // 你的性别是:".$user['sex']."\n你的城市是:".$user['city']."\n你所在国家是".$user['country']."\n
      // 你在的省份是:".$user['province'];

      // $this->logger("发送用户的信息".$text);
      // $wechat->replyText($text);

      

    }


    private function logger($content){
    	$logSize=100000;

    	$log="log.txt";

    	if(file_exists($log) && filesize($log)  > $logSize){
    		unlink($log);
    	}

    	file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);

    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值