<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
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 = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!emptyempty($postStr)){
$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(!emptyempty( $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()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
//$wechatObj->valid(); $wechatObj->responseMsg();
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
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 = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$MsgType = $postObj->MsgType; //add
$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($MsgType != 'event') {
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
}else{
echo "Input something...";
}
} else {
$msgType = "text";
$contentStr = "感谢您关注AndyYang个人博客微信小助手。\r\n".
"回复【1】返回两篇最新文章\r\n".
"回复【2】返回两篇人气文章\r\n".
"回复【3】返回两篇热评文章\r\n".
"回复【4】返回两篇最新技术文章\r\n".
"回复【5】返回两篇最新写作文章\r\n".
"回复其他返回搜索关键字的两篇文章\r\n".
"更多精彩内容,尽在:<a href='http://www.1sucai.cn'>www.1sucai.cn</a>。亲们,请多多支持哦,谢谢~";
;
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING); //这个在新的sdk中添加了第二个参数(compare items as strings)
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->weixin_run();
class wechatCallbackapiTest {
private $fromUsername;
private $toUsername;
private $times;
private $keyword;
private $MsgType;
public function responseMsg() {
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!emptyempty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->fromUsername = $postObj->FromUserName;
$this->toUsername = $postObj->ToUserName;
$this->keyword = trim($postObj->Content);
$this->time = time();
$this->MsgType = $postObj->MsgType;
} else {
echo "Pay attention to <a href='http://{$_SERVER['HTTP_HOST']}'>http://{$_SERVER['HTTP_HOST']}</a>,thanks!";
exit;
}
}
public function weixin_run() {
$this->responseMsg();
if($this->MsgType != 'event') { //attention
$data = $this->getData();
$this->fun_xml("news", $data, count($data));
} else {
$data = $this->getWelData();
$this->fun_xml("text", $data, 1);
}
}
//type: text 文本类型, news 图文类型
//text,array(内容),array(ID)
//news,array(array(标题,介绍,图片,超链接),...小于10条),条数
private function fun_xml($type, $value_arr, $count) {
$con="<xml>
<ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
<FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
<CreateTime>{$this->times}</CreateTime>
<MsgType><![CDATA[{$type}]]></MsgType>";
switch($type) {
case "text" :
$con.="<Content><![CDATA[$value_arr]]></Content>";
break;
case "news" :
$con.="<ArticleCount>{$count}</ArticleCount>
<Articles>";
foreach($value_arr as $key => $v) {
$con.="<item>
<Title><![CDATA[{$v[0]}]]></Title>
<Description><![CDATA[{$v[1]}]]></Description>
<PicUrl><![CDATA[{$v[2]}]]></PicUrl>
<Url><![CDATA[{$v[3]}]]></Url>
</item>";
}
$con.="</Articles>";
break;
}
echo $con."</xml>";
}
private function getData() {
//数据库通过关键字查询文章
//。。。。。。。。。。。。
//。。。。。。。。。。。。
//返回文章结果的数组
return $data;
}
private function getWelData() {
$data = "感谢您关注AndyYang个人博客微信小助手。\r\n".
"回复【1】返回两篇最新文章\r\n".
"回复【2】返回两篇人气文章\r\n".
"回复【3】返回两篇热评文章\r\n".
"回复【4】返回两篇最新技术文章\r\n".
"回复【5】返回两篇最新写作文章\r\n".
"回复其他返回搜索关键字的两篇文章\r\n".
"更多精彩内容,尽在:<a href='http://www.1sucai.cn/'>www.1sucai.cn</a>。亲们,请多多支持哦,谢谢~";
;
return $data;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有