日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php获取ios或android通过文件头(header)传过来的坐标,通过百度接口获取具体城市和地址,并存入到session中...

發布時間:2025/7/14 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php获取ios或android通过文件头(header)传过来的坐标,通过百度接口获取具体城市和地址,并存入到session中... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,在function.php方法文件中封裝一個獲取header頭文件的方法。

if (!function_exists('getallheaders')) {
  function getallheaders() {
    $headers = array();
    foreach ($_SERVER as $name => $value) {
      if (substr($name, 0, 5) == 'HTTP_') {
        $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
      }
    }
    return $headers;
  }
}

然后,在相應控制器中調用該方法獲取數據,先檢查session中有沒有位置信息。

if(!$_SESSION['MyLocation'])
{
  //獲取定位坐標
  $location = getallheaders();
  $this->get_location($location); ?//調用封裝的方法
}

最后,封裝一個方法,用來請求百度接口返回具體位置信息,并存到session中。

public function get_location($location)
{
  $lat ?= $location['Latitude'];
  $lng = $location['Longitude'];

  if($lat && $lng)
  {
    $url = 'http://api.map.baidu.com/geocoder/v2/?ak=5BFNbSgnVF5g2O72NpvTDxFm&location=' . $lat . ',' . $lng . '&output=json&pois=1';
    $html = json_decode(file_get_contents($url),true);

    if($html)
    {
      $myLocation = array(
        'city' ? => $html['result']['addressComponent']['city'],
        'addr' => ?isset($html['result']['addressComponent']['street']) ? $html['result']['addressComponent']['street'] : $html['result']['formatted_address'],
        'lng' ? ?=> $html['result']['location']['lng'],
        'lat' ? ? => $html['result']['location']['lat'],
      );

      $_SESSION['MyLocation'] = $myLocation;
    }
  }
}

?

來源:https://www.cnblogs.com/shenzikun1314/p/7467119.html

轉載于:https://www.cnblogs.com/jdwang-admin/p/7866330.html

總結

以上是生活随笔為你收集整理的php获取ios或android通过文件头(header)传过来的坐标,通过百度接口获取具体城市和地址,并存入到session中...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。