php 微信模板消息发送
- <?php
- ini_set ( 'date.timezone', 'Asia/Shanghai' );
- define ( 'IN_ASK2', TRUE );
- $http_type = ((isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] == 'on') || (isset ( $_SERVER ['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER ['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'https://';
- if (is_https ()) {
- define ( 'SITE_URL', 'https://' . $_SERVER ['HTTP_HOST'] . '/' );
- } else {
- define ( 'SITE_URL', 'http://' . $_SERVER ['HTTP_HOST'] . '/' );
- }
- define ( 'ENVIRONMENT', isset ( $_SERVER ['CI_ENV'] ) ? $_SERVER ['CI_ENV'] : 'production' );
- define ( 'FC_PATH', dirname ( dirname ( dirname ( __FILE__ ) ) ) . DIRECTORY_SEPARATOR );
- define ( 'BASEPATH', FC_PATH . 'system' );
- require FC_PATH . 'lib/db_mysqli.php';
- $wechatObj = new wechatCallbackapiTest ();
- if (! isset ( $_GET ['echostr'] )) {
- $wechatObj->responseMsg ();
- } else {
- $wechatObj->valid ();
- }
- //判断是否是https
- function is_https() {
- if (! empty ( $_SERVER ['HTTPS'] ) && strtolower ( $_SERVER ['HTTPS'] ) !== 'off') {
- return TRUE;
- } elseif (isset ( $_SERVER ['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER ['HTTP_X_FORWARDED_PROTO'] === 'https') {
- return TRUE;
- } elseif (! empty ( $_SERVER ['HTTP_FRONT_END_HTTPS'] ) && strtolower ( $_SERVER ['HTTP_FRONT_END_HTTPS'] ) !== 'off') {
- return TRUE;
- }
- return FALSE;
- }
- class wechatCallbackapiTest {
- var $db;
- var $token;
- function wechatCallbackapiTest() {
- $this->init_db ();
- $this->token = $this->getoken ();
- }
- function init_db() {
- require FC_PATH . 'application' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
- $dbconfig = $db ['default'];
- $db = new db ();
- define ( 'DB_TABLEPRE', $dbconfig ['dbprefix'] );
- $config = array ();
- $config ['hostname'] = $dbconfig ['hostname'];
- $config ['username'] = $dbconfig ['username'];
- $config ['password'] = $dbconfig ['password'];
- $config ['database'] = $dbconfig ['database'];
- $config ['charset'] = $dbconfig ['char_set'];
- $config ['autoconnect'] = 1;
- $config ['dbport'] = 3306;
- $config ['debug'] = true;
- $db->open ( $config );
- $this->db = $db;
- }
- function getoken() {
- $wxtoken = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "setting where k='wxtoken' limit 0,1" );
- return trim ( $wxtoken ['v'] );
- }
- //验证签名
- public function valid() {
- $echoStr = $_GET ["echostr"];
- $signature = $_GET ["signature"];
- $timestamp = $_GET ["timestamp"];
- $nonce = $_GET ["nonce"];
- $token = $this->token;
- $tmpArr = array ($token, $timestamp, $nonce );
- sort ( $tmpArr, SORT_STRING );
- $tmpStr = implode ( $tmpArr );
- $tmpStr = sha1 ( $tmpStr );
- if ($tmpStr == $signature) {
- echo $echoStr;
- exit ();
- }
- }
- //响应消息
- public function responseMsg() {
- $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]? $GLOBALS ["HTTP_RAW_POST_DATA"]:file_get_contents('php://input');
- if (! empty ( $postStr )) {
- $this->logger ( "R \r\n" . $postStr );
- $postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
- $RX_TYPE = trim ( $postObj->MsgType );
- if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")) {
- //过滤关注和取消关注事件
- } else {
- }
- //消息类型分离
- switch ($RX_TYPE) {
- case "event" :
- $result = $this->receiveEvent ( $postObj );
- break;
- case "text" :
- $result = $this->receiveText ( $postObj );
- break;
- case "image" :
- $result = $this->receiveImage ( $postObj );
- break;
- case "location" :
- $result = $this->receiveLocation ( $postObj );
- break;
- case "voice" :
- $result = $this->receiveVoice ( $postObj );
- break;
- case "video" :
- $result = $this->receiveVideo ( $postObj );
- break;
- case "link" :
- $result = $this->receiveLink ( $postObj );
- break;
- default :
- $result = "unknown msg type: " . $RX_TYPE;
- break;
- }
- $this->logger ( "T \r\n" . $result );
- echo $result;
- } else {
- echo "";
- exit ();
- }
- }
- //接收事件消息
- private function receiveEvent($object) {
- $content = "";
- switch ($object->Event) {
- case "subscribe" :
- $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" );
- $content = $site ['msg'];
- $content .= (! empty ( $object->EventKey )) ? ("\n来自二维码场景 " . str_replace ( "qrscene_", "", $object->EventKey )) : "";
- break;
- case "unsubscribe" :
- $content = "取消关注";
- break;
- case "CLICK" :
- $content = $this->getcontent ( $object->EventKey );
- break;
- case "VIEW" :
- $content = "跳转链接 " . $object->EventKey;
- break;
- case "SCAN" :
- $content = "扫描场景 " . $object->EventKey;
- break;
- case "LOCATION" :
- // $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
- break;
- case "scancode_waitmsg" :
- if ($object->ScanCodeInfo->ScanType == "qrcode") {
- $content = "扫码带提示:类型 二维码 结果:" . $object->ScanCodeInfo->ScanResult;
- } else if ($object->ScanCodeInfo->ScanType == "barcode") {
- $codeinfo = explode ( ",", strval ( $object->ScanCodeInfo->ScanResult ) );
- $codeValue = $codeinfo [1];
- $content = "扫码带提示:类型 条形码 结果:" . $codeValue;
- } else {
- $content = "扫码带提示:类型 " . $object->ScanCodeInfo->ScanType . " 结果:" . $object->ScanCodeInfo->ScanResult;
- }
- break;
- case "scancode_push" :
- $content = "扫码推事件";
- break;
- case "pic_sysphoto" :
- $content = "系统拍照";
- break;
- case "pic_weixin" :
- $content = "相册发图:数量 " . $object->SendPicsInfo->Count;
- break;
- case "pic_photo_or_album" :
- $content = "拍照或者相册:数量 " . $object->SendPicsInfo->Count;
- break;
- case "location_select" :
- $content = "发送位置:标签 " . $object->SendLocationInfo->Label;
- break;
- default :
- // $content = "receive a new event: ".$object->Event;
- break;
- }
- if($object->Event=='subscribe'||$object->Event=='SCAN'){
- $myopenid = sprintf($object->FromUserName);
- $mytoken = sprintf($object->EventKey);
- $user = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid='$myopenid' limit 0,1" );
- if($object->Event=='SCAN'){
- $type=1;
- $content = '';
- }else{
- $content = $site ['msg'];
- $type=0;
- $mytoken = substr($mytoken,-9);
- }
- if(empty($user)){
- $uid=0;
- $content .= "请先注册 ";
- }else{
- $uid=$user['uid'];
- if(empty($user['phone'])){
- $content .= "请验证手机号";
- }else{
- $content .= "欢迎登陆 ";
- }
- }
- $time = time();
- $this->db->query ( "INSERT INTO " . DB_TABLEPRE . "user_loginjilu(openid,uid,token,type,time) values ('$myopenid','$uid','$mytoken','$type',$time)" );
- $infoid = $this->db->insert_id ();
- // file_put_contents("text.txt", var_export($infoid,true)."\r\n",FILE_APPEND);
- }
- if (is_array ( $content )) {
- if (isset ( $content [0] ['PicUrl'] )) {
- $result = $this->transmitNews ( $object, $content );
- } else if (isset ( $content ['MusicUrl'] )) {
- $result = $this->transmitMusic ( $object, $content );
- }
- } else {
- if ($content == "") {
- $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" );
- $content = $site ['unword'];
- }
- $result = $this->transmitText ( $object, $content );
- }
- return $result;
- }
- private function getcontent($keyword, $object = null) {
- // runlog ( 'test', $keyword );
- $keys = array ();
- $kcontent = array ();
- $content = '';
- $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords order by id desc LIMIT 0,1000" );
- while ( $key = $this->db->fetch_array ( $query ) ) {
- //精准匹配
- if ($key ['showtype'] == 1) {
- if ($keyword == $key ['txtname']) {
- //系统关键词
- if ($key ['txttype'] == 1) {
- switch (trim ( $key ['txtcontent'] )) {
- case '#最新问题#' :
- $content = $this->newquestion ();
- if (count ( $content ) <= 0) {
- $content = "没有最新问题推荐哟";
- }
- break;
- case '#热门问题#' :
- $content = $this->hotquestion ();
- if (count ( $content ) <= 0) {
- $content = "没有热门问题推荐哟";
- }
- break;
- case '#最新文章#' :
- $content = $this->newblog ();
- if (count ( $content ) <= 0) {
- $content = "没有最新文章推荐哟";
- }
- break;
- case '#站长推荐#' :
- $content = $this->hotblog ();
- if (count ( $content ) <= 0) {
- $content = "没有站长推荐的文章哟";
- }
- break;
- case '#附近的人#' :
- break;
- case '#附近的问题#' :
- break;
- }
- } else {
- if (! empty ( $key ['title'] ) && $key ['title'] != '') {
- $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname='$keyword' order by id desc LIMIT 0,9" );
- while ( $topic = $this->db->fetch_array ( $sql ) ) {
- if (strstr ( $topic ['wburl'], 'http:' )) {
- $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => $topic ['wburl'] );
- } else {
- $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['wzid'] . '.html' );
- }
- }
- } else {
- $content = $key ['txtcontent'];
- }
- }
- if (count ( $kcontent ) > 0) {
- $content = $kcontent;
- }
- break;
- }
- } else {
- //模糊匹配
- if (strstr ( $keyword, $key ['txtname'] )) {
- if ($key ['txttype'] == 1) {
- switch (trim ( $key ['txtcontent'] )) {
- case '#最新问题#' :
- $content = $this->newquestion ();
- if (count ( $content ) <= 0) {
- $content = "没有最新问题推荐哟";
- }
- break;
- case '#热门问题#' :
- $content = $this->hotquestion ();
- if (count ( $content ) <= 0) {
- $content = "没有最新问题推荐哟";
- }
- break;
- case '#最新文章#' :
- $content = $this->newblog ();
- if (count ( $content ) <= 0) {
- $content = "没有最新文章推荐哟";
- }
- break;
- case '#站长推荐#' :
- $content = $this->hotblog ();
- if (count ( $content ) <= 0) {
- $content = "没有站长推荐的文章哟";
- }
- break;
- case '#附近的人#' :
- break;
- case '#附近的问题#' :
- break;
- }
- } else {
- if (! empty ( $key ['title'] ) && $key ['title'] != '') {
- $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname='$keyword' order by id desc LIMIT 0,9" );
- while ( $topic = $this->db->fetch_array ( $sql ) ) {
- if (strstr ( $topic ['wburl'], 'http:' )) {
- $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => $topic ['wburl'] );
- } else {
- $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['wzid'] . '.html' );
- }
- }
- } else {
- $content = $key ['txtcontent'];
- }
- }
- if (count ( $kcontent ) > 0) {
- $content = $kcontent;
- }
- break;
- }
- }
- }
- if ($keyword == "签到" || $keyword == "打卡") {
- $content = "签到记录已经收到";
- }
- if ($keyword == "账号绑定" || $content == "账号绑定") {
- $openid = $object->FromUserName;
- $getone = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid='$openid' limit 0,1" );
- if ($getone == null) {
- $url = SITE_URL . "index.php?account/bind/$openid";
- $content = "<a href='$url'>" . $getone ['username'] . "点击进入账号绑定</a>";
- } else {
- $content = $getone ['username'] . "您已经绑定账号了!";
- }
- }
- if ($keyword == 'openid') {
- $content = "您的openid:" . $object->FromUserName;
- }
- return $content;
- }
- //接收文本消息
- private function receiveText($object) {
- $keyword = trim ( $object->Content );
- $content = "";
- $content = $this->getcontent ( $keyword, $object );
- $type = '';
- $firststr = substr ( $keyword, 0, 1 );
- $laststr = substr ( $keyword, strlen ( $keyword ) - 1, 1 );
- if ($laststr == "#" && $firststr == "#") {
- $type = 'topic'; //表示文章检索
- }
- if ($laststr == "$" && $firststr == "$") {
- $type = 'question'; //表示文章检索
- }
- // //检索内容
- switch ($type) {
- case 'topic' :
- $topickeyword = trim ( $keyword, "#" );
- $content = array ();
- $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "topic where title like '%$topickeyword%' order by id desc LIMIT 0,4" );
- while ( $topic = $this->db->fetch_array ( $query ) ) {
- //$topic['viewtime'] = tdate($topic['viewtime']);
- $index = strpos ( $topic ['image'], 'http' );
- if ($index == 0) {
- $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] );
- } else {
- $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => SITE_URL . $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] );
- }
- }
- break;
- case 'question' :
- $topickeyword = trim ( $keyword, "$" );
- $content = array ();
- $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "question where title like '%$topickeyword%' order by id desc LIMIT 0,4" );
- while ( $question = $this->db->fetch_array ( $query ) ) {
- //$topic['viewtime'] = tdate($topic['viewtime']);
- $imgsrc = $question ['description'];
- if ($imgsrc == null || $imgsrc == '') {
- $imgsrc = SITE_URL . "static/css/default/avatar.gif"; //get_avatar_dir($question['authorid']);
- }
- $question ['image'] = $imgsrc;
- $content [] = array ("Title" => $question ['title'], "Description" => "", "PicUrl" => $question ['image'], "Url" => SITE_URL . 'index.php?q-' . $question ['id'] . '.html' );
- }
- break;
- }
- if ($content == "") {
- $content = array ();
- $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "question where title like '%$keyword%' order by id desc LIMIT 0,4" );
- while ( $question = $this->db->fetch_array ( $query ) ) {
- //$topic['viewtime'] = tdate($topic['viewtime']);
- $imgsrc = $question ['description'];
- if ($imgsrc == null || $imgsrc == '') {
- $imgsrc = SITE_URL . "static/css/default/avatar.gif"; //get_avatar_dir($question['authorid']);
- }
- $question ['image'] = $imgsrc;
- $content [] = array ("Title" => $question ['title'], "Description" => "", "PicUrl" => $question ['image'], "Url" => SITE_URL . 'index.php?q-' . $question ['id'] . '.html' );
- }
- $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "topic where title like '%$keyword%' order by id desc LIMIT 0,4" );
- while ( $topic = $this->db->fetch_array ( $query ) ) {
- //$topic['viewtime'] = tdate($topic['viewtime']);
- $index = strpos ( $topic ['image'], 'http' );
- if ($index == 0) {
- $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] . '.html' );
- } else {
- $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => SITE_URL . $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] . '.html' );
- }
- }
- if (count ( $content ) == 0) {
- $unword = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "setting where k='unword' limit 0,1" );
- $_content = trim ( $unword ['v'] );
- $content = empty ( $_content ) ? "小编不知道你在说啥" : $_content;
- }
- }
- // if($content=='我圣诞快乐'){
- // @require '../../lib/wxpay/hongbao/pay.php';
- //$packet = new Packet();
- ////
- //////调取支付方法
- // $result=$packet->_route('wxpacket',array('openid'=>$object->FromUserName));
- ////
- // switch ($result){
- // case 'SUCCESS':
- // $content="圣诞快乐,送了你一个红包,赶快领取吧!";
- // break;
- // default:
- // $content="红包领取失败,再接再励!".$result;
- // break;
- // }
- ////
- // }
- if (is_array ( $content )) {
- if (isset ( $content [0] )) {
- $result = $this->transmitNews ( $object, $content );
- } else if (isset ( $content ['MusicUrl'] )) {
- $result = $this->transmitMusic ( $object, $content );
- }
- } else {
- $result = $this->transmitText ( $object, $content );
- }
- return $result;
- }
- //接收图片消息
- private function receiveImage($object) {
- $content = array ("MediaId" => $object->MediaId );
- $result = $this->transmitImage ( $object, $content );
- return $result;
- }
- //接收位置消息
- private function receiveLocation($object) {
- $content = "你发送的是位置,经度为:" . $object->Location_Y . ";纬度为:" . $object->Location_X . ";缩放级别为:" . $object->Scale . ";位置为:" . $object->Label;
- $result = $this->transmitText ( $object, $content );
- return $result;
- }
- //接收语音消息
- private function receiveVoice($object) {
- if (isset ( $object->Recognition ) && ! empty ( $object->Recognition )) {
- $content = "你刚才说的是:" . $object->Recognition;
- // $object->content=$object->Recognition;
- //$this->receiveText($object);
- $result = $this->transmitText ( $object, $content );
- } else {
- $content = array ("MediaId" => $object->MediaId );
- $result = $this->transmitVoice ( $object, $content );
- }
- return $result;
- }
- //接收视频消息
- private function receiveVideo($object) {
- $content = array ("MediaId" => $object->MediaId, "ThumbMediaId" => $object->ThumbMediaId, "Title" => "", "Description" => "" );
- $result = $this->transmitVideo ( $object, $content );
- return $result;
- }
- //接收链接消息
- private function receiveLink($object) {
- $content = "你发送的是链接,标题为:" . $object->Title . ";内容为:" . $object->Description . ";链接地址为:" . $object->Url;
- $result = $this->transmitText ( $object, $content );
- return $result;
- }
- //回复文本消息
- private function transmitText($object, $content) {
- if (! isset ( $content ) || empty ( $content )) {
- return "";
- }
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[text]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), $content );
- return $result;
- }
- //回复图文消息
- private function transmitNews($object, $newsArray) {
- if (! is_array ( $newsArray )) {
- return "";
- }
- $itemTpl = " <item>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- <PicUrl><![CDATA[%s]]></PicUrl>
- <Url><![CDATA[%s]]></Url>
- </item>
- ";
- $item_str = "";
- foreach ( $newsArray as $item ) {
- $item_str .= sprintf ( $itemTpl, $item ['Title'], $item ['Description'], $item ['PicUrl'], $item ['Url'] );
- }
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[news]]></MsgType>
- <ArticleCount>%s</ArticleCount>
- <Articles>
- $item_str </Articles>
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), count ( $newsArray ) );
- return $result;
- }
- //回复音乐消息
- private function transmitMusic($object, $musicArray) {
- if (! is_array ( $musicArray )) {
- return "";
- }
- $itemTpl = "<Music>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- <MusicUrl><![CDATA[%s]]></MusicUrl>
- <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
- </Music>";
- $item_str = sprintf ( $itemTpl, $musicArray ['Title'], $musicArray ['Description'], $musicArray ['MusicUrl'], $musicArray ['HQMusicUrl'] );
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[music]]></MsgType>
- $item_str
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
- return $result;
- }
- //回复图片消息
- private function transmitImage($object, $imageArray) {
- $itemTpl = "<Image>
- <MediaId><![CDATA[%s]]></MediaId>
- </Image>";
- $item_str = sprintf ( $itemTpl, $imageArray ['MediaId'] );
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[image]]></MsgType>
- $item_str
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
- return $result;
- }
- //回复语音消息
- private function transmitVoice($object, $voiceArray) {
- $itemTpl = "<Voice>
- <MediaId><![CDATA[%s]]></MediaId>
- </Voice>";
- $item_str = sprintf ( $itemTpl, $voiceArray ['MediaId'] );
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[voice]]></MsgType>
- $item_str
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
- return $result;
- }
- //回复视频消息
- private function transmitVideo($object, $videoArray) {
- $itemTpl = "<Video>
- <MediaId><![CDATA[%s]]></MediaId>
- <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- </Video>";
- $item_str = sprintf ( $itemTpl, $videoArray ['MediaId'], $videoArray ['ThumbMediaId'], $videoArray ['Title'], $videoArray ['Description'] );
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[video]]></MsgType>
- $item_str
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
- return $result;
- }
- //回复多客服消息
- private function transmitService($object) {
- $xmlTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[transfer_customer_service]]></MsgType>
- </xml>";
- $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
- return $result;
- }
- //回复第三方接口消息
- private function relayPart3($url, $rawData) {
- $headers = array ("Content-Type: text/xml; charset=utf-8" );
- $ch = curl_init ();
- curl_setopt ( $ch, CURLOPT_URL, $url );
- curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
- curl_setopt ( $ch, CURLOPT_POST, 1 );
- curl_setopt ( $ch, CURLOPT_POSTFIELDS, $rawData );
- $output = curl_exec ( $ch );
- curl_close ( $ch );
- return $output;
- }
- //字节转Emoji表情
- function bytes_to_emoji($cp) {
- if ($cp > 0x10000) { # 4 bytes
- return chr ( 0xF0 | (($cp & 0x1C0000) >> 18) ) . chr ( 0x80 | (($cp & 0x3F000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
- } else if ($cp > 0x800) { # 3 bytes
- return chr ( 0xE0 | (($cp & 0xF000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
- } else if ($cp > 0x80) { # 2 bytes
- return chr ( 0xC0 | (($cp & 0x7C0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
- } else { # 1 byte
- return chr ( $cp );
- }
- }
- //日志记录
- private function logger($log_content) {
- if (isset ( $_SERVER ['HTTP_APPNAME'] )) { //SAE
- sae_set_display_errors ( false );
- sae_debug ( $log_content );
- sae_set_display_errors ( true );
- } else if ($_SERVER ['REMOTE_ADDR'] != "127.0.0.1") { //LOCAL
- $max_size = 1000000;
- $log_filename = "log.xml";
- if (file_exists ( $log_filename ) and (abs ( filesize ( $log_filename ) ) > $max_size)) {
- unlink ( $log_filename );
- }
- file_put_contents ( $log_filename, date ( 'Y-m-d H:i:s' ) . " " . $log_content . "\r\n", FILE_APPEND );
- }
- }
- //最新博客
- function newblog() {
- $content = array ();
- $newtopic = file_get_contents ( SITE_URL . 'index.php?api_article/clist' );
- $de_json = json_decode ( $newtopic, TRUE );
- $count_json = count ( $de_json );
- for($i = 0; $i < $count_json; $i ++) {
- $content [] = array ("Title" => $de_json [$i] ['Title'], "Description" => $de_json [$i] ['Description'], "PicUrl" => $de_json [$i] ['PicUrl'], "Url" => $de_json [$i] ['Url'] );
- }
- return $content;
- }
- //最热博客
- function hotblog() {
- $content = array ();
- $newtopic = file_get_contents ( SITE_URL . 'index.php?api_article/hotalist' );
- $de_json = json_decode ( $newtopic, TRUE );
- $count_json = count ( $de_json );
- for($i = 0; $i < $count_json; $i ++) {
- $content [] = array ("Title" => $de_json [$i] ['Title'], "Description" => $de_json [$i] ['Description'], "PicUrl" => $de_json [$i] ['PicUrl'], "Url" => $de_json [$i] ['Url'] );
- }
- return $content;
- }
- //最新问题
- function newquestion() {
- $content = array ();
- $newquestion = file_get_contents ( SITE_URL . 'index.php?api_article/newqlist' );
- $de_json = json_decode ( $newquestion, TRUE );
- $count_json = count ( $de_json );
- for($i = 0; $i < $count_json; $i ++) {
- $content [] = array ("Title" => $de_json [$i] ['title'], "Description" => '', "PicUrl" => $de_json [$i] ['avatar'], "Url" => $de_json [$i] ['url'] );
- }
- return $content;
- }
- //热门问题
- function hotquestion() {
- $content = array ();
- if (is_https ()) {
- ! define ( 'SITE_URL' ) && define ( 'SITE_URL', 'https://' . $_SERVER ['HTTP_HOST'] . '/' );
- } else {
- ! define ( 'SITE_URL' ) && define ( 'SITE_URL', 'http://' . $_SERVER ['HTTP_HOST'] . '/' );
- }
- $hotquestion = file_get_contents ( SITE_URL . 'index.php?api_article/hotqlist' );
- $de_json = json_decode ( $hotquestion, TRUE );
- $count_json = count ( $de_json );
- for($i = 0; $i < $count_json; $i ++) {
- $content [] = array ("Title" => $de_json [$i] ['title'], "Description" => '', "PicUrl" => $de_json [$i] ['avatar'], "Url" => $de_json [$i] ['url'] );
- }
- return $content;
- }
- }
- ?>
php 微信模板消息发送的更多相关文章
- PHP实现微信模板消息发送给指定用户
使用公众号的模板消息功能,必须是认证过的服务号,需要发送微信号的openid,同一微信号在不同公众号下的openid是不同的,在公众号下是唯一的,获取不到微信号 进入公众平台 功能->模板消息 ...
- PHP微信模板消息发送
<?php class Wxtemplate extends Base { function __construct() { $this->appid = config('WXAPP_AP ...
- 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...
- C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...
- 前后端分离djangorestframework—— 接入微信模板消息推送
微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...
- java开发微信模板消息推送
发布时间:2018-12-12 技术:springboot+maven 概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...
- 5分钟连续出现某现象+微信模板消息提醒 PHP
需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis key 设为:插座编号+user户编号 value ...
- 应用jfinal发送微信模板消息的一个bug
严格来讲,这不是一个bug,只是我们应用的方式不对.微信发送模板消息的方法是: HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessTokenStr ...
- ThinkPHP3.2.3发送微信模板消息
一.开通模板消息功能 所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限:需要选择公众账号服务所处的2个行业,每月可 ...
随机推荐
- 使用kubeadm平滑地升级kubenetes集群(v1.10.2到v1.10.3)
写在前面 kubernetes是目前最炙手火热的容器管理.调度.编排平台,不仅拥有全面的生态系统,而且还开源.开放自治,谁贡献的多,谁的话语权就大,云提供商都有专门的工程师来从事kubernetes开 ...
- 关于raft算法
列出一些比较好的学习资料, 可以经常翻一番,加深印象 0 raft官方git 1 raft算法动画演示 2 Raft 为什么是更易理解的分布式一致性算法 3 raft一致性算法 4 Raf ...
- Python 闭包小记
闭包就是能够读取其他函数内部变量的函数.例如在javascript中,只有函数内部的子函数才能读取局部变量,所以闭包可以理解成“定义在一个函数内部的函数“.在本质上,闭包是将函数内部和函数外部连接起来 ...
- GC参考手册 —— GC 算法(基础篇)
本章简要介绍GC的基本原理和相关技术, 下一章节再详细讲解GC算法的具体实现.各种垃圾收集器的实现细节虽然并不相同,但总体而言,垃圾收集器都专注于两件事情: 查找所有存活对象 抛弃其他的部分,即死对象 ...
- 面向对象之七大基本原则(javaScript)
1. 前言 2. 单一职责 3. 开闭原则 4. 里氏替换 5. 依赖倒置 6. 接口隔离 7. 迪米特法则 8. 组合聚合复用原则 9. 总结 1. 前言 面向对象编程有自己的特性与原则,如果对于面 ...
- java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介
本文对常用的反编译工具进行简单介绍 JD-GUI.procyon-decompiler.luyten.crf 反编译工具分类 JD-GUI JDK7以及之前可以使用 JD-GUI,如果版本&g ...
- SLAM+语音机器人DIY系列:(二)ROS入门——8.理解roslaunch在大型项目中的作用
摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...
- 树莓派播放视频的播放器omxplayer
omxplyer为树莓派量身定做的一款GPU硬件加速的播放器,很好的解决了树莓派cpu计算力不足的缺点.(播放时cpu一定都不烫手) 1.安装方法: CTRL + ALT + T 调出终端命令行输入 ...
- C#工具:加密解密帮助类
using System; using System.IO; using System.Security.Cryptography; using System.Text; //加密字符串,注意strE ...
- int-Integer-String之间的转换方式
1.int和Integer之间的转换: 1) int----->Integer ①自动装箱 ②Integer的构造方法 ③调用Integer的静态方法:static Integer valu ...