fidor_starter_kits 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/fidor_starter_kits.rb +1 -1
  4. data/lib/fidor_starter_kits/version.rb +1 -1
  5. data/starter_kits/php_advanced/.fidor_meta.json +8 -0
  6. data/starter_kits/php_advanced/Fidor/SDK/Accounts.php +47 -0
  7. data/starter_kits/php_advanced/Fidor/SDK/Authorization.php +276 -0
  8. data/starter_kits/php_advanced/Fidor/SDK/Autoload.php +33 -0
  9. data/starter_kits/php_advanced/Fidor/SDK/Client.php +239 -0
  10. data/starter_kits/php_advanced/Fidor/SDK/Config.php +272 -0
  11. data/starter_kits/php_advanced/Fidor/SDK/Customers.php +27 -0
  12. data/starter_kits/php_advanced/Fidor/SDK/Transactions.php +47 -0
  13. data/starter_kits/php_advanced/Fidor/SDK/Transfers.php +65 -0
  14. data/starter_kits/php_advanced/Fidor/SDK/Transfers/Batch.php +18 -0
  15. data/starter_kits/php_advanced/Fidor/SDK/Transfers/Global.php +18 -0
  16. data/starter_kits/php_advanced/Fidor/SDK/Transfers/Internal.php +33 -0
  17. data/starter_kits/php_advanced/Fidor/SDK/Transfers/SEPA.php +18 -0
  18. data/starter_kits/php_advanced/Fidor/SDK/Users.php +23 -0
  19. data/starter_kits/php_advanced/README.md +19 -0
  20. data/starter_kits/php_advanced/curl-test.php +1 -0
  21. data/starter_kits/php_advanced/demo/assets/css/bootstrap-theme.css +587 -0
  22. data/starter_kits/php_advanced/demo/assets/css/bootstrap-theme.css.map +1 -0
  23. data/starter_kits/php_advanced/demo/assets/css/bootstrap-theme.min.css +6 -0
  24. data/starter_kits/php_advanced/demo/assets/css/bootstrap-theme.min.css.map +1 -0
  25. data/starter_kits/php_advanced/demo/assets/css/bootstrap.css +6760 -0
  26. data/starter_kits/php_advanced/demo/assets/css/bootstrap.css.map +1 -0
  27. data/starter_kits/php_advanced/demo/assets/css/bootstrap.min.css +6 -0
  28. data/starter_kits/php_advanced/demo/assets/css/bootstrap.min.css.map +1 -0
  29. data/starter_kits/php_advanced/demo/assets/fonts/glyphicons-halflings-regular.eot +0 -0
  30. data/starter_kits/php_advanced/demo/assets/fonts/glyphicons-halflings-regular.svg +288 -0
  31. data/starter_kits/php_advanced/demo/assets/fonts/glyphicons-halflings-regular.ttf +0 -0
  32. data/starter_kits/php_advanced/demo/assets/fonts/glyphicons-halflings-regular.woff +0 -0
  33. data/starter_kits/php_advanced/demo/assets/fonts/glyphicons-halflings-regular.woff2 +0 -0
  34. data/starter_kits/php_advanced/demo/assets/js/bootstrap.js +2363 -0
  35. data/starter_kits/php_advanced/demo/assets/js/bootstrap.min.js +7 -0
  36. data/starter_kits/php_advanced/demo/assets/js/jquery.min.js +6 -0
  37. data/starter_kits/php_advanced/demo/assets/js/npm.js +13 -0
  38. data/starter_kits/php_advanced/demo/authorize.php +103 -0
  39. data/starter_kits/php_advanced/demo/config.php +15 -0
  40. data/starter_kits/php_advanced/demo/dashboard.php +154 -0
  41. data/starter_kits/php_advanced/demo/error_auth.php +71 -0
  42. data/starter_kits/php_advanced/demo/error_auth_refresh.php +71 -0
  43. data/starter_kits/php_advanced/demo/error_token_expired.php +76 -0
  44. data/starter_kits/php_advanced/demo/get_accounts.php +152 -0
  45. data/starter_kits/php_advanced/demo/get_customers.php +130 -0
  46. data/starter_kits/php_advanced/demo/get_transactions.php +146 -0
  47. data/starter_kits/php_advanced/demo/index.php +84 -0
  48. data/starter_kits/php_advanced/demo/refresh_token.php +39 -0
  49. data/starter_kits/php_advanced/demo/revoke_access.php +38 -0
  50. data/starter_kits/php_advanced/demo/setup.php +14 -0
  51. data/starter_kits/php_advanced/demo/transfer_money.php +241 -0
  52. data/starter_kits/php_advanced/demo/transfer_money_results.php +126 -0
  53. metadata +50 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe72996845c734b423e4c9571e3ea15427168a27
