Interface call model
Jump to
[1]
[Full screen preview]
<?php /** * 项目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 请求接口地址 */ public function __construct($baseUrl = '') { if (!empty($baseUrl)) { $this->baseUrl = $baseUrl; } else { $this->baseUrl = Cola::config('_bigDataApiURL'); } $u2Config = Cola::config('_OAuthU2Config');//从配置文件中获取验证信息 $this->loginAppId = $u2Config['AppId'];//获取登录用户的信息 $this->accessToken = 'ef4e3a2b-ae21-4cad-ada8-86ec46a8a83g';//获取登录用户信息 } /** * 设置url * * @param string $url url地址 */ public function setBaseUrl($url) { $this->baseUrl = $url; } /** * 设置AccessToken * * @param void */ public function setAccessToken($token) { $this->accessToken = $token; } /** * 开启缓存 * * @param bool $bool */ public function openCache($bool = true) { $this->_fileCache = $bool; } /** * 获取数据 * * @param $method 方法名 * @param array $params 参数 * @param int $isPost 0 GET,1 POST请求 * @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 参数 * @param int $isPost 0 GET,1 POST请求 * * @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); } /** * 请求接口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); } /** * 获取拼接后的请求地址【缓存的key】 * * @param $method 方法名 * @param $params 参数 * @param $isPost 0 GET,1 POST请求 * * @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; } /** * 获取缓存数据 * * @param string $url url * @param array $params 参数 * @param int $isPost 0 GET,1 POST请求 * * @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; } /** * 设置缓存key * * @param $url * @param $params * @param $isPost * * @return string */ private function _cacheKey($url, $params, $isPost) { return $url . @json_encode($params) . $isPost; } /** * json转换为数组 * * @param string $string * * @return mixed */ private function _json2array($string) { if (@$json = @json_decode($string, true)) { return $json; } else { return $string; } } /** * curl 请求 * * @param string $url 请求地址 * @param array $params 参数【!!仅当POST时有用】 * @param int $isPost 0 GET,1 POST请求 * * @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);//所需传的数组用http_bulid_query()函数处理一下,就ok了 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec($curl); $result = $this->_json2array($result); return $this->_checkRequestStatus($result, $url, $params); } /** * 检查请求状态 * * 参考地址:http://scmgit.staff.xdf.cn/bigdata/mdm/wikis/error_code * * @param $result * @param $url * @param $params * * @return bool */ private function _checkRequestStatus($result, $url, $params) { //错误验证 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("请求地址:" . $url . " 参数:" . json_encode($params) . " 错误信息:" . 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("请求地址:" . $url . " 参数:" . json_encode($params) . " 错误信息:" . json_encode($result)); } } return $result; } }
Copy after login
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

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

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

Undress AI Tool
Undress images for free

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 KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics
Java Tutorial
1668
14


CakePHP Tutorial
1427
52


Laravel Tutorial
1329
25


PHP Tutorial
1273
29


C# Tutorial
1256
24

