相思资源网 Design By www.200059.com

本文实例讲述了php版微信开发之接收消息,自动判断及回复相应消息的方法。分享给大家供大家参考,具体如下:

微信的api现在己经强大到可以随意调用我们网站或指定数据库的内容进行自能回复现推送消息了,这里就来给大家介绍一个微信开发的笔记.

发送1,2,3,7测试相应的消息,菜单模式需要升级为服务号,或者订阅人数超过500以上.

1. 用户发送的消息都是通过你提交的开发者URL来接收,这里订阅号就可以搞定,发送过来的都是xml格式数据,需要解析,代码如下:

$data = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!emptyempty($data)) {//接收消息并处理
$xml = (array)simplexml_load_string($data, ‘SimpleXMLElement', LIBXML_NOCDATA);
return $xml;
}

回复也是需要返回XML的数据格式.

2. 主动发送给用户消息,查询用户信息,创建自定义菜单(现在订阅号也可以用),以及一些高级功能,都是需要获取appid 和 AppSecret 以便得到 access_token.

access_token 这东西,除接收信息,每个接口都要用到,access_token 的时间为 7200毫秒过期需要重新获取.

更多请参考:http://mp.weixin.qq.com/wiki/index.php"htmlcode">

<"TOKEN", "你的申请上的token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
 public function valid()
  {
    $echoStr = $_GET["echostr"];
    //valid signature , option
    if($this->checkSignature()){
     $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 (!emptyempty($postStr)){
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
  $msgType = "text";
        $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 ))
        {
         $contentStr = $this->keyrep($keyword);
   if( emptyempty($contentStr) )
   {
   $contentStr ="你是故意的吧,没文化真可怕";//你是故意的吧,没文化真可怕;
   }
   //$contentStr = @iconv('UTF-8','gb2312',$keyword);
         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
         echo $resultStr;
        }else{
         $contentStr = '没文化真可怕,居然不会打字!';//$this->keyrep($keyword);
         //$contentStr = @iconv('UTF-8','gb2312',$keyword);
   $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);
 $tmpStr = implode( $tmpArr );
 $tmpStr = sha1( $tmpStr );
 if( $tmpStr == $signature ){
  return true;
 }else{
  return false;
 }
 }
 function keyrep($key)
 {
 //return $key;
 if( $key=='嗨' || $key=='在吗' || $key=='你好' ){
  $mt = mt_rand(1,17);
  $array = array(1=>'自杀中,稍后再说...',2=>'有事找我请大叫!',3=>'我正在裸奔,已奔出服务区',4=>'我现在位置:WC; 姿势:下蹲; 脸部:抽搐; 状态:用力中。。。。',5=>'去吃饭了,如果你是帅哥,请一会联系我,如果你是美女...............就算你是美女,我也要先吃饱肚子啊',6=>'
洗澡中~谢绝旁观!!^_^0',7=>'有熊出"妹妹你坐船头"据说很傻逼的人看到都是唱出来的。',9=>'我这辈子只有两件事不会"我脸油吗"反光?"";
 }
 function keylist()
 {
 $array = array(1=>'嗨',2=>'你好',3=>'靠',4=>'在吗',5=>'请问');
 }
}
"_blank" href="https://www.jb51.net/Special/556.htm">PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
php,微信,接收消息,自动判断,回复消息

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“php版微信开发之接收消息,自动判断及回复相应消息的方法”

暂无php版微信开发之接收消息,自动判断及回复相应消息的方法的评论...