Api.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace baidu;
  3. /*
  4. 百度WebAPI接口插件
  5. */
  6. class Api
  7. {
  8. protected $AK="";
  9. public function __construct()
  10. {
  11. //执行父类的构造方法
  12. $this->AK = sysconfig('baidu.map_ak');
  13. }
  14. // IP地址转位置
  15. public function ip_to_address($ip)
  16. {
  17. $url = "http://api.map.baidu.com/location/ip?ak=".$this->AK."&ip=".$ip."&coor=bd09ll";
  18. $result=json_decode(file_get_contents($url),true);
  19. if(isset($result['status']) && 0==$result['status']){
  20. return $result['content'];
  21. }else{
  22. Wlog('BaiduAPI','ACTION:'.__FUNCTION__);
  23. Wlog('BaiduAPI','REQUEST:'.$ip);
  24. Wlog('BaiduAPI',$result['message']);
  25. return false;
  26. }
  27. }
  28. // 根据行政区域编码获取天气
  29. public function getWeather($areacode)
  30. {
  31. if(!empty($areacode)){
  32. $url = "https://api.map.baidu.com/weather/v1/?district_id=$areacode&data_type=now&ak=".$this->AK;
  33. $result = json_decode(file_get_contents($url),true);
  34. if(isset($result['status']) && 0==$result['status']){
  35. return $result['result']['now'];
  36. }else{
  37. Wlog('BaiduAPI','ACTION:'.__FUNCTION__);
  38. Wlog('BaiduAPI','REQUEST:'.$areacode);
  39. Wlog('BaiduAPI',$result['message']);
  40. return false;
  41. }
  42. }else{
  43. return false;
  44. }
  45. }
  46. }
  47. ?>