url = $url; $this->dir = __DIR__ . '/../admin/logs/version/'; $this->fileName = 'version.json'; $this->filePath = $this->dir . $this->fileName; $this->fileModifiTime = filemtime($this->filePath); $this->time = time(); } public function readJson($name = 'tag_name') { if (is_file($this->filePath)) { if ($this->time - $this->fileModifiTime > 864000) { $this->downJson(); } else { $file = fopen($this->filePath, 'r'); $test = fread($file, filesize($this->filePath)); $version = json_decode($test, true); return $version[$name]; fclose($file); } } else { $this->downJson(); } } public function downJson() { if (!is_dir($this->dir)) { mkdir($this->dir, 0755, true); } $version = $this->geturl($this->url); $version = json_decode($version, true); $file = fopen($this->filePath, 'w+'); fwrite($file, $version); fclose($file); } public function geturl($url) { $headerArray = array("Content-type:application/json;", "Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT_MS, 666); // 超时时间 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'); $output = curl_exec($ch); curl_close($ch); $output = json_encode($output, true); return $output; } } /////////// TEST ///////// /* $url = "https://api.github.com/repositories/188228357/releases/latest"; $test = new getVersion($url); echo $test->readJson(); */