-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_flickr_api.php
134 lines (90 loc) · 2.73 KB
/
lib_flickr_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
#
# $Id$
#
loadlib("http");
#################################################################
$GLOBALS['cfg']['flickr_api_endpoint'] = 'http://api.flickr.com/services/rest/';
$GLOBALS['cfg']['flickr_auth_endpoint'] = 'http://api.flickr.com/services/auth/';
#################################################################
function flickr_api_authtoken_perms_map($string_keys=0){
$map = array(
'0' => 'read',
'1' => 'write',
'2' => 'delete',
);
if ($string_keys){
$map = array_flip($map);
}
return $map;
}
#################################################################
function flickr_api_auth_url($perms, $extra=null){
$args = array(
'api_key' => $GLOBALS['cfg']['flickr_api_key'],
'perms' => $perms,
);
if ($extra){
$extra = http_build_query($extra);
$args['extra'] = $extra;
}
$api_sig = _flickr_api_sign_args($args);
$args['api_sig'] = $api_sig;
$url = $GLOBALS['cfg']['flickr_auth_endpoint'] . "?" . http_build_query($args);
return $url;
}
#################################################################
function flickr_api_call_build($method, $args=array(), $more=array()){
$args['api_key'] = $GLOBALS['cfg']['flickr_api_key'];
$args['method'] = $method;
$args['format'] = 'json';
$args['nojsoncallback'] = 1;
if ((isset($args['auth_token'])) || (isset($more['sign']))){
$api_sig = _flickr_api_sign_args($args);
$args['api_sig'] = $api_sig;
}
$url = $GLOBALS['cfg']['flickr_api_endpoint'];
return array($url, $args);
}
#################################################################
function flickr_api_call($method, $args=array(), $more=array()){
list($url, $args) = flickr_api_call_build($method, $args, $more);
$defaults = array(
'http_timeout' => 10,
);
$more = array_merge($defaults, $more);
$headers = array();
$rsp = http_post($url, $args, $headers, $more);
# $url = $url . "?" . http_build_query($args);
# $rsp = http_get($url);
if (! $rsp['ok']){
return $rsp;
}
if (isset($more['raw'])){
return $rsp;
}
$json = json_decode($rsp['body'], 'as a hash');
if (! $json){
return array( 'ok' => 0, 'error' => 'failed to parse response' );
}
if ($json['stat'] != 'ok'){
return array( 'ok' => 0, 'error' => $json['message']);
}
unset($json['stat']);
return array( 'ok' => 1, 'rsp' => $json );
}
#################################################################
function _flickr_api_sign_args($args){
$parts = array(
$GLOBALS['cfg']['flickr_api_secret']
);
$keys = array_keys($args);
sort($keys);
foreach ($keys as $k){
$parts[] = $k . $args[$k];
}
$raw = implode("", $parts);
return md5($raw);
}
#################################################################
?>