forked from philprobinson84/Wunderlist2-PHP-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.class.php
131 lines (109 loc) · 3.41 KB
/
base.class.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
<?php
/**
* Wrapper for Wunderlist2 API
* api.class.php
* Purpose: communicate with Wunderlist2 API
*
* @author Joshua de Gier
* @version 1.01 18/07/2013
*/
class Wunderbase
{
protected function call($action, $method, $data)
{
// Check action parameter
if( $action == "" )
{
throw new Exception( "No API action given", 0001 );
}
// Check method parameter
if( $method == "" )
{
throw new Exception( "No API method given", 0002 );
}
// Expected response is 200 OK
$expectedResponse = 200;
// Start with an empty set of headers
$headers = array();
// Init cURL
$ch = curl_init($this->api_url.$action);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
// Pass data?
if( is_array($data) && count($data) > 0 )
{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
}
// Set request type for POST
if( strtolower($method) == 'post' )
{
if( $action != "/login" )
{
// For post-request the expected response is 201 Created
$expectedResponse = 201;
}
curl_setopt($ch, CURLOPT_POST, true );
}
// Set request type for PUT
if( strtolower($method) == 'put' )
{
// Set custom request to put
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
// Data needs to be send as json
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
// Set header content-type to application/json
$headers[] = 'content-type: application/json';
// Set header Content-Lenght to the json encoded length
$headers[] = 'Content-Length: '.strlen(json_encode($data));
}
// Set request type for DELETE
if( strtolower($method) == 'delete' )
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
// Send authtoken if set
if( $this->authtoken != false )
{
$headers[] = 'authorization: Bearer '.$this->authtoken;
}
// Files
if( strpos($action, "/files") )
{
$headers[] = "Accept: application/json";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: nl,en-us;q=0.7,en,q=0.3";
$headers[] = "Content-Type: application/json; charset=utf-8";
$headers[] = "Host: files.wunderlist.com";
$headers[] = "Origin: https://www.wunderlist.com";
$headers[] = "Referer: https://www.wunderlist.com";
$headers[] = "X-6W-Platform: web";
$headers[] = "X-6W-Product: wunderlist";
$headers[] = "X-6W-System: MacIntel";
}
// Send headers with the request
if( count($headers) > 0 )
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$output = curl_exec($ch);
// Get HTTP response
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close CURL
curl_close($ch);
// get / put / delete requests should have HTTP Code 200 OK
// only exception is the login method, which returns HTTP Code 200 OK
if($httpCode == 200 && (strtolower($method) != 'post' || $action == '/login'))
{
return json_decode($output, true);
}
// all non-login post requests should have HTTP Code 201 Created
elseif($httpCode == 201 && strtolower($method) == 'post')
{
return json_decode($output, true);
}
// If the HTTP code did not match, than the request failed
else
{
throw new Exception( "API Call failed - Method: $method - Action: $action - HTTP Response: $httpCode (Expected $expectedResponse)", 0000 );
}
}
}