4
- data.tar.gz: 311aa52916b738f63844d0e5eb902c752282f2df
3
+ metadata.gz: c40f1a70c94e4892f40607a57997d6d8f3a3f684
4
+ data.tar.gz: 25a939d8c4eabb519dc5d27dccf031adfeef229f
5
5
  SHA512:
6
- metadata.gz: 0892a53df4347cc65ddf716e80039413f81dd49fe8065d831bf795344618b7f43d607354c69154a72c08ad3c5b190bbcd9182d078c0b310f92c95ea82491be48
7
- data.tar.gz: 3c6673db52300cd822794a9aab7dc29948e9a7dac46638d763eed429e04113dfa9bfa4f02c0d956deeb93aaa4dc56f4cd7d6f9eb6ae4af4eed8c2ccbe1a917ae
6
+ metadata.gz: 3c055c409b78ac09c41a3305f37f80678ced864462ab2e9e30dd8620e2e98ed4b38aac02d0c70e19ed1c1f515461f14e2ea4f7e9c50a06a481f8c556c8b52f9d
7
+ data.tar.gz: f21988b94f1a1f24b905a07059febdebf3248efff20acb35bad57aec7c6dd3526d1e01c2b4413740475cafabc824457ad9fed02828b67949d889e7a3026dc5a9
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Changelog Fidor Admin API Schema
2
2
  See [commit messages](https://github.com/fidor/fidor_starter_kits/commits/) for details.
3
3
 
4
+ ##2016-04
5
+
6
+ * add PHP advanced example
7
+
4
8
  ##2016-01
5
9
 
6
10
  * remove client_secret usage in params => moved to BasicAuth header
@@ -6,7 +6,7 @@ require 'json'
6
6
 
7
7
  module FidorStarterKits
8
8
 
9
- STARTER_KITS = %w{ node_tx golang_transactions php_oauth_plain ruby_oauth_plain java_servlet }
9
+ STARTER_KITS = %w{ node_tx golang_transactions php_oauth_plain ruby_oauth_plain java_servlet php_advanced }
10
10
 
11
11
  class << self
12
12
 
@@ -1,3 +1,3 @@
1
1
  module FidorStarterKits
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -0,0 +1,8 @@
1
+ {
2
+ "display_name" : "PHP Advanced",
3
+ "description" : "PHP Website using multiple API objects and a tiny PHP SDK",
4
+ "app_name":"php_advanced",
5
+ "app_url":"http://localhost:8000",
6
+ "callback_urls":"http://localhost:8000",
7
+ "language" : "php"
8
+ }
@@ -0,0 +1,47 @@
1
+ <?php
2
+ namespace Fidor\SDK;
3
+
4
+ /**
5
+ * @property Transactions $transactions Get transactions resource
6
+ * @property Transactions $transactions Get transfer resource
7
+ */
8
+ class Accounts extends Client {
9
+
10
+ /**
11
+ *
12
+ * @var string
13
+ */
14
+ protected $id;
15
+
16
+ /**
17
+ *
18
+ * @param \Fidor\SDK\Config $config
19
+ */
20
+ public function __construct( Config $config ) {
21
+ parent::__construct( $config );
22
+ }
23
+
24
+ /**
25
+ *
26
+ * @param string $name
27
+ * @return mixed
28
+ */
29
+ public function __get( $name ) {
30
+ $obj = parent::__get( $name );
31
+ return $obj;
32
+ }
33
+
34
+ /**
35
+ *
36
+ * @param integer $id
37
+ * @return string
38
+ */
39
+ public function get( $id = null ) {
40
+ $data = parent::get( $this->config->getApiUrl() . '/accounts' );
41
+ if ( ! empty( $data['data'] ) ) {
42
+ $data = $data['data'];
43
+ }
44
+ return $data;
45
+ }
46
+
47
+ }
@@ -0,0 +1,276 @@
1
+ <?php
2
+ namespace Fidor\SDK;
3
+
4
+ class Authorization {
5
+
6
+ /**
7
+ * Configuration
8
+ *
9
+ * @var \Fidor\SDK\Config
10
+ */
11
+ protected $config;
12
+
13
+ /**
14
+ * User agent used in HTTP requests
15
+ *
16
+ * @var string
17
+ */
18
+ protected $user_agent = 'FidorSDK (+https://docs.fidor.de)';
19
+
20
+ /**
21
+ *
22
+ * @var integer
23
+ */
24
+ protected $connect_timeout = 15;
25
+
26
+ /**
27
+ *
28
+ * @var integer
29
+ */
30
+ protected $timeout = 60;
31
+
32
+ /**
33
+ * Stores last request log
34
+ *
35
+ * @var string
36
+ */
37
+ protected static $debug_log;
38
+
39
+ /**
40
+ *
41
+ * @var boolean
42
+ */
43
+ protected $debug_enabled = false;
44
+
45
+ /**
46
+ *
47
+ * @param \Fidor\SDK\Config $config
48
+ * @param string $redirect_uri
49
+ */
50
+ public function __construct( Config $config ) {
51
+ $this->config = $config;
52
+ }
53
+
54
+ /**
55
+ * Turn debugging on
56
+ *
57
+ * @return \Fidor\SDK\Authorization
58
+ */
59
+ public function enableDebug( ) {
60
+ $this->debug_enabled = true;
61
+ return $this;
62
+ }
63
+
64
+ /**
65
+ * Turn debugging off
66
+ *
67
+ * @return \Fidor\SDK\Authorization
68
+ */
69
+ public function disableDebug( ) {
70
+ $this->debug_enabled = false;
71
+ return $this;
72
+ }
73
+
74
+ /**
75
+ * Defines the HTTP user agent string used in requests
76
+ *
77
+ * @param string $userAgent
78
+ * @return \Fidor\SDK\Authorization
79
+ */
80
+ public function setUserAgent( $userAgent ) {
81
+ $this->user_agent = $userAgent;
82
+ return $this;
83
+ }
84
+
85
+ /**
86
+ * Sets connection timeout
87
+ *
88
+ * @param integer $connectTimeout
89
+ * @return \Fidor\SDK\Authorization
90
+ */
91
+ public function setConnectTimeout( $connectTimeout ) {
92
+ $this->connect_timeout = $connectTimeout;
93
+ return $this;
94
+ }
95
+
96
+ /**
97
+ *
98
+ * @param integer $timeout
99
+ * @return \Fidor\SDK\Authorization
100
+ */
101
+ public function setTimeout( $timeout ) {
102
+ $this->timeout = (int) $timeout;
103
+ return $this;
104
+ }
105
+
106
+ /**
107
+ *
108
+ * @return string
109
+ */
110
+ public function getRequestUrl( $redirect_uri = null, $state = null ) {
111
+ return sprintf( '%s/authorize?response_type=code&client_id=%s&state=%s&redirect_uri=%s', $this->config->getOAuthUrl(), $this->config->getClientId(), $state, urlencode( $redirect_uri ) );
112
+ }
113
+
114
+ /**
115
+ *
116
+ * @return string
117
+ */
118
+ public function getTokenUrl( ) {
119
+ return $this->config->getOAuthUrl() . '/token';
120
+ }
121
+
122
+ /**
123
+ *
124
+ * @return string
125
+ */
126
+ public function getRevokeTokenUrl( ) {
127
+ return $this->config->getOAuthUrl() . '/revoke';
128
+ }
129
+
130
+ /**
131
+ * Redirect user browser to authorization server.
132
+ *
133
+ * @param string $state
134
+ */
135
+ public function start( $state = null ) {
136
+ $state = $state ? $state : self::makeState();
137
+ $auth_url = $this->getRequestUrl( $this->config->getCallbackUrl(), $state );
138
+ header( 'Location: ' . $auth_url );
139
+ exit;
140
+ }
141
+
142
+ /**
143
+ * Request access token after authorization was grantd by resource owner
144
+ *
145
+ * @param string $code
146
+ * @return string
147
+ */
148
+ public function finish( $code ) {
149
+ $json = $this->http_post(
150
+ $this->getTokenUrl(),
151
+ sprintf( 'client_id=%s&client_secret=%s&code=%s&redirect_uri=%s&grant_type=authorization_code', $this->config->getClientId(), $this->config->getClientSecret(), $code, urlencode( $this->config->getCallbackUrl() ) ),
152
+ array( 'auth' => true, 'auth_scheme' => 'Bearer', )
153
+ );
154
+
155
+ return json_decode( $json, true );
156
+ }
157
+
158
+ /**
159
+ * Refresh access token
160
+ *
161
+ * @param string $refresh_token
162
+ * @return string
163
+ */
164
+ public function refresh( $refresh_token = null ) {
165
+ $json = $this->http_post(
166
+ $this->getTokenUrl(), sprintf( 'grant_type=refresh_token&refresh_token=%s', $refresh_token ? $refresh_token : $this->config->getRefreshToken() ),
167
+ array( 'auth' => true, )
168
+ );
169
+
170
+ return json_decode( $json, true );
171
+ }
172
+
173
+ /**
174
+ * Revoke both access and refresh tokens. By default, it revokes access token.
175
+ *
176
+ * @param string|null $token
177
+ * @return string
178
+ */
179
+ public function revoke( $token = null ) {
180
+ $json = $this->http_post(
181
+ $this->getRevokeTokenUrl(),
182
+ sprintf( 'token=%s', $token ? $token : $this->config->getAccessToken() ),
183
+ array( 'auth' => true, )
184
+ );
185
+
186
+ return json_decode( $json, true );
187
+ }
188
+
189
+ protected function http_post( $url, $data, $options = array() ) {
190
+ if ( function_exists( 'curl_init' ) ) {
191
+ $headers = array( 'Content-Type: application/x-www-form-urlencoded' );
192
+
193
+ $ch = curl_init( $url );
194
+
195
+ if ( array_key_exists( 'auth', $options ) && true === $options['auth'] ) {
196
+ $scheme = ! empty( $options['auth_scheme'] ) ? $options['auth_scheme'] : 'Basic';
197
+ $token = base64_encode( $this->config->getClientId() . ":" . $this->config->getClientSecret() );
198
+ $headers[] = sprintf( 'Authorization: %s %s', $scheme, $token );
199
+ }
200
+
201
+ curl_setopt( $ch, CURLOPT_HEADER, false );
202
+ curl_setopt( $ch, CURLOPT_POST, true );
203
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
204
+
205
+ if ( preg_match( '/^https:/is', trim( $url ) ) ) {
206
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
207
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
208
+ }
209
+
210
+ /**
211
+ * In case we're debugging
212
+ */
213
+ $fh = null;
214
+ if ( $this->debug_enabled ) {
215
+ curl_setopt( $ch, CURLOPT_VERBOSE, true );
216
+ $fh = fopen( 'php://temp', 'w+' );
217
+ curl_setopt( $ch, CURLOPT_STDERR, $fh );
218
+ }
219
+
220
+ curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout );
221
+ curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout );
222
+ curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
223
+ curl_setopt( $ch, CURLOPT_USERAGENT, $this->user_agent );
224
+ curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
225
+
226
+ $resp = curl_exec( $ch );
227
+
228
+ /**
229
+ * Debugging was enabled
230
+ */
231
+ if ( $this->debug_enabled ) {
232
+ rewind( $fh );
233
+ self::$debug_log = ( $data ? "sent data: \n" . ( is_array( $data ) ? print_r( $data, true ) : $data ) : '' ) . "\n\nHTTP logs:\n" . stream_get_contents( $fh );
234
+ }
235
+
236
+ curl_close( $ch );
237
+
238
+ return $resp;
239
+ } else {
240
+ $headers = "Content-type: application/x-www-form-urlencoded\r\n";
241
+
242
+ if ( array_key_exists( 'auth', $options ) && true === $options['auth'] ) {
243
+ $scheme = ! empty( $options['auth_scheme'] ) ? $options['auth_scheme'] : 'Basic';
244
+ $token = base64_encode( $this->config->getClientId() . ":" . $this->config->getClientSecret() );
245
+ $headers .= sprintf( "Authorization: %s %s\r\n", $scheme, $token );
246
+ }
247
+
248
+ $options = array(
249
+ 'http' => array(
250
+ 'header' => $headers,
251
+ 'method' => 'POST',
252
+ 'content' => $data,
253
+ ),
254
+ );
255
+ $context = stream_context_create( $options );
256
+ return file_get_contents( $url, false, $context );
257
+ }
258
+ }
259
+
260
+ /**
261
+ *
262
+ * @return string
263
+ */
264
+ public static function getDebugLog( ) {
265
+ return self::$debug_log;
266
+ }
267
+
268
+ /**
269
+ * Generate a random string for use as state parameter
270
+ *
271
+ * @return string
272
+ */
273
+ public static function makeState( ) {
274
+ return md5( time() . rand( 1000000, 9999999 ) );
275
+ }
276
+ }
@@ -0,0 +1,33 @@
1
+ <?php
2
+ namespace Fidor\SDK;
3
+
4
+ class Autoload {
5
+
6
+ /**
7
+ * Register class autoloader
8
+ */
9
+ public static function register() {
10
+ spl_autoload_register( array( __CLASS__, 'load' ) );
11
+ }
12
+
13
+ /**
14
+ * Load class
15
+ *
16
+ * @param string $class
17
+ */
18
+ public static function load( $class ) {
19
+ // load only Fidor classes
20
+ if ( 'Fidor\\' === substr( $class, 0, 6 ) ) {
21
+ // turn namespace separator and _ to directory separtor
22
+ $file_path = __DIR__ . '/' . preg_replace( '/[\\_]/', DIRECTORY_SEPARATOR, substr( $class, strrpos( $class, '\\' ) + 1 ) ) . '.php';
23
+
24
+ // load class file if exists
25
+ if ( is_readable( $file_path ) ) {
26
+ require_once $file_path;
27
+ }
28
+ }
29
+ }
30
+
31
+ }
32
+
33
+ Autoload::register();
@@ -0,0 +1,239 @@
1
+ <?php
2
+ namespace Fidor\SDK;
3
+
4
+ /**
5
+ * @property \Fidor\SDK\Users $users Get users resource
6
+ * @property \Fidor\SDK\Accounts $accounts Get accounts resource
7
+ * @property \Fidor\SDK\Customers $customers Get customers resource
8
+ * @property \Fidor\SDK\Transactions $transactions Get transactions resource
9
+ * @property \Fidor\SDK\Transfers $transfers Get transfers resource
10
+ *
11
+ */
12
+ class Client {
13
+
14
+ /**
15
+ *
16
+ * @var Config
17
+ */
18
+ protected $config;
19
+
20
+ /**
21
+ *
22
+ * @var integer
23
+ */
24
+ protected $connect_timeout = 15;
25
+
26
+ /**
27
+ *
28
+ * @var integer
29
+ */
30
+ protected $timeout = 60;
31
+
32
+ /**
33
+ *
34
+ * @var string
35
+ */
36
+ protected $user_agent = 'FidorSDK (+https://docs.fidor.de)';
37
+
38
+ /**
39
+ *
40
+ * @var string
41
+ */
42
+ protected static $debug_log;
43
+
44
+ /**
45
+ *
46
+ * @param \Fidor\SDK\Config $config
47
+ */
48
+ public function __construct( Config $config ) {
49
+ $this->config = $config;
50
+ }
51
+
52
+ /**
53
+ *
54
+ * @param string $name
55
+ * @return mixed
56
+ */
57
+ public function __get( $name ) {
58
+ $class = __NAMESPACE__ . '\\' . ucfirst( $name );
59
+ if ( class_exists( $class ) && is_subclass_of( $class, __CLASS__ ) ) {
60
+ $obj = new $class( $this->config );
61
+ return $obj;
62
+ }
63
+ }
64
+
65
+ /**
66
+ *
67
+ * @param string $url
68
+ * @return string
69
+ */
70
+ public function get( $url ) {
71
+ return $this->request( 'get', $url );
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @param string $url
77
+ * @param string|array $data
78
+ * @return string
79
+ */
80
+ public function post( $url, $data ) {
81
+ return $this->request( 'post', $url, $data );
82
+ }
83
+
84
+ /**
85
+ *
86
+ * @param string $url
87
+ * @param string|array $data
88
+ * @return string
89
+ */
90
+ public function put( $url, $data ) {
91
+ return $this->request( 'put', $url, $data );
92
+ }
93
+
94
+ /**
95
+ *
96
+ * @param string $url
97
+ * @param string|array $data
98
+ * @return string
99
+ */
100
+ public function patch( $url, $data ) {
101
+ return $this->request( 'patch', $url, $data );
102
+ }
103
+
104
+ /**
105
+ *
106
+ * @param string $url
107
+ * @return string
108
+ */
109
+ public function delete( $url ) {
110
+ return $this->request( 'put', $url );
111
+ }
112
+
113
+ /**
114
+ *
115
+ * @param string $method
116
+ * @param string $url
117
+ * @param string|null $data
118
+ */
119
+ public function request( $method, $url, $data = null ) {
120
+ if ( function_exists( 'curl_init' ) ) {
121
+ return $this->curl_request( $method, $url, $data );
122
+ } else {
123
+ return $this->stream_request( $method, $url, $data );
124
+ }
125
+ }
126
+
127
+ /**
128
+ *
129
+ * @param string $method
130
+ * @param string $url
131
+ * @param mixed $data
132
+ * @return string
133
+ */
134
+ protected function curl_request( $method, $url, $data = null ) {
135
+ $method = trim( strtoupper( $method ) );
136
+
137
+ /**
138
+ * The empty Expect headers removes HTTP 100-continue mechanism
139
+ */
140
+ $headers = array(
141
+ 'Accept: application/vnd.fidor.de; version=1,text/json',
142
+ 'Authorization: Bearer ' . $this->config->getAccessToken(),
143
+ 'Expect: ',
144
+ );
145
+
146
+ $ch = curl_init( $url );
147
+ curl_setopt( $ch, CURLOPT_HEADER, false );
148
+
149
+ if ( 'POST' === $method ) {
150
+ curl_setopt( $ch, CURLOPT_POST, true );
151
+ } elseif ( in_array( $method, array( 'HEAD', 'PUT', 'PATCH', 'DELETE' ) ) ) {
152
+ curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );
153
+ }
154
+
155
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
156
+
157
+ if ( preg_match( '/^https:/is', trim( $this->uri ) ) ) {
158
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
159
+ curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
160
+ }
161
+
162
+ // debugging
163
+ curl_setopt( $ch, CURLOPT_VERBOSE, true );
164
+ $fh = fopen( 'php://temp', 'w+' );
165
+ curl_setopt( $ch, CURLOPT_STDERR, $fh );
166
+ // -debugging
167
+
168
+ curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout );
169
+ curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout );
170
+
171
+ if ( ! empty( $data ) ) {
172
+ if ( is_array( $data ) ) {
173
+ $data = json_encode( $data );
174
+ }
175
+ if ( in_array( $method, array( 'PUT', 'PATCH', 'DELETE' ) ) ) {
176
+ curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $data );
177
+ } else {
178
+ curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
179
+ }
180
+
181
+ $headers[] = 'Content-Type: application/json';
182
+ }
183
+
184
+ if ( $this->user_agent ) {
185
+ curl_setopt( $ch, CURLOPT_USERAGENT, $this->user_agent );
186
+ }
187
+
188
+ curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
189
+
190
+ $result = curl_exec( $ch );
191
+
192
+ $request_info = curl_getinfo( $ch );
193
+
194
+ rewind( $fh );
195
+ self::$debug_log = ( $data ? "SENDING DATA: \n" . ( is_array( $data ) ? print_r( $data, true ) : $data ) . "\n\n" : '' ) . stream_get_contents( $fh ) . "\n\nRESPONSE:\n" . $result;
196
+
197
+ curl_close( $ch );
198
+
199
+ return json_decode( $result, true );
200
+ }
201
+
202
+ /**
203
+ *
204
+ * @param string $method
205
+ * @param string $url
206
+ * @param mixed $data
207
+ * @return string
208
+ */
209
+ protected function stream_request( $method, $url, $data = null ) {
210
+ $headers = "Accept: application/vnd.fidor.de; version=1,text/json\r\n";
211
+ $headers .= "Authorization: Bearer " . $this->config->getAccessToken() . "\r\n";
212
+ $headers .= "Expect: \r\n";
213
+
214
+ $options = array();
215
+ $options['http']['method'] = $method;
216
+
217
+ if ( ! empty( $data ) ) {
218
+ if ( is_array( $data ) ) {
219
+ $data = json_encode( $data );
220
+ }
221
+ $headers .= "Content-Type: application/json\r\n";
222
+ $options['http']['content'] = $data;
223
+ }
224
+
225
+ $options['http']['header'] = $headers;
226
+
227
+ $context = stream_context_create( $options );
228
+ return file_get_contents( $url, false, $context );
229
+ }
230
+
231
+ /**
232
+ *
233
+ * @return string
234
+ */
235
+ public static function get_debug_log( ) {
236
+ return self::$debug_log;
237
+ }
238
+
239
+ }