<?php /** * 用于獲取遠程文件并緩存 * CurlCache v1.0 * Created by cr. * Date: 2015/12/03 */ //namespace vendor\core; class CurlCache { /** * 請求地址 * @var string */ protected $url; /** * 緩存生存期 * @var int */ protected $expires; /** * 請求超時 * @var int */ protected $timeOut; /** * 是否開啟緩存 * @var int */ protected $cacheEna; /** * 是否顯示debug信息 * @var bool */ protected $debug; /** * 其他不用配置的內(nèi)部屬性 */ protected $path; protected $filename; protected $curlData; protected $curlHeader; protected $curlError; protected $cacheData; public $data; /** * CurlCache constructor. * @param int $expires * @param int $timeOut * @param bool|true $cacheEna * @param bool|false $debug */ public function __construct($expires=3600, $timeOut=30, $cacheEna=true, $debug=false) { $this->expires=$expires; $this->timeOut=$timeOut; $this->cacheEna=$cacheEna; $this->debug=$debug; } /** * 檢測并創(chuàng)建目錄 * @return bool */ public function mkdirs(){ if(is_dir($this->path)){ return true; }else{ return mkdir($this->path, 777, true); } } /** * 設(shè)置緩存文件相關(guān)信息 * @param $url */ public function setCacheFileInfo($url){ $this->url=$url; $this->path=dirname(__FILE__).'/cache/'; $this->filename=$this->url?md5($this->url):'null'; } /** * 主方法,取得數(shù)據(jù) * @param $url * @return bool|string */ public function get($url){ $this->setCacheFileInfo($url); if($this->cacheEna) $this->data=unserialize($this->getCache($url)); if(!$this->data) $this->data=$this->curlGet($url); if($this->data && $this->cacheEna) $this->setCache($url,serialize($this->data)); return $this->data->contents; } /** * curl GET 請求數(shù)據(jù) * @param $url * @return string */ public function curlGet($url){ if($this->debug && $url=='') echo('請求URL為空,請檢查'); $return=new stdClass(); $ch = curl_init (); curl_setopt($ch, CURLOPT_URL,$url ); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut); curl_setopt($ch, CURLOPT_HEADER, 0 ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $this->curlData=curl_exec($ch); $this->curlHeader=curl_getinfo($ch); $this->curlError=curl_error($ch); $return->header=$this->curlHeader; $return->contents=$this->curlData; curl_close($ch); if((!$this->curlData || $this->curlHeader['http_code'] != '200') && $this->debug ) echo '請求失敗,錯誤信息:'.$this->curlError; else return $return; } /** * 取得緩存數(shù)據(jù) * @param $url * @return bool|string */ public function getCache($url){ $this->setCacheFileInfo($url); if(file_exists($this->path.$this->filename)){ if(time() - filemtime($this->path.$this->filename) < $this->expires) $this->cacheData=file_get_contents($this->path.$this->filename); else if($this->debug) echo '緩存文件已過期'; if($this->cacheData) return $this->cacheData;else if($this->debug) echo '緩存文件內(nèi)容為空'; }else{ //if($this->debug) echo '緩存文件不存在'; return false; } } /** * 寫入緩存數(shù)據(jù) * @param $url * @param $data */ public function setCache($url,$data){ $this->setCacheFileInfo($url); if(!$this->mkdirs() && $this->debug) echo('創(chuàng)建緩存目錄失敗:'.$this->path); if($data){ if(!file_put_contents($this->path.$this->filename,$data) && $this->debug){ echo '寫入緩存失敗 File:'.$this->path.$this->filename; } }else{ if($this->debug) echo '數(shù)據(jù)為空,不寫入緩存'; } } }
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?
4 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)
