-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_api.php
183 lines (137 loc) · 5.26 KB
/
ch_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
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
<?php
/*
chphp - a PHP interface to Cooper-Hewitt API
http://github.com/pidg/chphp/
http://twitter.com/tarasyoung
Gives you the following functions (all return an array, see
individual functions for detailed instructions):
ch_request (args) Directly request info from the API
ch_get (args) Send a GET url-style request
ch_search (query) Search for things
ch_object (info_type, object) Request info on an object
ch_list (info_type) Request a list of things of type info_type
ch_info (info_type, id) Request info on a thing of info_type
Available info_types are:
| ch_object ch_list ch_info |
------------+--------------------------------------+-------------
info | x | info
image | x | image
person | x x | person
exhibition | x x x | exhibition
department | x x | department
period | x x | period
role | x x | role
type | x x | type
object | x | object
------------+--------------------------------------+-------------
/!\ This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
// Stick your access token in here:
$mytoken = "replacemewithatoken";
// Define constants (such that it doesn't matter if you prefer singular)
define("department",0,1); define("departments",0,1);
define("exhibition",1,1); define("exhibitions",1,1);
define("person",2,1); define("people",2,1);
define("period",3,1); define("periods",3,1);
define("role",4,1); define("roles",4,1);
define("type",5,1); define("types",5,1);
define("object",6,1); define("objects",5,1);
define("image",6,1); define("images",5,1);
define("info",7,1);
// (I realise how awful this is, but bear with me)
function ch_request($args)
{
/*
Asks Cooper-Hewitt API for stuff and spits it back as an array
example: $a = ch_request( array("method" => "cooperhewitt.search.collection", "query" => "sampler") );
*/
global $mytoken; $args["access_token"] = $mytoken;
// Perform query:
$query = curl_init();
curl_setopt($query, CURLOPT_URL, "https://api.collection.cooperhewitt.org/rest/");
curl_setopt($query, CURLOPT_POST, 1);
curl_setopt($query, CURLOPT_POSTFIELDS, $args);
curl_setopt($query, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($query);
curl_close($query);
return json_decode($response,1);
}
function ch_get($args)
{
/*
Lets you format your request like a GET request
example: $a = ch_get("method=cooperhewitt.search.collection&query=sampler");
*/
// Split arguments up and shove them in $data:
$p = explode("&",$args);
for ( $n=0; $n < count($p); $n++ )
{
$e = explode("=", $p[$n]);
$data[$e[0]] = $e[1];
}
return ch_request($data);
}
function ch_search($query)
{
/*
Searches the collection
example: $a = ch_search("sampler");
*/
return ch_get("method=cooperhewitt.search.collection&query=$query");
}
function determine_requested_type($inputtype)
{
switch ($inputtype)
{
case department: $returntype = "departments"; break;
case exhibition: $returntype = "exhibitions"; break;
case periods: $returntype = "periods"; break;
case person: $returntype = "people"; break;
case role: $returntype = "roles"; break;
case type: $returntype = "types"; break;
default: $returntype = "departments";
}
return $returntype;
}
function ch_object($type, $object)
{
/*
Returns a list of things related to an object
example: $a = ch_object(images, 18766551)
Possibilities: info, images, people, exhibitions
*/
switch ( $type )
{
case info: $what = "Info"; break;
case people: $what = "Participants"; break;
case images: $what = "Images"; break;
case exhibitions: $what = "Exhibitions"; break;
default: $what="Info";
}
return ch_get("method=cooperhewitt.objects.get$what&id=$object");
}
function ch_list($what)
{
/*
Returns a list of things
example: $a = ch_list(roles);
Possibilities: departments, exhibitions, periods, roles, types
*/
$datatype = determine_requested_type($what);
return ch_get("method=cooperhewitt.$datatype.getList");
}
function ch_info($what, $id)
{
/*
Returns info about someone or something
example: $a = ch_info(role, 35236657);
Possibilities: object, department, exhibition, period,
person, role, type
*/
$datatype = determine_requested_type($what);
return ch_get("method=cooperhewitt.$datatype.getInfo&id=$id");
}