微信根據openid發送消息,獲取用戶詳情,獲取access_token類

純屬娛樂,有什麼不對的地方請多多指教

<?php

session_start();//開啓session緩存機制
class WxSendMessage{
private  $Appid;//應用id
private  $Appsecret;//應用密鑰
private  $WxGetUrl ;//微信Get請求url
private  $WxPostUrl;//微信Post請求url
private  $MessageID;//信息模板id;
private  $access_token;//微信唯一密鑰
private  $state;//返回狀態
private  $message;//返回信息
private  $result = array();//返回內容
public function __construct($appid,$appsecret){
$this->Appid = $appid;
$this->Appsecret = $appsecret; 
}
/**
* Get方式實現
* @return [array]  $output   [返回數據]
*/
private function curl_Get(){
try{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $this->WxGetUrl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}catch(Exception $e){
return false;
}

}
/**
* [curl_Post Post方式實現]
* @param  [array] $data [發送數據]
* @return [array]       [返回數據]
*/
private function curl_Post($data){
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->WxPostUrl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
      curl_close($ch);
return $output;
}catch(Exception $e){
return false;
}
}
/**
* [getAccontToken 獲取微信access_token唯一憑證]
* @return [array] [返回數據信息]
*/
public function getAccontToken(){
$this->state = 0;//0:失敗,1:成功;
$this->message = "獲取微信的請求數據失敗";
$result = array();//返回數據
$this->WxGetUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->Appid."&secret='".$this->Appsecret."'";
if(!$_SESSION['time'])$_SESSION['time'] = 0;
if(!$_SESSION['access_token'] || time()-$_SESSION['time']>7200){
$res = $this->curl_Get();
if($res){
$res = json_decode($res,true);
if($res['errcode']){
$this->message = $res['errmsg'];
}else{
$this->state = 1;
$this->message = "獲取微信的請求數據成功";
$_SESSION['time'] = time();
$this->result['access_token'] = $this->access_token = $_SESSION['access_token'] = $res['access_token'];
}
}
}else{
$this->state = 1;
$this->message = "獲取微信的請求數據成功";
$this->result['access_token'] = $this->access_token = $_SESSION['access_token'] ;
}
$this->getResultInfo();
  return $this->result;
}
/**
* [sendMessageByOpenid 向openid用戶發送信息]
* @param  [string] $openid    [接收者openid]
* @param  [string] $messageid [消息類型id]
* @param  [array] $data      [發送數據]
* @return [array]            [返回發送狀態]
*/
public function sendMessageByOpenid($openid,$messageid,$datas){
$this->state = 0;//狀態:0:失敗;1:成功;
$this->message = "微信發送信息失敗";
$result = array();
$this->getAccontToken();
if(!$this->access_token){
$this->message = "獲取微信access_token失敗";
$this->getResultInfo();
return $this->result;
}
if(!$openid || !$messageid || !$datas){
$this->message = "缺少微信發送信息必填參數";
$this->getResultInfo();
return $this->result;
}
$this->WxPostUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->access_token;
$data['touser'] = $openid;
$data['messageid'] = $messageid;
$data['data'] = $datas;
$res = $this->curl_Post($data);
if($res){
$res = json_encode($res,true);
if($res['errcode'] == 0){
$this->result['msgid'] = $res['msgid'];
$this->state = 1;
$this->message = '微信發送信息成功';
}
}
return $this->result;
}
/**
* 返回信息
*/
public function getReturnInfo(){
$this->result['message'] = $this->message;
$this->result['state']   = $this->state;
}
/**
* [getUserInfoByOpenid 根據openid獲取用戶信息]
* @param  [string] $openid [用戶openid]
* @return 
*/
public function getUserInfoByOpenid($openid){
$this->state = 0;//狀態:0:失敗;1:成功;
$this->getAccontToken();
if(!$this->access_token){
$this->message = '獲取微信access_token失敗';
$this->getReturnInfo();
return $this->result;
}
if(!$openid){
$this->message = '獲取微信openid失敗';
$this->getReturnInfo();
return $this->result;
}
$this->WxGetUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token='".$this->access_token."'&openid='".$openid."'&lang=zh_CN";
$res = $this->curl_Get();
if($res){
$this->state = 1;
$this->message = '獲取用戶信息成功';
$this->result['data'] = $res;
}
$this->getReturnInfo();
return $this->result;
}
}

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章