Home > PHP Framework > YII > body text

How to customize public classes in yii2

王林
Release: 2019-12-24 14:39:56
Original
2490 people have browsed it

How to customize public classes in yii2

Take the WeChat enterprise account as an example:

1. Create a new folder wechat in the YII root directory and create a new Wechat.php file

The code is as follows :

<? 
namespace app\wechat;
class Wechat
{
	const AGENTID = 3;//应用ID
	const CORPID = "wx5d0183ad90c95d8b";//ID
	const CORPSECRET = "KTHAkkVl4mX4Jr_g89d3PXajYupsUcJFvGWQ1K6ZMagTPOh4kiNMfBLFoDr12DVh";//秘钥
	const SCOPE = "snsapi_base"; 
	const STATE = "123"; 	
 
	//自动登录跳转
	public function wxauto($jumpurl){	
		$corpid = self::CORPID; 
		$scope = self::SCOPE; 
		$state = self::STATE;  
		$url=&#39;https://open.weixin.qq.com/connect/oauth2/authorize?appid=&#39;.$corpid.&#39;
		&redirect_uri=&#39;.urlencode($jumpurl).&#39;
		&response_type=code&scope=&#39;.$scope.&#39;&state=&#39;.$state.&#39;#wechat_redirect&#39;;
		header("Location:".$url);
		exit;
	}
	
	//推送信息
	public function getPush($userid , $agentid , $message){
			$userinfo = $this->getToken();//获取access_token
			$access_token = $userinfo[&#39;access_token&#39;];
			$sendmsg_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".
			$access_token;
			$data="{\"touser\":\"$userid\",\"msgtype\":\"text\",\"agentid\":$agentid,\"text\":
			{\"content\":\"$message\"},\"safe\":0}";
			$res = $this->curlPost($sendmsg_url,$data);
			$errmsg=json_decode($res)->errmsg;
	}
	
	//获取token
	public function getToken(){		
		$corpid = self::CORPID; 
		$corpsecret = self::CORPSECRET; 
		$Url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret;
		$res = $this->curlPost($Url);
		$access_token=json_decode($res)->access_token;
		$userinfo = array();
		$userinfo[&#39;access_token&#39;]=$access_token;
		return $userinfo;
	}
	
	//定义curl方法
	public function curlPost($url,$data=""){
		$ch = curl_init();
		$opt = array(
				CURLOPT_URL     => $url,
				CURLOPT_HEADER  => 0,
				CURLOPT_POST    => 1,
				CURLOPT_POSTFIELDS      => $data,
				CURLOPT_RETURNTRANSFER  => 1,
				CURLOPT_TIMEOUT         => 20
		);
		$ssl = substr($url,0,8) == "https://" ? TRUE : FALSE;
		if ($ssl){
			$opt[CURLOPT_SSL_VERIFYHOST] = 2; //注意1已经弃用
			$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
		}
		curl_setopt_array($ch,$opt);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
###########**********增加获取微信openid***********#########################
}
?>
Copy after login

2. Controller call:

Introduction use app\wechat\Wechat;

	$chat = new Wechat();  //实例化类
	$REDIRECT_URI= $_SERVER[&#39;HTTP_HOST&#39;].&#39;/test/back&#39;;//定义跳转URL
	$chat->wxauto($REDIRECT_URI);//调用类的方法
        $chat->getToken();//调用类的方法
 
	$corpid = Wechat::CORPID;//调用类的常量
	$corpsecret = Wechat::CORPSECRET;//调用类的常量
	$agentid=Wechat::AGENTID;//调用类的常量
Copy after login

Recommended related articles and tutorials: yii tutorial

The above is the detailed content of How to customize public classes in yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template