-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patheloquaRequest.php
More file actions
102 lines (83 loc) · 2.42 KB
/
eloquaRequest.php
File metadata and controls
102 lines (83 loc) · 2.42 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* REST client for Eloqua's API.
*/
class EloquaRequest
{
private $ch;
public $baseUrl;
public $responseInfo;
public function __construct($site, $user, $pass, $baseUrl)
{
// basic authentication credentials
$credentials = $site . '\\' . $user . ':' . $pass;
// set the base URL for the API endpoint
$this->baseUrl = $baseUrl;
// initialize the cURL resource
$this->ch = curl_init();
// set cURL and credential options
curl_setopt($this->ch, CURLOPT_URL, $this->baseUrl);
curl_setopt($this->ch, CURLOPT_USERPWD, $credentials);
// set headers
$headers = array('Content-type: application/json');
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
// return transfer as string
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
}
public function __destruct()
{
curl_close($this->ch);
}
public function get($url)
{
return $this->executeRequest($url, 'GET');
}
public function post($url, $data)
{
return $this->executeRequest($url, 'POST', $data);
}
public function put($url, $data)
{
return $this->executeRequest($url, 'PUT', $data);
}
public function delete($url)
{
return $this->executeRequest($url, 'DELETE');
}
public function executeRequest($url, $method, $data=null)
{
// set the full URL for the request
curl_setopt($this->ch, CURLOPT_URL, $this->baseUrl . '/' . $url);
switch ($method) {
case 'GET':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
break;
case 'POST':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PUT':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'DELETE':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
break;
}
// execute the request
$response = curl_exec($this->ch);
// store the response info including the HTTP status
// 400 and 500 status codes indicate an error
$this->responseInfo = curl_getinfo($this->ch);
$httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
if ($httpCode > 400)
{
print_r($this->responseInfo);
}
// todo : add support in constructor for contentType {xml, json}
return json_decode($response);
}
}
?>