微信開發 PHP程式碼 免費送貨

qiphon3650發表於2018-06-17
<?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;
                $customrevent = $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 ($customrevent=="subscribe")
                    {$contentStr = "感謝你的關注\n回覆1檢視聯絡方式\n回覆2檢視最新資訊\n回覆3檢視法律文書";}
                break;
                case "image";
                $contentStr = "你的圖片很棒!";
                break;
                case "location";
                $geourl="http://api.map.baidu.com/telematics/v2/distance?waypoints=116.681889,23.355164;{$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c";//測距api
                $apistr=file_get_contents($geourl);
                $apiobj=simplexml_load_string($apistr);
                $distanceobj=$apiobj->results->distance;
                $distanceint=intval($distanceobj);//轉換為整數型
                $diskmint=distanceint/1000;//轉換為公里
                if ($diskmint<5)
                {$contentStr = "你離我的公司還有{$diskmint}公里遠,我們可以免費送貨上門";}
                else {
                $contentStr="你離我的公司還有{$diskmint}公里遠,我們不能送貨上門";}
                break;
                case "link" ;
                $contentStr = "你的連結有病毒吧!";
                break;
                case "text";
                  switch($keyword)
                  {
                    case "1";                               
                    $contentStr = "聯絡方式:汕頭市金平區華乾大廈703,1341702551,          email:30090032@qq.com";
                    break;
                    case "2";
                    $contentStr = "最新資訊。";
                    break;
                    case "3";
                    $contentStr = "31離婚協議\n32租房合同\n33交通事故協議書";
                    break;
                    default;
                    $contentStr ="hi";
                 }
                 break;                 
            default;
            $contentStr ="此項功能尚未開發";   
            }
                $msgType="text";
                $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;
        }
    }
}

?>

相關文章