forked from hugoh/Offlickr2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoPhpFlickr.php
76 lines (66 loc) · 2.65 KB
/
oPhpFlickr.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
<?php
require_once('./phpflickr/phpFlickr.php');
require_once('./scribe-php/src/test/bootstrap.php');
class oPhpFlickr extends phpFlickr {
function __construct($api_key, $secret = NULL, $die_on_error = false) {
parent::__construct($api_key, $secret, $die_on_error);
$builder = new ServiceBuilder();
$this->oauth_service = $builder->provider(new FlickrApi())
->apiKey($api_key)
->apiSecret($secret)
->build();
}
function authorize_console() {
// Obtain the Request Token
$requestToken = $this->oauth_service->getRequestToken();
print("Go and authorize the application here:\n");
print($this->oauth_service->getAuthorizationUrl($requestToken) . "\n");
fwrite(STDOUT, "And paste the code you're given here: ");
$verifier = new Verifier(trim(fgets(STDIN)));
print("\n");
// Trade the Request Token and Verfier for the Access Token
$accessToken = $this->oauth_service->getAccessToken($requestToken, $verifier);
return $accessToken;
}
function request ($command, $args = array(), $already_tried=0) {
// NOTE: cache not implemented
$args = array_merge(
array("url" => $this->rest_endpoint,
"method" => $command,
"format" => "json",
"nojsoncallback" => "1"),
$args);
$request = new OAuthRequest(Verb::POST, $args['url']);
foreach ($args as $key => $value) {
$request->addBodyParameter($key, $value);
print("*** request[" . $key . "]=" . $value . "\n");
}
$this->oauth_service->signRequest($this->token, $request);
$response_object = $request->send();
$response = $response_object->getBody();
$this->parsed_response = json_decode($response, TRUE);
if ($this->parsed_response['stat'] == 'fail') {
if ($this->die_on_error) {
if ($already_tried > 3) {
die("The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}");
} else {
print("warning: (retry {$already_tried}<=3) The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}\n");
$sleep = 1 * 10**$already_tried;
print("Sleeping {$sleep} seconds\n");
sleep($sleep);
$already_tried++;
$this->request($command, $args, $already_tried);
}
} else {
$this->error_code = $this->parsed_response['code'];
$this->error_msg = $this->parsed_response['message'];
$this->parsed_response = false;
}
} else {
$this->error_code = false;
$this->error_msg = false;
}
return $response;
}
}
?>