Completed
Push — master ( 2c597e...326f00 )
by Sand
01:45
created

SandCage::payloadArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 2
1
<?php 
2
3
namespace SandCage;
4
5
class SandCage {
6
	// Your SandCage API Key
7
	// This can be retrieved from https://www.sandcage.com/panel/api_key
8
	protected $sandcage_api_key = '[YOUR SANDCAGE API KEY]';
9
	
10
	// SandCage API version
11
	protected $sandcage_api_version = '0.2';
12
13
	// SandCage API endpoint base
14
	protected $sandcage_api_endpoint_base;
15
16
	protected $user_agent;
17
	protected $follow_location = false;
18
	protected $timeout = 30;
19
	protected $post_fields;
20
	protected $status;
21
	protected $response;
22
23
	public function __construct($sandcage_api_key = null) {
24
25
		if (!is_null($sandcage_api_key)) {
26
			$this->sandcage_api_key = $sandcage_api_key;
27
		}
28
		$this->sandcage_api_endpoint_base = 'https://api.sandcage.com/' . $this->sandcage_api_version . '/';
29
		$this->user_agent = 'SandCage - ' . $this->sandcage_api_version;
30
31
	}
32
33
	/** 
34
	 * Send a requst using cURL 
35
	 * @param string $service endpoint to request
36
	 * @param array $payload values to send
37
	 * @param string $callback_endpoint to send the callback to. Optional
38
	 */ 
39
	public function call($service, $payload, $callback_endpoint = '') {
40
41
		$service_endpoint = $this->sandcage_api_endpoint_base . $service;
42
		
43
		$this->payloadArray($payload, $callback_endpoint);
44
45
		// Initialize the cURL session
46
		$ch = curl_init($service_endpoint);
47
48
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
49
50
		// Handle open_basedir & safe mode
51
		if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
52
			$this->follow_location = true;
53
		}
54
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
55
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
56
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
57
		curl_setopt($ch, CURLOPT_POST, TRUE);
58
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields));
59
60
		// Execute the cURL session
61
		$this->response = curl_exec($ch);
62
63
		// Retry if certificates are missing.
64
		if (curl_errno($ch) == CURLE_SSL_CACERT) {
65
66
			// Set the pem file holding the CA Root Certificates to verify the peer with.
67
			curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
68
			
69
			// Retry execution after setting CURLOPT_CAINFO
70
			$this->response = curl_exec($ch);
71
			
72
		}
73
74
		// Get information regarding the transfer
75
		$this->status = curl_getinfo($ch);
76
77
		// Close the cURL session
78
		curl_close($ch);
79
80
	}
81
82
	/** 
83
	 * Build the payload array
84
	 * @param array $payload values to send
85
	 * @param string $callback_endpoint to send the callback to
86
	 */ 
87
	private function payloadArray($payload, $callback_endpoint) {
88
89
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
90
91
		if ($callback_endpoint != '') {
92
			$this->post_fields['callback_url'] = $callback_endpoint;
93
		}
94
95
	}
96
97
	/** 
98
	 * Return the HTTP status of the call
99
	 * @return array or FALSE on failure 
100
	 */ 
101
	public function getHttpStatus() {
102
103
		return $this->status;
104
105
	}
106
107
	/** 
108
	 * Return the HTTP status of the call
109
	 * @return array or FALSE on failure  
110
	 */ 
111
	public function getResponse() {
112
113
		return $this->response;
114
115
	}
116
}
117