<?php /** * 用于獲取遠(yuǎn)程文件并緩存 * CurlCache v1.0 * Created by cr. * Date: 2015/12/03 */ //namespace vendor\core; class CurlCache { /** * 請(qǐng)求地址 * @var string */ protected $url; /** * 緩存生存期 * @var int */ protected $expires; /** * 請(qǐng)求超時(shí) * @var int */ protected $timeOut; /** * 是否開(kāi)啟緩存 * @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; } /** * 檢測(cè)并創(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 請(qǐng)求數(shù)據(jù) * @param $url * @return string */ public function curlGet($url){ if($this->debug && $url=='') echo('請(qǐng)求URL為空,請(qǐng)檢查'); $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 '請(qǐng)求失敗,錯(cuò)誤信息:'.$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 '緩存文件已過(guò)期'; if($this->cacheData) return $this->cacheData;else if($this->debug) echo '緩存文件內(nèi)容為空'; }else{ //if($this->debug) echo '緩存文件不存在'; return false; } } /** * 寫(xiě)入緩存數(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 '寫(xiě)入緩存失敗 File:'.$this->path.$this->filename; } }else{ if($this->debug) echo '數(shù)據(jù)為空,不寫(xiě)入緩存'; } } }
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
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months 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)