Parale::makeRequest()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 8.439
cc 5
eloc 29
nc 6
nop 2
1
<?php namespace DouaParale\Parale;
2
/**
3
 * Super-simple, minimum abstraction 2Parale API v1 wrapper
4
 *
5
 * Uses curl by default and falls back to file_get_contents and HTTP stream.
6
 * This probably has more comments than code.
7
 *
8
 * @author Costin "necenzurat" Moise <[email protected]>
9
 * @version 0.1.0
10
 */
11
12
class Parale {
13
	private $username;
14
	private $password;
15
	private $api_endpoint = "https://api.2parale.ro";
16
	private $verify_ssl = false;
17
	private $api_version = "0.1";
18
19
	/**
20
	 * Create a new instance
21
	 * @param string $username Your username
22
	 * @param string $password Your password
23
	 */
24
	function __construct($username, $password) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
		$this->username = $username;
26
		$this->password = $password;
27
	}
28
29
	/**
30
	 * Call method, just call me maybe
31
	 * @param  string $method The API method to call, e.g. 'campaigns/listforaffiliate'
32
	 * @param  array  $args   An array of arguments to pass to the method e.g. page=2 Will be json-encoded for you.
33
	 * @return array          Associative array of json decoded API response.
34
	 */
35
	public function call($method, $args = array()) {
36
		return $this->makeRequest($method, $args);
37
	}
38
39
	/**
40
	 * @param string $method
41
	 */
42
	private function makeRequest($method, $args = array()) {
43
44
		$url = $this->api_endpoint . '/' . $method . '.json';
45
46
		if (function_exists('curl_init') && function_exists('curl_setopt')) {
47
			$ch = curl_init();
48
			curl_setopt($ch, CURLOPT_URL, $url);
49
			curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
50
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
51
			curl_setopt($ch, CURLOPT_USERAGENT, "2Parale-" . $this->api_version);
52
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
54
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
55
			if (!empty($args)) {
56
				curl_setopt($ch, CURLOPT_POST, true);
57
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
58
			}
59
			$result = curl_exec($ch);
60
			curl_close($ch);
61
		} else {
62
			$json_data = json_encode($args);
63
			$result = file_get_contents($url, null, stream_context_create(array(
64
				'http' => array(
65
					'protocol_version' => 1.1,
66
					'user_agent' => "2Parale-" . $this->api_version,
67
					'method' => 'POST',
68
					'header' => "Authorization: Basic " . base64_encode($this->username . ':' . $this->password) . "\r\n" .
69
					"Content-type: application/json\r\n" .
70
					"Connection: close\r\n" .
71
					"Content-length: " . strlen($json_data) . "\r\n",
72
					'content' => $json_data,
73
				),
74
			)));
75
		}
76
		return $result ? json_decode($result, true) : false;
77
	}
78
79
}
80