微信公众号开发之(15)静态地图

静态地图

  • 中心
  • zoom级别
  • 标注

需要以下接口:
百度地图API 静态地图API(暂未找到)
微信公众号开发之(15)静态地图_第1张图片微信公众号开发之(15)静态地图_第2张图片
核心代码:

                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $MsgType=$postObj->MsgType;
                $j=$postObj->Location_X;
                $w=$postObj->Location_Y;
                $time = time();
                $textTpl = "<xml>
                            <ToUserName>ToUserName>
                            <FromUserName>FromUserName>
                            <CreateTime>%sCreateTime>
                             <MsgType>MsgType>
                             <ArticleCount>1ArticleCount>
                             <Articles>
                                 <item>
                                     <Title>Title> 
                                     <Description>Description>
                                     <PicUrl>PicUrl>
                                     <Url>Url>
                                 item>
                             Articles>
                             <FuncFlag>1FuncFlag>
                            xml>";             


                if($MsgType=="location")

                {
                    $url="http://api.map.baidu.com/staticimage?width=640&height=320¢er=116.725467,23.368905&zoom=16&markers=116.724964,23.36781|{$w},{$j}&markerStyles=l,M,0xFF0000|l,Y,0x008000";

                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$url,$url);

                     echo $resultStr;

                }else{
                    echo "Input something...";
                }

index.php代码如下:


/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$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;
                $MsgType=$postObj->MsgType;
                $j=$postObj->Location_X;
                $w=$postObj->Location_Y;
                $time = time();
                $textTpl = "
                            
                            
                            %s
                             
                             1
                             
                             
                             <![CDATA[本店位置]]> 
                             
                             
                             
                             
                             
                             1
                            ";             


                if($MsgType=="location")

                {$url="http://api.map.baidu.com/staticimage?width=640&height=320¢er=116.725467,23.368905&zoom=16&markers=116.724964,23.36781|{$w},{$j}&markerStyles=l,M,0xFF0000|l,Y,0x008000";

              $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$url,$url);
              echo $resultStr;

      }else{
                    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);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>

你可能感兴趣的:(微信公众号开发)