<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
//------------my sql ----------
$ylocal="127.0.0.1";
$yuser="root";
$ypass="LY@0419.cn";
$yname="wx";
$link = mysql_connect($ylocal, $yuser, $ypass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_query("set names utf8");
mysql_select_db($yname, $link) or die ('Can\'t use foo : ' . mysql_error());
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
//echo $echoStr;
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($postStr==""){die("");}
$dom = new DOMDocument();
$dom->loadXML($postStr);
$a=getArray($dom->documentElement);
file_put_contents("99.txt",$postStr);
//我 ToUserName
//用户 FromUserName
if($a['MsgType']=="event"){
if($a['Event']=="subscribe"){ //订阅事件
$resultStr = "<xml>
<ToUserName><![CDATA[".$a['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$a['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>";
//mysql_query("insert into `log` (`cc`,`sj`,uid) values ('开始关注','".date("Y-b-j G:i:s")."','".$a['ToUserName']."'");
}
if($a['Event']=="unsubscribe"){ //取消订阅事件
$resultStr = "<xml>
<ToUserName><![CDATA[".$a['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$a['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[再见]]></Content>
</xml>";
// mysql_query("insert into `log` (`cc`,`sj`,uid) values ('取消关注','".date("Y-b-j G:i:s")."','".$a['ToUserName']."'");
}
if($a['Event']=="CLICK"){ //点击事件
if($a['EventKey']=='V1'){
$resultStr = "<xml>
<ToUserName><![CDATA[".$a['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$a['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你点击了V1事件 \n 请输入你的银行卡帐号和密码]]></Content>
</xml>";
// mysql_query("insert into `log` (`cc`,`sj`,uid) values ('取消关注','".date("Y-b-j G:i:s")."','".$a['ToUserName']."'");
}
if($a['EventKey']=='V2'){
$resultStr =" <xml>
<ToUserName><![CDATA[".$a['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$a['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[撒旦阿苏大阿嫂大]]></Title>
<Description><![CDATA[撒旦萨的撒旦撒旦撒旦萨达三大苏打萨达]]></Description>
<PicUrl><![CDATA[http://wx.0419.cn/3.png]]></PicUrl>
<Url><![CDATA[http://wx.0419.cn/web.php?a=d]]></Url>
</item>
<item>
<Title><![CDATA[乡村vxcvsdf]]></Title>
<Description><![CDATA[sdf sd f第三方苏打粉三大分速度分速度分速度分速度分速度发]]></Description>
<PicUrl><![CDATA[http://wx.0419.cn/2.png]]></PicUrl>
<Url><![CDATA[http://wx.0419.cn/web.php?a=c]]></Url>
</item>
</Articles>
</xml> ";
// mysql_query("insert into `log` (`cc`,`sj`,uid) values ('取消关注','".date("Y-b-j G:i:s")."','".$a['ToUserName']."'");
}
if($a['EventKey']=='V3'){
$resultStr = "<xml>
<ToUserName><![CDATA[".$a['FromUserName']."]]></ToUserName>
<FromUserName><![CDATA[".$a['ToUserName']."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你点击了v3事件]]></Content>
</xml>";
// mysql_query("insert into `log` (`cc`,`sj`,uid) values ('取消关注','".date("Y-b-j G:i:s")."','".$a['ToUserName']."'");
}
}
echo $resultStr;
}
if($a['MsgType']=="text"){
$this->responseMsg();
$sj=$a['CreateTime'];
$zh=$a['FromUserName'];
$wxid=$a['MsgId'];
$xx=$a['Content'];
mysql_query("insert into flxx (`sj`,`zh`,`wxid`,`xx`) values ('$sj','$zh','$wxid','$xx')");
}
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();
$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";
if ($keyword=="客服"){
$contentStr = "13912341234";
}else if($keyword=="1"){
$contentStr = "0419-79846532";
}else if($keyword=="2"){
$contentStr = "白塔青100000号";
}else{
$contentStr = "欢迎来到XXX网络世界! \n 回复 '1' 查询电话 \n 回复 '2' 查询地址 \n 回复 '客服' 当然是查询了.";
}
$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;
}
}
}
function getArray($node) {
$array = false;
/* if ($node->hasAttributes()) {
foreach ($node->attributes as $attr) {
$array[$attr->nodeName] = $attr->nodeValue;
}
}*/
if ($node->hasChildNodes()) {
if ($node->childNodes->length == 1) {
$array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
} else {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType != XML_TEXT_NODE) {
$array[$childNode->nodeName]=$childNode->nodeValue;
}
}
}
} else {
return $node->nodeValue;
}
return $array;
}
?>
-----------------------------------------------------------------------------------------------------------
获取access_token
session_start();
$APPID="0";
$AppSecret="0";
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$AppSecret";
if(empty($_SESSION['access_token']) ){ //and time()-$_SESSION['time']>7200
$html = file_get_contents($url);
$php_json = json_decode($html); //再把json格式的数据转换成php数组
print_r($php_json);
$_SESSION['access_token']=$php_json->access_token;
$_SESSION['expires_in']=$php_json->expires_in;
$_SESSION['time']=time();
echo "新获取的 access_token :".$_SESSION['access_token'];
}else{
echo "已经获取的access_token :".$_SESSION['access_token'];
}
exit;
---------------------------------------------------------------------------------------------------------
<?php
session_start();
header("Content-type: text/html; charset=utf-8");
$sub_button=array();
$sub_button[]=array("type"=>"view","name"=>urlencode("公司简介"),"url"=>'http://wx.0419.cn/web.php?a=a');
$sub_button[]=array("type"=>"view","name"=>urlencode("公司信息"),"url"=>'http://wx.0419.cn/web.php?a=b');
$sub_button[]=array("type"=>"view","name"=>urlencode("联系我们"),"url"=>'http://wx.0419.cn/web.php?a=c');
$sub_button2=array();
$sub_button2[]=array("type"=>"click","name"=>urlencode("会员"),"key"=>'V2');
$sub_button2[]=array("type"=>"click","name"=>urlencode("申请会员"),"key"=>'V3');
$a=array();
$a['button'][]=array("type"=>"click","name"=>urlencode("近日特权"),"key"=>"V1");
$a['button'][]=array("name"=>urlencode("菜单点击"),"sub_button"=>$sub_button2);
$a['button'][]=array("name"=>urlencode("菜单跳转"),"sub_button"=>$sub_button);
$b = json_encode($a);
$c= urldecode($b);
function createMenu($data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$_SESSION['access_token']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
//获取菜单
function getMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$_SESSION['access_token']);
}
//删除菜单
function deleteMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$_SESSION['access_token']);
}
/*
if($_GET['a']=="a"){
echo createMenu($c);
}
if($_GET['a']=="b"){
echo getMenu($c);
}
if($_GET['a']=="c"){
echo deleteMenu($c);
}
?>