|
 |
|
ระบบ cache ไม่ทำงานอยากทราบว่าผิดตรงไหน ไม่เก็บไฟล์ cache เลยรันไม่ขึ้นช่วยด้วยครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
$url = 'https://www.google.com';
$sources = curl($url);
$cache_active = true;
$cache_time = 7200;
$cache_path = 'cache/test.cac';
if($cache_active && file_exists($cache_path) && (time() - filemtime($cache_path) < $cache_time)) {
$data = @file_get_contents($cache_path);
echo $data;
} else {
$data = @file_get_contents($sources);
$json = json_decode($data,TRUE);
if ($json['title'][0]['title'] != NULL) {
@file_put_contents($cache_path, $data);
}
echo $data;
}
function curl($url)
{
$ch = @curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$head[] = "Connection: keep-alive";
$head[] = "Keep-Alive: 300";
$head[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$head[] = "Accept-Language: en-us,en;q=0.5";
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$page = curl_exec($ch);
curl_close($ch);
return $page;
}
?>
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2018-12-09 14:42:20 |
By :
nengkub |
View :
709 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอา @ ออกไปก่อน มันปิด error
|
 |
 |
 |
 |
Date :
2018-12-09 16:34:50 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
if ($json['title'][0]['title'] != NULL) {
@file_put_contents($cache_path, $data);
}
ไปใส่เงื่อนไขครอบการเขียนไฟล์ไว้ แน่ใจแล้วเหรอว่าถ้าไม่มีไฟล์หรือไฟล์ว่างเปล่าแล้วมันจะเขียนอะไรลงไปมั้ย?
ทบทวนเงื่อนไขนี้ให้ดีสัก 5 รอบ.
|
 |
 |
 |
 |
Date :
2018-12-09 19:56:42 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|