-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSubscription.php
More file actions
81 lines (68 loc) · 2.36 KB
/
Subscription.php
File metadata and controls
81 lines (68 loc) · 2.36 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
<?php
namespace Tco\Source\Api\Rest\V6;
use Tco\Interfaces\Auth;
use Tco\Source\TcoConfig;
use Tco\Exceptions\TcoException;
class ApiCore {
/**
* @var TcoConfig
*/
public $tcoConfig;
/**
* @var Auth
*/
public $auth;
/**
* ApiCore constructor.
*
* @param TcoConfig $tcoConfig
* @param Auth $auth
*/
public function __construct( $tcoConfig, $auth ) {
$this->tcoConfig = $tcoConfig;
$this->auth = $auth;
}
/**
* @param $endpoint
* @param $params
* @param string $method
* @param string $apiLocation
*
* @return mixed
* @throws \TcoException
*/
public function call( $endpoint, $params, $method = 'POST', $apiLocation = 'restApi' ) {
try {
$url = $this->tcoConfig->getApiEndpoints()[ $apiLocation ] . $endpoint;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->auth->getHeaders() );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
if ( ! $this->tcoConfig->getCurlVerifySsl() ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false ); //by default value is 2
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); //by default value is 1
}
if ( $method === 'POST' ) {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
}
if ( $method === 'PUT' ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
}
if ( $method === 'DELETE' ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$response = curl_exec( $ch );
curl_close( $ch );
if ( $response === false ) {
throw new TcoException( sprintf('Curl response :%s', curl_error( $ch )));
}
return json_decode( $response, true );
} catch ( Exception $e ) {
throw new TcoException( sprintf('Exception ApiCore response: %s', $e->getMessage()
) );
}
}
}