微信接口文档
2016-07-17 20:57
513 查看
接口配置信息的Token修改:
<?php/**
* wechat php test
*/
//define your token
define("TOKEN", "su_dong");//只要修改 su_dong 成你的配置信息
$wechatObj = new wechatCallbackapiTest();
$echoStr = $_GET["echostr"];
if(isset($echoStr)){
$wechatObj->valid();
}else{
$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)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$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 = "33333333333333";
}elseif($keyword == '11111'){
$contentStr = "22222222222222";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "9999999";
exit;
}
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
相关文章推荐
- Android自定义PopWindow(QQ、微信弹出菜单)
- 微信网页怎么用电脑打开
- 运算符练习小程序
- 微信二次分享失败
- 微信服务器ip获取
- 微信授权方法及通过授权获取临时访客的个人信息方法
- 微信的redirect_uri参数错误原因分析
- 如何使用iTunes与iTools导出微信聊天记录
- iOS开发支付集成之微信支付
- 2016年做好企业微信运营的四大关键点
- 微信收费体现的目的是什么
- 如何做好微商城等微信公众平台开发功能营销活动
- 微信开发学习记录 01
- 爬取微信公众号
- ChatForFun 公众号使用说明
- Android 实现长按录音获取实时音量显示图片(类似微信)
- Android微信支付
- IOS微信支付
- 微信jssdk用法汇总
- Android 微信支付接入