You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
4.3 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/8/25
  6. * Time: 16:07
  7. */
  8. require_once __DIR__.'/../api/common.php';
  9. class commonUtils extends virtifyUsers
  10. {
  11. public static $AK = "qnG9Q2oSgfGlwLUBeYBp7gTvFUCk31me"; //百度访问应用AK
  12. public static $URL = "http://api.map.baidu.com/routematrix/v2/driving";
  13. public static $GeocodingUrl = "http://api.map.baidu.com/geocoder/v2/";
  14. //发送HTTP请求
  15. static function httpsPost($url, $param = array())
  16. {
  17. $ch = curl_init(); // 初始化一个 cURL 对象
  18. curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL
  19. curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
  21. // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
  22. curl_setopt($ch, CURLOPT_POST, 1);
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串
  24. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  25. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  26. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  27. 'Content-type:application/x-www-form-urlencoded;charset=utf-8'
  28. ));
  29. $rtn = curl_exec($ch); // 运行cURL,请求网页
  30. if ($errno = curl_errno($ch)) {
  31. throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch));
  32. }
  33. curl_close($ch); // 关闭URL请求
  34. return $rtn; // 返回获取的数据
  35. }
  36. //发送get参数(链接,参数数组)
  37. static function send_get($url, $get_data)
  38. {
  39. $url_param = http_build_query($get_data);
  40. $result = file_get_contents($url . "?{$url_param}");
  41. return $result;
  42. }
  43. //获取订单信息中对ordermain表中order_description字段的解析
  44. static function fomatStr($str)
  45. {
  46. $format_str = str_replace('|', '', $str);
  47. $arr_str = explode(',', $format_str);
  48. return $arr_str[2];
  49. }
  50. //调用百度API获取两个坐标(经纬度)之间的最小时间
  51. static function baiduShortTime($origins, $destinations)
  52. {
  53. $data = array('origins' => $origins, 'destinations' => $destinations, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK);
  54. //var_dump($data);exit();
  55. $res = commonUtils::send_get(commonUtils::$URL, $data);
  56. $shortTimeList = json_decode($res, true);
  57. if (isset($shortTimeList['status']) && $shortTimeList['status'] == 0) {
  58. foreach ($shortTimeList['result'] as $res) {
  59. if (count($shortTimeList['result']) > 1) {
  60. $time[] = ceil(($res['duration']['value']) / 60);
  61. } else {
  62. $time = ceil(($res['duration']['value']) / 60);
  63. }
  64. }
  65. return $time;
  66. } else {
  67. return false;
  68. }
  69. }
  70. /**
  71. * 提供从地址到经纬度坐标或者从经纬度坐标到地址的转换服务
  72. */
  73. static function GeocodingAPI($location)
  74. {
  75. $data = array('location' => $location, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK);
  76. $res = commonUtils::send_get(commonUtils::$GeocodingUrl, $data);
  77. $addressList = json_decode($res, true);
  78. if (isset($addressList['status']) && $addressList['status'] == 0) {
  79. $res = $addressList['result']['addressComponent'];
  80. //$country = $res['country'];
  81. //$province = $res['province'];
  82. $city = $res['city'];
  83. return $city;
  84. } else {
  85. return false;
  86. }
  87. }
  88. /**
  89. * 生成订单号
  90. * @param $num
  91. */
  92. static function produceOrderId($num)
  93. {
  94. require_once __DIR__.'/../../config/Mysql.php';
  95. $pdo = conn();
  96. $sql = "SELECT FUNC_GET_UNIQUE_ID(1,{$num}) AS order_id";
  97. if (is_object($pdo)) {
  98. $result = $pdo->query($sql);
  99. $json = $result->fetchAll(PDO::FETCH_ASSOC);
  100. $result->closeCursor();
  101. }
  102. return $json[0]['order_id'];
  103. }
  104. }