-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocrata.php
More file actions
141 lines (136 loc) · 5.26 KB
/
socrata.php
File metadata and controls
141 lines (136 loc) · 5.26 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
<?php
class Socrata {
// The base URL for this Socrata API, ex: http://data.medicare.gov or http://www.socrata.com
private $root_url = "https://opendata.socrata.com";
// App Token
private $app_token = "";
// Username and password, used for authenticated requests
private $user_name = "";
private $password = "";
// Basic constructor
public function __construct($root_url = "", $app_token = "", $user_name = "", $password = "") {
$this->root_url = $root_url;
$this->app_token = $app_token;
$this->user_name = $user_name;
$this->password = $password;
return true;
}
// create query URL based on the root URL, path, and parameters
public function create_query_url($path, $params = array()) {
// The full URL for this resource is the root + the path
$full_url = $this->root_url . $path;
// Build up our array of parameters
$parameters = array();
foreach($params as $key => $value) {
array_push($parameters, urlencode($key) . "=" . urlencode($value));
}
if(count($parameters) > 0) {
$full_url .= "?" . implode("&", $parameters);
}
return $full_url;
}
// create cURL handle, which can then be submitted via get
public function create_curl_handle($path, $params = array()) {
// The full URL for this resource is the root + the path
$full_url = $this->create_query_url($path, $params);
// Build up the headers we'll need to pass
$headers = array(
'Accept: application/json',
'Content-type: application/json',
"X-App-Token: " . $this->app_token
);
// Time for some cURL magic...
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $full_url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
// Set up request, and auth, if configured
if($this->user_name != "" && $this->password != "") {
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password);
}
return $handle;
}
// Convenience function for GET calls
public function get($path, $params = array()) {
$handle = $this->create_curl_handle($path, $params);
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($code != "200") {
echo "Error \"$code\" from server: $response";
die();
}
return json_decode($response, true);
}
// Convenience function for Posts
public function post($path, $json_filter) {
// The full URL for this resource is the root + the path
$full_url = $this->root_url . $path;
// Build up the headers we'll need to pass
$headers = array(
'Accept: application/json',
'Content-type: application/json',
"X-App-Token: " . $this->app_token
);
// Time for some cURL magic...
$handle = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_URL, $full_url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
// Set up request, and auth, if configured
if($this->user_name != "" && $this->password != "") {
curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password);
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($code != "200") {
echo "Error \"$code\" from server: $response";
die();
}
return json_decode($response, true);
}
// Convenience function for Puts
public function put($path, $json_filter) {
// The full URL for this resource is the root + the path
$full_url = $this->root_url . $path;
// Build up the headers we'll need to pass
$headers = array(
'Accept: application/json',
'Content-type: application/json',
"X-App-Token: " . $this->app_token
);
// Time for some cURL magic...
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $full_url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json_filter);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT");
// Set up request, and auth, if configured
if($this->user_name != "" && $this->password != "") {
curl_setopt($handle, CURLOPT_USERPWD, $this->user_name . ":" . $this->password);
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($code != "200") {
echo "Error \"$code\" from server: $response";
die();
}
return json_decode($response, true);
}
}
// Convenience functions
function array_get($needle, $haystack) {
return (in_array($needle, array_keys($haystack)) ? $haystack[$needle] : NULL);
}
function pre_dump($var) {
echo "<pre>" . print_r($var) . "</pre>";
}
?>