日期:2014-05-16  浏览次数:20685 次

关于微信开发模式的PHP代码
<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$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;
                $toUsername = $postObj->ToUserName;
$type =$postObj->MsgType;
$customevent = $postObj->Event;
$latitude =$postObj->Location_X;
$longitude =$postObj->Location_Y;
                $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>";             
switch($type)
{
case "event";
if($customevent == "subscribe")
{$contentStr = "感谢关注"}
break;
case "image"
$contentStr ="图片很棒";
    break;
case "location";
$contentStr ="{$latitude},{longitude}";
break;
case "text";
switch($keyword)
{case "1"
 $contentStr="1"
 break;
 case "2"
 $contentStr ="2"
 break;
 default;
 $contentStr= "hi";}
 break;
 }
 $msgType = "text";
 $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
 echo $resultStr;
 }else    &n