|
- <?php
-
- /**
- * Created by PhpStorm.
- * User: Steven
- * Date: 2016/8/25
- * Time: 16:07
- */
- require_once __DIR__.'/../api/common.php';
- class commonUtils extends virtifyUsers
- {
- public static $AK = "qnG9Q2oSgfGlwLUBeYBp7gTvFUCk31me"; //百度访问应用AK
- public static $URL = "http://api.map.baidu.com/routematrix/v2/driving";
- public static $GeocodingUrl = "http://api.map.baidu.com/geocoder/v2/";
-
- //发送HTTP请求
- static function httpsPost($url, $param = array())
- {
- $ch = curl_init(); // 初始化一个 cURL 对象
- curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL
- curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
- // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-type:application/x-www-form-urlencoded;charset=utf-8'
- ));
- $rtn = curl_exec($ch); // 运行cURL,请求网页
- if ($errno = curl_errno($ch)) {
- throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch));
- }
- curl_close($ch); // 关闭URL请求
- return $rtn; // 返回获取的数据
- }
-
- //发送get参数(链接,参数数组)
- static function send_get($url, $get_data)
- {
- $url_param = http_build_query($get_data);
- $result = file_get_contents($url . "?{$url_param}");
- return $result;
- }
-
- //获取订单信息中对ordermain表中order_description字段的解析
- static function fomatStr($str)
- {
- $format_str = str_replace('|', '', $str);
- $arr_str = explode(',', $format_str);
- return $arr_str[2];
- }
-
- //调用百度API获取两个坐标(经纬度)之间的最小时间
- static function baiduShortTime($origins, $destinations)
- {
- $data = array('origins' => $origins, 'destinations' => $destinations, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK);
- //var_dump($data);exit();
- $res = commonUtils::send_get(commonUtils::$URL, $data);
- $shortTimeList = json_decode($res, true);
- if (isset($shortTimeList['status']) && $shortTimeList['status'] == 0) {
- foreach ($shortTimeList['result'] as $res) {
- if (count($shortTimeList['result']) > 1) {
- $time[] = ceil(($res['duration']['value']) / 60);
- } else {
- $time = ceil(($res['duration']['value']) / 60);
- }
-
- }
- return $time;
- } else {
- return false;
- }
- }
-
- /**
- * 提供从地址到经纬度坐标或者从经纬度坐标到地址的转换服务
- */
- static function GeocodingAPI($location)
- {
- $data = array('location' => $location, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK);
- $res = commonUtils::send_get(commonUtils::$GeocodingUrl, $data);
- $addressList = json_decode($res, true);
- if (isset($addressList['status']) && $addressList['status'] == 0) {
- $res = $addressList['result']['addressComponent'];
- //$country = $res['country'];
- //$province = $res['province'];
- $city = $res['city'];
- return $city;
- } else {
- return false;
- }
- }
-
- /**
- * 生成订单号
- * @param $num
- */
- static function produceOrderId($num)
- {
- require_once __DIR__.'/../../config/Mysql.php';
- $pdo = conn();
- $sql = "SELECT FUNC_GET_UNIQUE_ID(1,{$num}) AS order_id";
- if (is_object($pdo)) {
- $result = $pdo->query($sql);
- $json = $result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
- }
- return $json[0]['order_id'];
- }
-
- }
|