PHP完全自学手册(珍藏版) 中文pdf扫描版下载
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$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; //发送消息放ID
$toUsername = $postObj->ToUserName; //接受信息放ID
$form_MsgType = $postObj->MsgType; //消息类型
$form_Content = $postObj->Content; //消息内容
$keyword = trim($postObj->Content); //关键字
$form_Event = $postObj->Event; //获取事件类型
$form_Key = $postObj->EventKey; //获取菜单Key值
$j=$postObj->Location_X;
$w=$postObj->Location_Y;
$time = time(); //时间
$form_CreateTime = $postObj->CreateTime; //发送时间
$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>";
$newsTpl = " <xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml>";
if($form_MsgType=="text"){
if($keyword == "1")
{
$msgType = "text";
$contentStr = "这是公司介绍";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($keyword == "你妹")
{
$msgType = "text";
$contentStr = "嘿嘿 不要骂人哦!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($keyword == "2")
{
$msgType = "text";
$contentStr = "这是公司电话";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($keyword == "3")
{
$msgType = "text";
$contentStr = "这是公司地址";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($keyword == "网址")
{
$msgType = "text";
$contentStr = "http://www.kuitao8.com";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
if($keyword == "懒人程序")
{
$msgType = "news";
$title = "欢迎关注懒人程序";
$Description= "免费提供国外psd下载的中文网站,Jquery插件,jQuery图片特效,焦点幻灯Javascript, CSS, CSS3, Html5,ajax,前端代码,前端设计,图片特效,导航菜单,ui图标下载,国外psd下载,网页素材,Ui图标下载,jquery教程";
$PicUrl = "http://img.kuitao8.com/uploads/2014/0304/20140304122352531.jpg";
$url = "http://www.kuitao8.com/20140304/2081.shtml";
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType, $title,$Description,$PicUrl,$url);
echo $resultStr;
exit;
}
if (substr($keyword, -6, strlen($keyword)) == "天气"){
$keyword = trim(substr($keyword, 0, strlen($keyword) - 6));
if ($keyword == "")
{
$keyword = "北京";
}
$apihost = "http://api2.sinaapp.com/";
$apimethod = "search/weather/?";
$apiparams = array('appkey'=>"0020120430", 'appsecert'=>"fa6095e113cd28fd", 'reqtype'=>"text");
$apikeyword = "&keyword=".urlencode($keyword);
$apicallurl = $apihost.$apimethod.http_build_query($apiparams).$apikeyword;
$weatherJson = file_get_contents($apicallurl);
$weather = json_decode($weatherJson, true);
$contentStr = $weather['text']['content'];
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}else{
$msgType = "text";
$contentStr = "您可以根据提示内容,和我们进行互动,目前支持功能:[1]公司相关信息:输入序号即可\n1:公司介绍\n2:公司电话\n3:公司地址[2]酒店搜索:发送自己所在的位置\n[3]天气搜索:输入地区(市)+天气,例如“北京天气”“上海天气”";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
}
/*
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "您可以根据提示内容,和我们进行互动,目前支持功能:天气搜索,药店搜索";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
*/
$locationTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>4</ArticleCount>
<Articles>
<item>
<Title><![CDATA[你周边附近的酒店如下]]></Title>
<Description><![CDATA[s]]></Description>
<PicUrl><![CDATA[url]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[s]]></Description>
<PicUrl><![CDATA[url]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[s]]></Description>
<PicUrl><![CDATA[url]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[s]]></Description>
<PicUrl><![CDATA[url]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml>";
if($form_MsgType=="location")
{
$locationurl="http://api.map.baidu.com/telematics/v3/local?location={$w},{$j}&keyWord=酒店&number=3&ak=558E91eBd19f987f5b9c997f2e90464f";
$fa=file_get_contents($locationurl);
$f=simplexml_load_string($fa);
$d1=$f->poiList->point[0]->name;
$d2=$f->poiList->point[1]->name;
$d3=$f->poiList->point[2]->name;
$w1=$f->poiList->point[0]->address;
$w2=$f->poiList->point[1]->address;
$w3=$f->poiList->point[2]->address;
$p1=$f->poiList->point[0]->telephone;
$p2=$f->poiList->point[1]->telephone;
$p3=$f->poiList->point[2]->telephone;
$q1=$f->poiList->point[0]->distance;
$q2=$f->poiList->point[1]->distance;
$q3=$f->poiList->point[2]->distance;
$m1="{$d1}地址{$w1}电话{$p1}距离{$q1}米";
$m2="{$d2}地址{$w2}电话{$p2}距离{$q2}米";
$m3="{$d3}地址{$w3}电话{$p3}距离{$q3}米";
$resultStr = sprintf($locationTpl, $fromUsername, $toUsername, $time,$m1,$m2,$m3);
echo $resultStr;
exit;
}
if($form_MsgType=="event") //判断消息内容
{
if($form_Event=="subscribe") //判断事件类型
{
//回复欢迎文字消息
$msgType = "text";
$contentStr = "欢迎关注懒人程序:http://www.kuitao8.com";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
}
}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;
}
}
}
转载请注明:谷谷点程序 » php微信接口实例下载