微信开发生成二维码 接收回复消息
来源:互联网 发布:加内特身体数据 编辑:程序博客网 时间:2024/06/08 15:06
<?phpclass WeChat{ private $appID; private $appSecret; public function __construct($appID, $appSecret) { $this->appID = $appID; $this->appSecret = $appSecret; } public function request($curl, $method = "GET", $data = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $curl); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($method == "POST") { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $content = curl_exec($ch); curl_close($ch); return $content; } public function getAccessToken() { $tokenFile = "accessToken"; if (file_exists($tokenFile)) { $content = file_get_contents($tokenFile); $content = json_decode($content); if (time() - filemtime($tokenFile) < $content->expires_in) { return $content->access_token; } } $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appID&secret=$this->appSecret"; $content = $this->request($url); file_put_contents($tokenFile, $content); $content = json_decode($content); return $content->access_token; } public function getTicket($sceneID, $temp = true, $expireSeconds = 604800) { if ($temp) { $data = "{\"expire_seconds\": $expireSeconds, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": $sceneID}}}"; } else { $data = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": $sceneID}}}"; } $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->getAccessToken(); $content = $this->request($url, "POST", $data); $content = json_decode($content); return $content->ticket; } public function showQRCode($sceneID, $temp = true, $expireSeconds = 604800) { $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$this->getTicket($sceneID, $temp, $expireSeconds); $content = $this->request($url); header("content-type:image/jpeg"); echo $content; } public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
0 0
- 微信开发生成二维码 接收回复消息
- 微信开发_接收消息,回复消息(3)
- java微信开发-消息接收和自动回复
- 微信开发<二>接收普通消息、自动回复
- 微信公众号开发-接收与回复消息
- 微信接收回复消息(java)
- 微信公众号开发--消息的接收与被动回复消息
- 用java开发微信公众号:接收和被动回复普通消息(三)
- 微信公众号开发--接收与回复消息(Java)
- dotNet微信公众号开发二:接收并回复普通文字消息
- 微信公众号开发(一、接收用户消息自动回复)
- 基于springboot+mybatis的微信公众号开发第三篇-消息的接收与回复
- 微信公众号开发--接收与回复消息(Java)
- 微信公众号开发(2)---接收订阅事件推送并回复消息
- 微信公众号开发--接收与回复消息(Java)
- 从零开发微信公众号系列 【三】接收和自动回复消息
- 微信开发:接收消息
- 微信公众平台 java示例 接收消息并回复
- ArrayList与LinkedList
- Ant, Gradle,Maven,jCenter
- Fedora 24 下开发Qt 5应用需要做的准备工作
- 高效的LINQ语句(三)
- 从RtlInitUnicodeString函数汇编代码理解UnicodeString结构
- 微信开发生成二维码 接收回复消息
- Android中Handler引起的内存泄露
- android学习笔记1
- Java的可变参数
- 8、DELETE 语句
- 几种肤色分割算法(OpenCV实现)
- 自定义View中的测量
- 第四课-----Linux目录结构及文件基本操作
- 最小生成树Prime->HDU1875