微信 php 获取ticket


<?php header('content-type:text/html; charset=utf8'); define('TOKEN', 'youtoken'); // TOKEN
define('APP_ID', 'youid'); // 开发者AppID,服务号可获取
define('APP_SECRET', 'youSECRET'); // 开发者AppSecret,服务号可获取
set_time_limit(30); //为防止access_token获取失败导致死循环
$wechatObj = new test(); //echo '二维码地址:'.$wechatObj->QrcodeCreate(111); //echo '显示结果'.$wechatObj->AccessTokenGet(); //连接数据库
$con = mysql_connect("localhost","root","mimabugaoshuini"); mysql_query("SET NAMES utf8"); mysql_select_db("servicedb", $con); $a=$wechatObj->QrcodeCreate(111); $arr = explode("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=",$a); $query = "update sipuser set ticketid='".$arr[1]."' where id=100001"; $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); mysql_close($con); //echo '二维码地址:'.$query; 
class test { private $token; private $appID; private $appSecret; private $accessToken; public function __CONSTRUCT(){ $this->token = TOKEN; $this->appID = APP_ID; $this->appSecret = APP_SECRET; $this->accessToken = './access_token'; if(!file_exists($this->accessToken)){ $this->AccessTokenGet(); } } /* 创建二维码 @param - $qrcodeID传递的参数,$qrcodeType二维码类型 默认为临时二维码 @return - 返回二维码图片地址 */
public function QrcodeCreate($qrcodeID, $qrcodeType = 0) { if ($qrcodeType == 0) { $qrcodeType = 'QR_SCENE'; } else { $qrcodeType = 'QR_LIMIT_SCENE'; } $tempJson = '{"expire_seconds": 1800, "action_name": "' . $qrcodeType . '", "action_info": {"scene": {"scene_id": ' . $qrcodeID . '}}}'; $access_token = file_get_contents ( $this->accessToken ); $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token; $tempArr = json_decode ( $this->JsonPost ( $url, $tempJson ), true ); if (@array_key_exists ( 'ticket', $tempArr )) { return 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $tempArr ['ticket']; } else { $this->ErrorLogger ( 'qrcode create falied.' ); $this->AccessTokenGet (); $this->QrcodeCreate (); } } private function AccessTokenGet(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appID.'&secret='.$this->appSecret; //$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfcbb68eb1c87a00d&secret=a450e03bb8347ea77e4876590256badd';
$tempArr = json_decode(file_get_contents($url), true); if(@array_key_exists('access_token', $tempArr)){ $tempWriter = fopen($this->accessToken, 'w'); fwrite($tempWriter, $tempArr['access_token']); }else{ $this->ErrorLogger('access_token get falied.'); exit(); } } // /* 用户分组查询 */
public function GroupsQuery(){ $access_token = file_get_contents($this->accessToken); $url = 'https://api.weixin.qq.com/cgi-bin/groups/get?access_token='.$access_token; $tempArr = json_decode(file_get_contents($url), true); if(@array_key_exists('groups', $tempArr)){ return $tempArr['groups']; //返回数组格式的分组信息
}else{ $this->ErrorLogger('groups query falied.'); $this->AccessTokenGet(); $this->GroupsQuery(); } } // 工具函数 // /* 使用curl来post一个json数据 */
// CURLOPT_SSL_VERIFYPEER,CURLOPT_SSL_VERIFYHOST - 在做https中要用到 // CURLOPT_RETURNTRANSFER - 不以文件流返回,带1
private function JsonPost($url, $jsonData){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); if (curl_errno($curl)) { $this->ErrorLogger('curl falied. Error Info: '.curl_error($curl)); } curl_close($curl); return $result; } /* 错误日志记录 */
private function ErrorLogger($errMsg){ $logger = fopen('./ErrorLog.txt', 'a+'); fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."\r\n"); } } ?>


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM