forked from mradionov/phonegap-build-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhonegapBuildApi.php
909 lines (812 loc) · 26.9 KB
/
PhonegapBuildApi.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php
/**
* PHP library to interact with Phonegap Build API
*
* The latest version of Phonegap for the time when library was developed 3.1.0
* Anyways, it has to work with the latest versions of Cordova (Phonegap) 5+.
* Contains all methods the a presented in this version of API
*
* Requires CURL PHP extension to be installed and enabled
*
* Original API documentation:
* @link http://docs.build.phonegap.com/en_US/developer_api_api.md.html
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html
*
* @author Michael Radionov <https://github.com/mradionov>
* @link https://github.com/mradionov/phonegap-build-api
* @license MIT
*/
class PhonegapBuildApi
{
const IOS = 'ios';
const ANDROID = 'android';
const ROLE_TESTER = 'tester'; // read-only
const ROLE_DEV = 'dev'; // read and write
/**
* Api Endpoint
*
* @var string
*/
protected $endpoint = 'https://build.phonegap.com/api/v1/';
/**
* Available methods
*
* @var array
*/
protected $methods = array('get', 'post', 'put', 'delete');
/**
* Successful HTTP codes
*
* @var array
*/
protected $codes = array(200, 201, 202, 302);
/**
* Username
*
* May stay empty if using simple token or access token
*
* @var string
*/
protected $username = '';
/**
* Password
*
* May stay empty if using simple token or access token
*
* @var string
*/
protected $password = '';
/**
* Simple authentication token, taken from PG account
*
* May stay empty if using username + password or access token
*
* @var string
*/
protected $token = '';
/**
* OAuth2 access token
*
* May stay empty if using username + password or simple token
*
* @var string
*/
protected $accessToken = '';
/**
* Check whether a request was successful
*
* @var bool
*/
protected $success = false;
/**
* Error string
*
* Stores error for request if request failed
*
* @var string
*/
protected $error = '';
/**
* Create instance
*
* If using username and password, pass both values to constructor;
* otherwise, if using token, - pass single token value, leave second one empty
*
* @param string $usernameOrToken - username of token
* @param string $password - password, if username passed as first arg
*/
public function __construct($usernameOrToken = '', $password = '')
{
if (! function_exists('curl_init')) {
trigger_error('PhonegapBuildApi library requires CURL module to be enabled');
}
if (! empty($usernameOrToken)) {
if (! empty($password)) {
$this->setCredentials($usernameOrToken, $password);
} else {
$this->setToken($usernameOrToken);
}
}
}
/**
* Create new instance statically
*
* @param string $usernameOrToken - username of token
* @param string $password - password, if username passed as first arg
*
* @return PhonegapBuildApi
*/
public static function factory($usernameOrToken = null, $password = null)
{
return new self($usernameOrToken, $password);
}
/**
* Set username and password for authentication
*
* Tokens will be cleared
*
* @param string $username
* @param string $password
*
* @return PhonegapBuildApi
*/
public function setCredentials($username, $password)
{
$this->username = $username;
$this->password = $password;
$this->token = '';
$this->accessToken = '';
return $this;
}
/**
* Set simple token for authentication
*
* Username + password and access token will be cleared
*
* @param string $token
*
* @return PhonegapBuildApi
*/
public function setToken($token)
{
$this->token = $token;
$this->username = '';
$this->password = '';
$this->accessToken = '';
return $this;
}
/**
* Set access token for OAuth2 authentication
*
* Username + password and simple token will be cleared
*
* @param string $accessToken
*
* @return PhonegapBuildApi
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
$this->username = '';
$this->password = '';
$this->token = '';
return $this;
}
// ------------------------------------
// Read API methods
// ------------------------------------
/**
* Get user's profile information
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_me
*
* @return mixed: array on success | false on fail
*/
public function getProfile()
{
return $this->request('me');
}
/**
* Get user's applications
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_apps
*
* @return mixed: array on success | false on fail
*/
public function getApplications()
{
return $this->request('apps');
}
/**
* Get user's application by application id
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_apps_id
*
* @param mixed: int | string $applicationId
*
* @return mixed: array on success | false on fail
*/
public function getApplication($applicationId)
{
return $this->request(array('apps', $applicationId));
}
/**
* Get user's application icon url by application id
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_apps_id_icon
*
* @param mixed: int | string $applicationId
*
* @return mixed: array on success | false on fail
*/
public function getApplicationIcon($applicationId)
{
return $this->request(array('apps', $applicationId, 'icon'));
}
/**
* Get user's application download url for platform by application id
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_apps_id_platform
*
* @param mixed: int | string $applicationId
* @param string $platform - platform name ('android', 'ios', ...')
*
* @return mixed: array on success | false on fail
*/
public function downloadApplicationPlatform($applicationId, $platform)
{
return $this->request(array('apps', $applicationId, $platform));
}
/**
* Get user's keys
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_keys
*
* @return mixed: array on success | false on fail
*/
public function getKeys()
{
return $this->request('keys');
}
/**
* Get user's keys for platform
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_keys_platform
*
* @param string $platform - platform name ('android', 'ios', ...')
*
* @return mixed: array on success | false on fail
*/
public function getKeysPlatform($platform)
{
return $this->request(array('keys', $platform));
}
/**
* Get user's key for platform by key id
*
* @link http://docs.build.phonegap.com/en_US/developer_api_read.md.html#_get_https_build_phonegap_com_api_v1_keys_platform_id
*
* @param string $platform - platform name ('android', 'ios', ...')
* @param mixed: int | string $keyId
*
* @return mixed: array on success | false on fail
*/
public function getKeyPlatform($platform, $keyId)
{
return $this->request(array('keys', $platform, $keyId));
}
// ------------------------------------
// Write API methods
// ------------------------------------
/**
* Create application
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function createApplication($options = array())
{
// duplicating the default values from API documentation
$defaults = array(
'title' => 'Phonegap Application',
'package' => 'com.phonegap.www',
'version' => '0.0.1',
'description' => '',
'debug' => false,
// don't set defaults for keys, as it will throw an error
// if you want to set keys just pass them as options
// 'keys' => array(),
'private' => true,
'phonegap_version' => '3.1.0',
'hydrates' => false,
);
$options = array_merge($defaults, $options);
return $this->request('apps', 'post', $options);
}
/**
* Create application using GitHub repository
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps
*
* @param string $source - GitHub repository
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function createApplicationFromRepo($source, $options = array())
{
$options = array_merge($options, array(
'create_method' => 'remote_repo',
'repo' => $source,
));
return $this->createApplication($options);
}
/**
* Create application from file
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps
*
* @param string $source - file path
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function createApplicationFromFile($source, $options = array())
{
$options = array_merge($options, array(
'create_method' => 'file',
'file' => $this->file($source),
));
return $this->createApplication($options);
}
/**
* Update application
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_apps_id
*
* @param mixed: int | string $applicationId
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function updateApplication($applicationId, $options = array())
{
return $this->request(array('apps', $applicationId), 'put', $options);
}
/**
* Update application from GitHub repository
*
* No need to pass repository as it was set when application had been created
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_apps_id
*
* @param mixed: int | string $applicationId
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function updateApplicationFromRepo($applicationId, $options = array())
{
$options = array_merge($options, array(
'pull' => true,
));
return $this->updateApplication($applicationId, $options);
}
/**
* Update application from file
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_apps_id
*
* @param mixed: int | string $applicationId
* @param string $source - file path
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function updateApplicationFromFile($applicationId, $source, $options = array())
{
$options = array_merge($options, array(
'file' => $this->file($source),
));
return $this->updateApplication($applicationId, $options);
}
/**
* Update application icon
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps_id_icon
*
* @param mixed: int | string $applicationId
* @param string $source - png icon file path
*
* @return mixed: array on success | false on fail
*/
public function updateApplicationIcon($applicationId, $source)
{
$options['icon'] = $this->file($source);
return $this->request(array('apps', $applicationId, 'icon'), 'post', $options);
}
/**
* Start building application
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps_id_build
*
* @param mixed: int | string $applicationId
* @param array | string $platforms - platform name ('android', 'ios', ...')
*
* @return mixed: array on success | false on fail
*/
public function buildApplication($applicationId, $platforms = array())
{
$options = array();
if (! empty($platforms)) {
if (! is_array($platforms)) {
$platforms = array($platforms);
}
$options['platforms'] = $platforms;
}
return $this->request(array('apps', $applicationId, 'build'), 'post', $options);
}
/**
* Start building application for specified platform
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps_id_build_platform
*
* @param mixed: int | string $applicationId
* @param mixed: string | array $platform - platform name ('android', 'ios', ...')
*
* @return mixed: array on success | false on fail
*/
public function buildApplicationPlatform($applicationId, $platform)
{
return $this->request(array('apps', $applicationId, 'build', $platform), 'post');
}
/**
* Add collaborator
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps_id_collaborators
*
* @param mixed: int | string $applicationId
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function addCollaborator($applicationId, $options = array())
{
$defaults = array(
'email' => '',
'role' => self::ROLE_TESTER, // self::ROLE_DEV
);
$options = array_merge($defaults, $options);
return $this->request(array('apps', $applicationId, 'collaborators'), 'post', $options);
}
/**
* Update collaborator
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_apps_id_collaborators_id
*
* @param mixed: int | string $applicationId
* @param mixed: int | string $collaboratorId
* @param array $options- additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function updateCollaborator($applicationId, $collaboratorId, $options = array())
{
$defaults = array(
'role' => self::ROLE_TESTER, // self::ROLE_DEV
);
$options = array_merge($defaults, $options);
return $this->request(array('apps', $applicationId, 'collaborators', $collaboratorId), 'put', $options);
}
/**
* Add key for platform
*
* Better use function for specified platforms, cause it has some values restrictions
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_keys_platform
*
* @param string $platform - platform name ('android', 'ios', ...')
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function addKeyPlatform($platform, $options = array())
{
return $this->request(array('keys', $platform), 'post', $options);
}
/**
* Add key for android
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_keys_platform
*
* @param string $title - key title
* @param string $keystore - keystore file
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function addKeyAndroid($title, $keystore, $options = array())
{
$defaults = array(
'title' => $title,
'keystore' => $this->file($keystore),
// 'alias' => 'release',
// 'key_pw' => '',
// 'keystore_pw' => '',
);
$options = array_merge($defaults, $options);
return $this->addKeyPlatform(self::ANDROID, $options);
}
/**
* Add key for ios
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_keys_platform
*
* @param string $title - key title
* @param string $cert - p12 certificate file
* @param string $profile - mobileprovision file
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function addKeyIos($title, $cert, $profile, $options = array())
{
$defaults = array(
'title' => $title,
'cert' => $this->file($cert),
'profile' => $this->file($profile),
// 'password' => '',
);
$options = array_merge($defaults, $options);
return $this->addKeyPlatform(self::IOS, $options);
}
/**
* Update / unlock key for platform
*
* Better use function for specified platforms, cause it has some values restrictions
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_keys_platform
*
* @param string $platform
* @param mixed: int | string $keyId
* @param array $options - additional options, see details in API docs
*
* @return mixed: array on success | false on fail
*/
public function updateKeyPlatform($platform, $keyId, $options = array())
{
return $this->request(array('keys', $platform, $keyId), 'put', $options);
}
/**
* Update / unlock key for ios
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_keys_platform
*
* @param mixed: int | string $keyId
* @param string $password - key password
*
* @return mixed: array on success | false on fail
*/
public function updateKeyIos($keyId, $password)
{
$options['password'] = $password;
return $this->updateKeyPlatform(self::IOS, $keyId, $options);
}
/**
* Update / unlock key for android
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_put_https_build_phonegap_com_api_v1_keys_platform
*
* @param mixed: int | string $keyId
* @param string $keyPw - key password
* @param string $keystorePw - keystore password
*
* @return mixed: array on success | false on fail
*/
public function updateKeyAndroid($keyId, $keyPw, $keystorePw)
{
$options = array(
'key_pw' => $keyPw,
'keystore_pw' => $keystorePw,
);
return $this->updateKeyPlatform(self::ANDROID, $keyId, $options);
}
/**
* Delete application
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_delete_https_build_phonegap_com_api_v1_apps_id
*
* @param mixed: int | string $applicationId
*
* @return mixed: array on success | false on fail
*/
public function deleteApplication($applicationId)
{
return $this->request(array('apps', $applicationId), 'delete');
}
/**
* Delete collaborator
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_delete_https_build_phonegap_com_api_v1_apps_id_collaborators_id
*
* @param mixed: int | string $applicationId
* @param mixed: int | string $collaboratorId
*
* @return mixed: array on success | false on fail
*/
public function deleteCollaborator($applicationId, $collaboratorId)
{
return $this->request(array('apps', $applicationId, 'collaborators', $collaboratorId), 'delete');
}
/**
* Delete key for platform
*
* @link http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_delete_https_build_phonegap_com_api_v1_keys_platform_id
*
* @param string $platform - platform name ('android', 'ios', ...')
* @param mixed: int | string $keyId
*
* @return mixed: array on success | false on fail
*/
public function deleteKeyPlatform($platform, $keyId)
{
return $this->request(array('keys', $platform, $keyId), 'delete');
}
// ------------------------------------
// Public utility methods
// ------------------------------------
/**
* Check whether last request was successful
*
* @return bool
*/
public function success()
{
return $this->success;
}
/**
* Return error string for last request
*
* @return string
*/
public function error()
{
return $this->error;
}
// ------------------------------------
// Protected utility methods
// ------------------------------------
/**
* Set error
*
* @param string $error
*
* @return bool
*/
protected function setError($error)
{
$this->error = $error;
return false;
}
/**
* Clear last request options
*/
protected function clear()
{
$this->error = '';
$this->success = false;
}
/**
* Return file path for curl
*
* @param string $source
*
* @return string
*/
protected function file($source)
{
return '@' . realpath($source);
}
/**
* PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
* See: https://wiki.php.net/rfc/curl-file-upload
*
* @param string $file constructed file string
*
* @return mixed instance of CurlFile or string
*/
protected function toCurlFile($file) {
if (function_exists('curl_file_create')) {
// remove first @ character
return curl_file_create(substr($file, 1));
}
return $file;
}
/**
* Represent associative array as string
*
* @param array $array
* @param string $keyValueSeparator
* @param string $pairSeparator
*
* @return string
*/
protected function assocToString($array, $keyValueSeparator = ' - ', $pairSeparator = '; ')
{
if (! is_array($array)) {
return print_r($array, true);
}
foreach ($array as $key => &$value) {
$value = $key . $keyValueSeparator . $value;
}
return implode($pairSeparator, $array);
}
/**
* Perform request to api using CURL
*
* @param mixed $uri - may be uri string or array with uri parts
* @param string $method - request method
* @param array $options - api request parameters
*
* @return array (if succeeded) | boolean (false, if failed)
*/
protected function request($uri, $method = 'get', $options = array())
{
$this->clear();
if (! in_array(strtolower($method), $this->methods)) {
return $this->setError('Unknown request method: ' . $method);
}
$isAuthorized = $this->accessToken || $this->token ||
($this->username && $this->password);
if (! $isAuthorized) {
return $this->setError('Please provide token or username and password');
}
// if uri is passed as array, create uri string from uri parts
if (is_array($uri)) {
$uri = implode('/', $uri);
}
// api request url
$url = $this->endpoint . $uri;
$handle = curl_init();
if ($this->accessToken) {
// Access token is used in OAuth2 flow
$url .= '?access_token=' . $this->accessToken;
} else if ($this->token) {
// Auth token is taken from PG account
$url .= '?auth_token=' . $this->token;
} else {
// if using username and password - pass them as curl option
curl_setopt($handle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
}
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
// if request has additional options - add them to request
if (! empty($options)) {
// extract files, pass them separately
$files = array();
foreach ($options as $key => $value) {
if (! empty($value) && ! is_array($value) && $value[0] === '@') {
$files[$key] = $this->toCurlFile($value);
unset($options[$key]);
}
}
$data = $files;
if (! empty($options)) {
// original API requires data to be in json format
$data = array_merge($data, array(
'data' => json_encode($options),
));
}
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($handle);
if (curl_errno($handle)) {
$this->setError(curl_error($handle));
curl_close($handle);
return false;
}
// get httd code to identify if request was successful
$httpcode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$this->success = in_array($httpcode, $this->codes);
curl_close($handle);
$decoded = json_decode($response, true);
// request may be successful, but also can have errors, for example, about keys
if (isset($decoded['error']) && $error = $decoded['error']) {
if (is_array($error)) {
$error = $this->assocToString($error);
}
$this->setError($error);
}
if (! $this->success) {
return false;
}
return $decoded;
}
}