forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
60 lines (59 loc) · 1.8 KB
/
api.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
<?php
function MakeDir( $path ) {
return is_writeable( $path ) || mkdir( $path, 0777, true );
}
function TryGetParam( $key, &$value ){
if( isset( $_GET[$key] ) && $_GET[$key] ){
$value = $_GET[$key];
return true;
}
return false;
}
function GetUrlContent( $url ){
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16');
$res=curl_exec($curl);
curl_close($curl);
return $res;
}
abstract class API {
protected $service;
protected $key;
protected $data;
protected $cachetime;
public function __construct( ){
$this->cachetime = 86400;
$this->service = strtolower( get_class( $this ) );
$this->data = null;
$this->LoadData( );
}
protected function LoadData( ) {
$this->LoadRemote( );
}
public function GetData( $callback = null ) {
$data = json_encode( $this->data );
if( is_null( $callback ) ) {
return $data;
} else {
return "{$callback}({$data})";
}
}
protected abstract function LoadRemote( );
}
if( TryGetParam( 'service', $service ) ) {
$service = strtolower( $service );
$apifile = "api/{$service}.php";
if( file_exists( $apifile ) ){
include( $apifile );
$api = new $service( );
if( TryGetParam( 'callback', $callback ) ) {
header( 'Content-type:text/javascript' );
echo $api->GetData( $callback );
} else {
header( 'Content-type:application/json' );
echo $api->GetData( );
}
}
}