private static function getJsApiTicket() { $ck = Cache::CK_WX_JSAPI_TICKET . WX_APP_ID; $ret = Cache::get($ck); if (!empty($ret)) { return $ret; } $accessToken = self::getAccessToken(); if ($accessToken === false) { return false; } $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsApi&access_token=' . $accessToken; $ret = HttpUtil::request($url); if ($ret === false) { Log::fatal('weixin - get js api ticket failed!'); return false; } $ret = json_decode($ret, true); if (!empty($ret['errcode'])) { Log::fatal('weixin - get js api ticket failed! errcode = ' . $ret['errcode'] . ' errmsg=' . $ret['errmsg']); return false; } $ticket = $ret['ticket']; $expireIn = (int) $ret['expires_in']; if (empty($ticket)) { Log::fatal('weixin - get js api ticket empty!'); return false; } Cache::setex($ck, $expireIn - 300, $ticket); return $ticket; }
public static function wxGeoConvToBaidu($coords) { if (empty($coords) || strlen($coords) < 8) { return false; } $ck = Cache::CK_BAIDU_WX_GEOCONV . $coords; $result = Cache::get($ck); if ($result !== false) { return json_decode($result, true); } $querystringArrays = array('ak' => self::APPKEY, 'coords' => $coords, 'output' => 'json', 'from' => '3', 'to' => '5'); $uri = '/geoconv/v1/'; $sn = self::caculateAKSN(self::SECURITY_KEY, $uri, $querystringArrays); $url = 'http://api.map.baidu.com/geoconv/v1/?ak=%s&coords=%s&output=%s&from=%s&to=%s&sn=%s'; $url = sprintf($url, self::APPKEY, urlencode($coords), 'json', '3', '5', $sn); $ret = HttpUtil::request($url, false, false, 2); if ($ret === false) { Log::warng('baidu ' . __METHOD__ . ' - request timeout or fail coords=' . $coords); return false; } $retDecode = json_decode($ret, true); if ($retDecode['status'] != 0) { Log::warng('baidu ' . __METHOD__ . ' - error status=' . $retDecode['status'] . ' coords=' . $coords); return false; } if (!empty($retDecode)) { Cache::set($ck, json_encode($retDecode['result'][0])); } return $retDecode['result'][0]; }