多个域脚本的API密钥,谷歌地图V2

时间:2010-09-30 22:36:52

标签: php google-maps-api-2

任何人都可以检查下面我的脚本无效的原因吗?

<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=
<?php  
$this->googleMapsApiKey = $this->getValueFromDB("google", "googleMapsApiKey");
            if ($_SERVER['HTTP_HOST']=='www.ABC.info') { 
        $this->googleMapsApiKey = "Googlemap-keys";
            } elseif ($_SERVER['HTTP_HOST']=='www.CBA.com') { 
        $this->googleMapsApiKey = "Googlemap-keys";
        } 
?>" type="text/javascript"></script>

非常感谢!!

2 个答案:

答案 0 :(得分:2)

您可以避免这个问题:Maps API密钥现在支持多个域(您可以随时编辑授权域)。有关详细信息,请参阅Obtaining an API key

答案 1 :(得分:0)

我完全赞同Wrikken,在此过程中似乎没有回应变量。也许这种方法会有所帮助:

            <?php  
            print '<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=';
            $this->googleMapsApiKey = $this->getValueFromDB("google", "googleMapsApiKey");
                      switch ($_SERVER['HTTP_HOST'])
                      {
                     //Specific for a domain, but I think that the default handles it against your DB automatically
                     //case 'www.ABC.info' : $this->googleMapsApiKey = "Googlemap-keys";
                     // break;

                     //Same here
                     // case 'www.CBA.com': $this->googleMapsApiKey = "Googlemap-keys";
                     // break;

                      default: $this->googleMapsApiKey = "Googlemap-keys";
                      }

            echo "$this->googleMapsApiKey type=\"text/javascript\"></script>";  

            ?>