-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.php
71 lines (58 loc) · 1.62 KB
/
custom.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
define('PROJECT_DIR', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
function readURL($url, $cacheSeconds = null)
{
$filename = 'cache/'.urlencode($url).'.html';
if($cacheSeconds)
{
if(file_exists($filename))
{
if(time() - filemtime($filename) < $cacheSeconds)
return file_get_contents($filename);
}
}
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$ret = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
if($ret && $cacheSeconds)
{
file_put_contents($filename, $ret);
}
return $ret;
}
class myDB
{
/** @var PDO */
public static $pdo = null;
public static function init()
{
$filename = PROJECT_DIR.'db.sqlite';
self::$pdo = new PDO('sqlite:'.$filename);
return self::$pdo;
}
public static function doQuery($sql, array $params)
{
$sth = myDB::$pdo->prepare($sql);
if($sth->execute($params))
$ret = $sth->fetchAll();
else
print_r(myDB::$pdo->errorInfo());
return $ret;
}
public static function doInsert($sql, array $params)
{
$sth = myDB::$pdo->prepare($sql);
if($sth)
$ret = $sth->execute($params);
else
print_r(myDB::$pdo->errorInfo());
return $ret;
}
}