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个行业,每月可 ...
随机推荐
- 如何在Microsoft Word里面插入图片作为背景/封面?
Stay hungry, Stay foolish. 如何在Word里面插入图片作为背景?其实很简单,开门见山,我们只需要这几步即可! 1.第一步,打开要插入图片的Word 2.第二步,插入图 ...
- bootStrap-table服务器端后台分页的使用,以及自定义搜索框的实现,前端代码到数据查询超详细讲解
关于分页,之前一直纯手写js代码来实现,最近又需要用到分页,找了好多最终确定bootstrap-table,正好前端页面用的是bootstrap. 首先下载BootStrap-table的js和CSS ...
- Eclipse中使用Maven搭建SSM框架
Eclipse中不使用Maven搭建SSM框架:https://www.cnblogs.com/xuyiqing/p/9569459.html IDEA中使用Maven搭建SSM框架:https:// ...
- Servlet不再是烦恼
Servlet 一.什么是Servlet? Servlet是在服务器上运行的小程序,也就是一个Java类,但比较特殊,不需要new,自动就可以运行.也有创建.垃圾回收和销毁过程.Servlet是Jav ...
- 怎么用Mac电脑创建多个桌面
区别于win的单个桌面,Mac电脑可以设置多个桌面,方面用户处理各种多乱杂的情况.究竟怎么用Mac电脑创建多个桌面呢?一起来看看吧! 1.首先打开Mission Control,点击偏好设置 2.然后 ...
- 第1章 程序设计和C语言
1.1什么是计算机程序 程序,就是一组计算机能识别和执行的指令.每一条指令使计算机执行特定的操作.只要让计算机执行这个程序,计算机就会“自动地”执行各条指令,有条不紊地进行工作. 1.2什么是计算机语 ...
- Java下载创建好的Excel模板
1.废话不多说,直接上源码 //从数据库取数据创建表格 private HSSFWorkbook exportStudentInfo(List<ExamStudentVo> student ...
- Entity Framework 之存储过程篇
最近几天在搞CRUD,使用的是EF这个ORM,最近的项目中上了存储过程,就把在开发中的经验分享出来!我们先创建一个最基本的存储过程,脚本如下,这是一个不带参数的存储过程,我们从最简单的往上走! cre ...
- 『集群』004 Slithice 集群分布式(多个客户端,基于中央服务器的集群服务)
Slithice 集群分布式(多个客户端,基于中央服务器的多个集群服务端) 案例Demo展示: 集群架构图 如下: 如上图,上图 展示了 这个集群 的 结构: >一个中央服务器(可以有多个),负 ...
- Asp.NetCore轻松学-业务重点-实现一个简单的手机号码验证
前言 本文纯干货,直接拿走使用,不用付费.在业务开发中,手机号码验证是我们常常需要面对的问题,目前市场上各种各样的手机号码验证方式,比如正则表达式等等,本文结合实际业务场景,在业务级别对手机号 ...