微信开发

来源:互联网 发布:淘宝刀具 编辑:程序博客网 时间:2024/05/22 04:54
<?phpdefine("TOKEN","zhaohao");if(isset($_GET['echostr'])){        //实现网址接入,调用valid函数        valid();    }else{        responseMsg();    //调用响应消息函数    }//验证消息function valid(){    if(checkSignature()){            $echostr = $_GET['echostr'];            if($echostr){                echo $echostr;                exit;            }else{        echo "error";                exit;            }        }    }//检查签名function checkSignature(){$signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);        // use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;        }else{        return false;        }    }//回应消息 function responseMsg(){         //1.接受微信服务器传来的post         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];         if(!$postStr){             echo "error";             exit;         }         //2.解析xml数据包         $obj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);                  //1)获得接收者的信息         //$ToUserName=$obj->ToUserName;         //1)获得发送者的信息         //$FromUserName=$obj->FromUserName;         //发送来的类型         $MsgType=$obj->MsgType;         switch($MsgType){             case "event":                 switch($obj->Event){                     case "subscribe":                         $reply="“欢迎关注我,谢谢”\r\n回复“新闻”,查看新闻\r\n也可当做简易计算器使用,例如输入“103+22”";                         echo replyText($obj,$reply);                     break;                                              case "unsubscribe":                                              break;                 }                 break;                              case "text":                 $content=$obj->Content;                 preg_match('/(\d+)([+-])(\d+)/i',$content,$res);                 if($res[2]=='+'){                     echo jsq($obj,$content);                 }                 else{                     switch($content){                         case "新闻":                             echo replyNews($obj);                             break;                                                      case "点歌":                             echo replyMusic($obj);                             break;                     }                 }              break;                              default:                 break;         }          }function replyText($obj,$reply){        //preg_match('/(\d+)([+-])(\d+)/i',$content,$res);        //if($res[2]=='+'){        //    $result1=$res[1]+$res[3];      //  }else if($res[2]=='-'){       //     $result1=$res[1]-$res[3];       // }        $replyxml="<xml>                     <ToUserName><![CDATA[%s]]></ToUserName>                     <FromUserName><![CDATA[%s]]></FromUserName>                     <CreateTime>%s</CreateTime>                     <MsgType><![CDATA[text]]></MsgType>                     <Content><![CDATA[%s]]></Content>                  </xml>";        $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$reply);        return $result;    }//计算器    function jsq($obj,$content){        preg_match('/(\d+)([+-])(\d+)/i',$content,$res);            if($res[2]=='+'){                $result1=$res[1]+$res[3];            }else if($res[2]=='-'){                $result1=$res[1]-$res[3];            }            $replyxml="<xml>                         <ToUserName><![CDATA[%s]]></ToUserName>                         <FromUserName><![CDATA[%s]]></FromUserName>                         <CreateTime>%s</CreateTime>                         <MsgType><![CDATA[text]]></MsgType>                         <Content><![CDATA[%s]]></Content>                      </xml>";            $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$result1);            return $result;        }//新闻function replyNews($obj){        $replyxml="<xml>                    <ToUserName><![CDATA[%s]]></ToUserName>                    <FromUserName><![CDATA[%s]]></FromUserName>                    <CreateTime>%s</CreateTime>                    <MsgType><![CDATA[news]]></MsgType>                    <ArticleCount>2</ArticleCount>                    <Articles>                    <item>                    <Title><![CDATA[%s]]></Title>                     <Description><![CDATA[%s]]></Description>                    <PicUrl><![CDATA[%s]]></PicUrl>                    <Url><![CDATA[%s]]></Url>                    </item>                    <item>                    <Title><![CDATA[%s]]></Title>                    <Description><![CDATA[%s]]></Description>                    <PicUrl><![CDATA[%s]]></PicUrl>                    <Url><![CDATA[%s]]></Url>                    </item>                    </Articles>                  </xml> ";       $title1="zyh";        $title2="yxy";                $description1="zyhde";        $description2="yxyde";                $picur1="http://1.zhaohao.applinzi.com/img/psb.jpg";        $picur2="http://1.zhaohao.applinzi.com/img/timg.jpg";                $url1="https://www.baidu.com/";        $url2="https://www.baidu.com/";        $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$title1,$description1,$picur1,$url1,$title2,$description2,$picur2,$url2);        return $result;    }    function replyMusic($obj){            $replyxml="<xml>                        <ToUserName><![CDATA[%s]]></ToUserName>                        <FromUserName><![CDATA[%s]]></FromUserName>                        <CreateTime>%s</CreateTime>                        <MsgType><![CDATA[music]]></MsgType>                        <Music>                        <Title><![CDATA[%s]]></Title>                        <Description><![CDATA[%s]]></Description>                        <MusicUrl><![CDATA[%s]]></MusicUrl>                        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>                        </Music>                      </xml>";            $title="勋章";            $des="zyhde";            $murl="http://1.zhaohao.applinzi.com/music/1.mp3";               $hmurl="http://1.zhaohao.applinzi.com/music/1.mp3";                    $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$title,$des,$murl,$hmurl);            return $result;        }

0 0
原创粉丝点击