Jump to
[1]
[Full screen preview]
<?php /** * 項(xiàng)目api接口類文件 */ class ApiModel extends Cola_Model { /* * 表名 * @var Cola_Model|null */ protected $_table = ''; /** * 是否開啟文件緩存[全局] * * @var bool */ protected $_fileCache = false; /** * baseUrl * * @var string */ protected $baseUrl = ''; //保留 protected $appKey = ''; protected $loginAppId = ''; protected $accessToken = ''; /** * 初始化 * * @param string $baseUrl 請(qǐng)求接口地址 */ public function __construct($baseUrl = '') { if (!empty($baseUrl)) { $this->baseUrl = $baseUrl; } else { $this->baseUrl = Cola::config('_bigDataApiURL'); } $u2Config = Cola::config('_OAuthU2Config');//從配置文件中獲取驗(yàn)證信息 $this->loginAppId = $u2Config['AppId'];//獲取登錄用戶的信息 $this->accessToken = 'ef4e3a2b-ae21-4cad-ada8-86ec46a8a83g';//獲取登錄用戶信息 } /** * 設(shè)置url * * @param string $url url地址 */ public function setBaseUrl($url) { $this->baseUrl = $url; } /** * 設(shè)置AccessToken * * @param void */ public function setAccessToken($token) { $this->accessToken = $token; } /** * 開啟緩存 * * @param bool $bool */ public function openCache($bool = true) { $this->_fileCache = $bool; } /** * 獲取數(shù)據(jù) * * @param $method 方法名 * @param array $params 參數(shù) * @param int $isPost 0 GET,1 POST請(qǐng)求 * @param bool $cache 緩存 * * @return mixed */ public function getData($method, $params = null, $isPost = 0, $cache = false) { if (!is_null($isPost)) { $params['accessToken'] = $this->accessToken; $params['appId'] = $this->loginAppId; } $url = $this->_getUrl($method, $params, $isPost); if (true == $cache || true == $this->_fileCache) { return $this->_getCache($url, $params, $isPost); } return $this->_curlRequest($url, $params, $isPost); } /** * 刪除緩存 * * @param $method 方法名 * @param null $params 參數(shù) * @param int $isPost 0 GET,1 POST請(qǐng)求 * * @return bool */ public function deleteCache($method, $params = null, $isPost = 0) { $url = $this->_getUrl($method, $params, $isPost); $key = $this->_cacheKey($url, $params, $isPost); $file = Cola_Com_Cache::factory(Cola::config('_fileCache')); return $file->delete($key); } /** * 獲取緩存key文件名 * * @param $method * @param null $params * @param int $isPost * * @return mixed */ public function getCacheFile($method, $params = null, $isPost = 0) { $url = $this->_getUrl($method, $params, $isPost); $file = Cola_Com_Cache::factory(Cola::config('_fileCache')); $key = $this->_cacheKey($url, $params, $isPost); return $file->getFile($key); } /** * 請(qǐng)求接口URL * * @param $method * @param null $params * @param int $isPost * * @return string */ public function getUrl($method, $params = null, $isPost = 0) { if (!is_null($isPost)) { $params['accessToken'] = $this->accessToken; $params['appId'] = $this->loginAppId; } return $this->_getUrl($method, $params, $isPost); } /** * 獲取拼接后的請(qǐng)求地址【緩存的key】 * * @param $method 方法名 * @param $params 參數(shù) * @param $isPost 0 GET,1 POST請(qǐng)求 * * @return string */ private function _getUrl($method, $params, $isPost) { $url = $this->baseUrl . $method; if ($isPost == 0) { if (is_array($params) && !empty($params)) { strpos($url, '?') === false ? $tmpParams = "?" : $tmpParams = "&"; foreach ($params as $key => $value) { $tmpParams .= $key . "=" . urlencode($value) . "&"; } $url .= rtrim($tmpParams, '&'); } } return $url; } /** * 獲取緩存數(shù)據(jù) * * @param string $url url * @param array $params 參數(shù) * @param int $isPost 0 GET,1 POST請(qǐng)求 * * @return mixed */ private function _getCache($url, $params, $isPost) { $file = Cola_Com_Cache::factory(Cola::config('_fileCache')); //緩存的key $sKey = $this->_cacheKey($url, $params, $isPost); $res = $file->get($sKey); if (!$res) { $res = $this->_curlRequest($url, $params, $isPost); if (false !== $res) { $file->set($sKey, $res); } } return $res; } /** * 設(shè)置緩存key * * @param $url * @param $params * @param $isPost * * @return string */ private function _cacheKey($url, $params, $isPost) { return $url . @json_encode($params) . $isPost; } /** * json轉(zhuǎn)換為數(shù)組 * * @param string $string * * @return mixed */ private function _json2array($string) { if (@$json = @json_decode($string, true)) { return $json; } else { return $string; } } /** * curl 請(qǐng)求 * * @param string $url 請(qǐng)求地址 * @param array $params 參數(shù)【??!僅當(dāng)POST時(shí)有用】 * @param int $isPost 0 GET,1 POST請(qǐng)求 * * @return mixed */ private function _curlRequest($url, $params = array(), $isPost = 0) { header("Content-type: text/html; charset=utf-8"); set_time_limit(0); // echo $url ; echo "<br>"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt ($curl , CURLOPT_TIMEOUT , 0 ); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36SE 2.X MetaSr 1.0'); if ($isPost == 1) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $params);//所需傳的數(shù)組用http_bulid_query()函數(shù)處理一下,就ok了 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec($curl); $result = $this->_json2array($result); return $this->_checkRequestStatus($result, $url, $params); } /** * 檢查請(qǐng)求狀態(tài) * * 參考地址:http://scmgit.staff.xdf.cn/bigdata/mdm/wikis/error_code * * @param $result * @param $url * @param $params * * @return bool */ private function _checkRequestStatus($result, $url, $params) { //錯(cuò)誤驗(yàn)證 if (empty($result) || in_array(@$result['status'], array(400, 404, 500))) { //寫入日志 $logFile = WEB_ROOT . '../app/' . 'log/api' . DS . date('Y') . DS . date('m') . DS . date('d') . '-error.log'; $log = Cola_Com::log(array('adapter' => 'File', 'params' => array('file' => $logFile, 'mode' => 0755))); $log->log("請(qǐng)求地址:" . $url . " 參數(shù):" . json_encode($params) . " 錯(cuò)誤信息:" . json_encode($result)); return false; } else{ //寫入日志 $arr = explode('/', $url); if(in_array('rule', $arr)){ $logFile = WEB_ROOT . '../app/' . 'log/api' . DS . date('Y') . DS . date('m') . DS . date('d') . '-su.log'; $log = Cola_Com::log(array('adapter' => 'File', 'params' => array('file' => $logFile, 'mode' => 0755))); $log->log("請(qǐng)求地址:" . $url . " 參數(shù):" . json_encode($params) . " 錯(cuò)誤信息:" . json_encode($result)); } } return $result; } }
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5060533 fails to install in Windows 10?
3 weeks ago
By DDD
Dune: Awakening - Where To Get Insulated Fabric
3 weeks ago
By Jack chen
Gmail Login: How to Sign Up, Sign In, or Sign Out of Gmail - MiniTool
1 months ago
By Jack chen
How to fix KB5060999 fails to install in Windows 11?
3 weeks ago
By DDD
Guild Guide In Tainted Grail: The Fall Of Avalon
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
