PHP 微信公众号开发者模式 第三方接入
2015-11-05 09:59
676 查看
<?php //define your token define("TOKEN", "weixin2015"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); class wechatCallbackapiTest{ public function valid(){ $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; $this->responseMsg();//验证后处理用户发关的消息-这里是原样返回用户的消息] 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; $type = $postObj->MsgType; $event = $postObj->Event; $c = $postObj->Location_X; $d = $postObj->Location_Y; $Label = $postObj->Label; $keyword = trim($postObj->Content); $PicUrl = trim($postObj->PicUrl); $msgType = trim($postObj->msgType); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>1</FuncFlag> </xml>"; $msgid = $postObj->MsgId; $picurl = $postObj->PicUrl; $piccontentTpl="<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>";//图片消息 //天气信息 $textHaderTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%d</ArticleCount> <Articles>"; $textContentTpl = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $textFooterTpl = "</Articles> <FuncFlag>1</FuncFlag> </xml>"; $imageTpl="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>";//图片image if(!empty($keyword)){ if(1==$keyword){ $msgType = "text"; $content1="你的openid: $fromUsername \r\n \r\n TOKEN:".TOKEN.''; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content1); echo $resultStr; }else{ //接入第三方URL、token $url = '第三方URL'; $token = '第三方TOKEN'; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); $url .= stripos($url, '?') === false ? '?' : '&'; $url .= http_build_query(array( 'signature' => $tmpStr, 'timestamp' => $timestamp, 'nonce' => $nonce, )); //echo $url; //如果不是项目相关的事件 $returnStr返回空 $header = array(); //$header[] = "Content-type: text/xml";//定义content-type为xml $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr); //curl_setopt($ch, CURLOPT_POSTFIELDS, $pp); $response = curl_exec($ch); if(curl_errno($ch)){ error_log (curl_error($ch)); } curl_close($ch); echo $response; } }else{ if($event=="subscribe"){// $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, "欢迎关注"); echo $resultStr; }else if($type=='location'){// } 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; } } } ?>
相关文章推荐
- Android调用qq聊天、qq、微信等
- 微信公众平台支付接口调用
- js和php判断当前是否为微信浏览器?
- 云龙区院:微信发布涉案财物管理流程
- 另一个国内的 Ngrok 服务器分享
- XMPP框架 微信项目开发之XMPP简介——XMPP的起源,工作原理,使用方法
- 最近做微信遇到的问题
- 将jeecms中微信后台功能和前台认证以及接收消息的功能,修改为插件形式
- 生成二维码,微信分享
- Android仿微信聊天图片缩略图裁剪
- 微信JS-SDK demo使用
- 微信支付提示缺少appid解决办法
- 张小丫第一次微信支付(讲解)
- 仿微信android客户端分享网页内容解决方案
- 微信公众号开发sprintf()函数其中%S等符号的说明
- 微信分享到朋友圈自定义图文
- XMPP框架 微信项目开发之Socket聊天室的建立与登录
- 實戰案例 - 微信平台與自有產品資料對接 (2)
- 关于MVC微信开发遇到的那些坑。
- 微信公众平台生成场景二维码