-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetty_lib.php
More file actions
200 lines (163 loc) · 6.4 KB
/
getty_lib.php
File metadata and controls
200 lines (163 loc) · 6.4 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function create_session(){
// set params
$endpoint = "https://connect.gettyimages.com/v1/session/CreateSession";
$systemId = "";
$systemPassword = "";
$userName = "";
$userPassword = "";
$token = null;
// create array of data for request
$createSessionArray = array(
"RequestHeader" => array(
"Token" => "",
"CoordinationId" => ""
),
"CreateSessionRequestBody" => array(
"SystemId" => $systemId,
"SystemPassword" => $systemPassword,
"UserName" => $userName,
"UserPassword" => $userPassword
)
);
$json = json_encode($createSessionArray);
//Get data
$data = curl_me($endpoint, $json);
$success = $data->ResponseHeader->Status;
if($success == "success"){
$token[] = $data->CreateSessionResult->Token;
// or retrieve secure token
$token[] = $data->CreateSessionResult->SecureToken;
}else{
$token = false;
}
return $token;
}
/*//////////////////////////////////////////////////////////////////////////////
//
// [Searching for Images] Pass in your search terms and the unsecure token
//
/*////////////////////////////////////////////////////////////////////////////*/
function search($searchPhrase, $token){
$endpoint = 'http://connect.gettyimages.com/v1/search/SearchForImages';
$searchImagesArray = array (
"RequestHeader" => array (
"Token" => $token // Token received from a CreateSession/RenewSession API call
),
"SearchForImages2RequestBody" => array (
"Query" => array (
"SearchPhrase" => $searchPhrase
),
"Filter" => array(
"ImageFamilies" => array("creative") // specify only creative image family here
),
"ResultOptions" => array (
"IncludeKeywords" => "false",
"ItemCount" => 25, // return 25 items
"ItemStartNumber" => 1 // 1-based int, start at the first page
)
)
);
// encode to json
$json = json_encode($searchImagesArray);
$data = curl_me($endpoint, $json);
$success = $data->ResponseHeader->Status;
if($success == "success"){
//return data
return $data->SearchForImagesResult->Images;
}else if($success == "error" && $data->ResponseHeader->StatusList[0]->Code == 'AUTH-012'){
//AUTH-012 means our tokens have expired
return false;
}else{
//Otherwise this will contend the era
return $data->ResponseHeader;
}
}
/*//////////////////////////////////////////////////////////////////////////////
//
// [Requesting Download] Pass image id and the unsecure token
//
/*////////////////////////////////////////////////////////////////////////////*/
function buy_getty($id, $token){
$imageIdArray[] = array("ImageId" => $id);
// build request to get largest available download of this image
$endpoint = "http://connect.gettyimages.com/v1/download/GetLargestImageDownloadAuthorizations";
// build array to query api for images
$imageAuthorizationArray = array (
"RequestHeader" => array (
"Token" => $token
),
"GetLargestImageDownloadAuthorizationsRequestBody" => array (
"Images" => $imageIdArray
)
);
// encode
$json = json_encode($imageAuthorizationArray);
$data = curl_me($endpoint, $json);
$success = $data->ResponseHeader->Status;
if($success == "success"){
//return data
return $data->GetLargestImageDownloadAuthorizationsResult->Images[0]->Authorizations[0]->DownloadToken;
}else if($success == "error" && $data->ResponseHeader->StatusList[0]->Code == 'AUTH-012'){
return false;
}else{
return $data->ResponseHeader;
}
}
/*//////////////////////////////////////////////////////////////////////////////
//
// [Accessing Download] Pass in your SECURE token and the download token retrieved
// from download request. This request requires https
//
/*////////////////////////////////////////////////////////////////////////////*/
function get_download($sectoken, $dtoken){
// image ID for an image we know exists in the system
$imageDownloadArray = array(array("DownloadToken" => $dtoken));
// build request to get largest available download of this image
$endpoint = "https://connect.gettyimages.com/v1/download/CreateDownloadRequest";
// build array to query api for images
$imageAuthorizationArray = array (
"RequestHeader" => array (
"Token" => $sectoken
),
"CreateDownloadRequestBody" => array(
"DownloadItems" => $imageDownloadArray
)
);
// encode
$json = json_encode($imageAuthorizationArray);
$data = curl_me($endpoint, $json, true);
$success = $data->ResponseHeader->Status;
if($success == "success"){
//return data
return $data;
}else if($success == "error" && $data->ResponseHeader->StatusList[0]->Code == 'AUTH-012'){
return false;
}else{
return $data->ResponseHeader;
}
}
function curl_me($endpoint, $json, $sec = false){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//To get cURL to handle https we need some more settings
if($sec == true){
//Unsafe but quick way
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Right way
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);
$data = json_decode(curl_exec($ch));
curl_close($ch);
return $data;
}