Skip to content

Commit e4e0554

Browse files
author
Maxim Kerstens
committed
Initial commit
1 parent 5f675c6 commit e4e0554

File tree

8 files changed

+1206
-0
lines changed

8 files changed

+1206
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# UitDatabank API wrapper
2+
3+
Met deze wrapper kan je makkelijk verbinding maken met UITdatabank's API.
4+
5+
De UiTdatabank is de rijkste bron van evenementen en activiteiten in Vlaanderen en Brussel vb. films, concerten, theater, ... zie [www.uitinvlaanderen.be](http://www.uitinvlaanderen.be).
6+
UiTID is een user platform met cultuurprofielen van meer dan 70.000 cultuur- en vrijetijdszoekers.
7+
8+
Er word gebruikt gemaakt van [cultuurnet/cdb](https://github.com/cultuurnet/Cdb) om de binnenkomende data te converteren.
9+
10+
[UITdatabank](http://tools.uitdatabank.be/)
11+
12+
### Voorbeeld
13+
14+
```
15+
use HappyDemon\UitDatabank\UitDatabank;
16+
17+
$uitDatabank = new UitDatabank(
18+
env('UIT_KEY'),// Jouw UIT API key
19+
env('UIT_SECRET'), // Jouw UIT API secret
20+
env('UIT_SERVER') // UIT server (basis foor endpoints)
21+
);
22+
23+
try {
24+
$filter = new Filter();
25+
$filter->setQ('*:*');
26+
$filter->setGroup('event');
27+
$filter->setRows(300);
28+
$response = $uitDatabank->search($filter);
29+
30+
} catch (\Exception $e) {
31+
die(var_dump($e));
32+
}
33+
34+
// output
35+
var_dump($response->getEvents());
36+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "happydemon/uitdatabank",
3+
"type": "library",
4+
"description": "Een handige package om verbinding te maken met Cultuurnet's API in PHP.",
5+
"homepage": "https://github.com/happyDemon/UiTdatabank",
6+
"license": "BSD",
7+
"authors": [
8+
{
9+
"name": "Maxim Kerstens",
10+
"email": "[email protected]",
11+
"role": "Developer"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.0",
16+
"ext-curl": "*",
17+
"cultuurnet/cdb": "^2.1"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"HappyDemon\\UitDatabank\\": "src"
22+
}
23+
}
24+
}
25+

src/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace HappyDemon\UitDatabank;
4+
5+
class Exception extends \Exception {
6+
7+
}

src/OAuth/OAuth.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace HappyDemon\UitDatabank\OAuth;
4+
5+
class OAuth
6+
{
7+
/**
8+
* All OAuth 1.0 requests use the same basic algorithm for creating a
9+
* signature base string and a signature. The signature base string is
10+
* composed of the HTTP method being used, followed by an ampersand ("&")
11+
* and then the URL-encoded base URL being accessed, complete with path
12+
* (but not query parameters), followed by an ampersand ("&"). Then, you
13+
* take all query parameters and POST body parameters (when the POST body is
14+
* of the URL-encoded type, otherwise the POST body is ignored), including
15+
* the OAuth parameters necessary for negotiation with the request at hand,
16+
* and sort them in lexicographical order by first parameter name and then
17+
* parameter value (for duplicate parameters), all the while ensuring that
18+
* both the key and the value for each parameter are URL encoded in
19+
* isolation. Instead of using the equals ("=") sign to mark the key/value
20+
* relationship, you use the URL-encoded form of "%3D". Each parameter is
21+
* then joined by the URL-escaped ampersand sign, "%26".
22+
*
23+
* @param string $url The URL.
24+
* @param string $method The method to use.
25+
* @param array $parameters The parameters.
26+
* @return string
27+
*/
28+
public static function calculateBaseString($url, $method, array $parameters = null)
29+
{
30+
// redefine
31+
$url = (string) $url;
32+
$parameters = (array) $parameters;
33+
34+
// init var
35+
$chunks = array();
36+
37+
// sort parameters by key
38+
uksort($parameters, 'strcmp');
39+
40+
// loop parameters
41+
foreach ($parameters as $key => $value) {
42+
// sort by value
43+
if (is_array($value)) {
44+
$parameters[$key] = natsort($value);
45+
}
46+
}
47+
48+
// process queries
49+
foreach ($parameters as $key => $value) {
50+
// only add if not already in the url
51+
if (substr_count($url, $key . '=' . $value) == 0) {
52+
$chunks[] = self::urlencode_rfc3986($key) . '%3D' .
53+
self::urlencode_rfc3986($value);
54+
}
55+
}
56+
57+
// buils base
58+
$base = $method . '&';
59+
$base .= urlencode($url);
60+
$base .= (substr_count($url, '?')) ? '%26' : '&';
61+
$base .= implode('%26', $chunks);
62+
$base = str_replace('%3F', '&', $base);
63+
64+
// return
65+
return $base;
66+
}
67+
68+
/**
69+
* URL-encode method for internal use
70+
*
71+
* @param mixed $value The value to encode.
72+
* @return string
73+
*/
74+
private static function urlencode_rfc3986($value)
75+
{
76+
if (is_array($value)) {
77+
return array_map(array(__CLASS__, 'urlencode_rfc3986'), $value);
78+
} else {
79+
$search = array('+', ' ', '%7E', '%');
80+
$replace = array('%20', '%20', '~', '%25');
81+
82+
return str_replace($search, $replace, urlencode($value));
83+
}
84+
}
85+
86+
/**
87+
* Build the Authorization header
88+
* @later: fix me
89+
*
90+
* @param array $parameters The parameters.
91+
* @param string $url The URL.
92+
* @return string
93+
*/
94+
public static function calculateHeader(array $parameters, $url)
95+
{
96+
// redefine
97+
$url = (string) $url;
98+
99+
// divide into parts
100+
$parts = parse_url($url);
101+
102+
// init var
103+
$chunks = array();
104+
105+
// process queries
106+
foreach ($parameters as $key => $value) {
107+
$chunks[] = str_replace(
108+
'%25',
109+
'%',
110+
self::urlencode_rfc3986($key) . '="' . self::urlencode_rfc3986($value) . '"'
111+
);
112+
}
113+
114+
// build return
115+
$return = 'Authorization: OAuth realm="' . $parts['scheme'] . '://' .
116+
$parts['host'];
117+
if (isset($parts['path'])) {
118+
$return .= $parts['path'];
119+
}
120+
$return .= '", ';
121+
$return .= implode(',', $chunks);
122+
123+
// prepend name and OAuth part
124+
return $return;
125+
}
126+
127+
/**
128+
* Build the signature for the data
129+
*
130+
* @param string $key The key to use for signing.
131+
* @param string $data The data that has to be signed.
132+
* @return string
133+
*/
134+
public static function hmacsha1($key, $data)
135+
{
136+
return base64_encode(hash_hmac('SHA1', $data, $key, true));
137+
}
138+
}

0 commit comments

Comments
 (0)