This repository was archived by the owner on Jun 3, 2021. It is now read-only.
forked from nickschwab/php-ambassador
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathambassador.php
83 lines (65 loc) · 2.1 KB
/
ambassador.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
<?php
/**
* Ambassador v2 API PHP Wrapper
*
* @version 0.1
* @author_name Nick Schwab
* @author_email [email protected]
* @author_url http://nickschwab.com
* @date June 26, 2012
*
* Example use:
include_once(APPPATH.'libraries/ambassador.php');
$ambassador = new Ambassador("USERNAME","API_KEY");
$params = array('email' => '[email protected]');
$result = $ambassador->call("ambassador/get",$params);
print_r($result);
*/
class Ambassador {
protected $api_root = "https://getambassador.com/api/v2/";
protected $api_username = "none";
protected $api_key = "none";
public $http_code = 500;
public $result_raw = NULL;
public $result_array = array();
function __construct($username = NULL, $key = NULL){
if(!empty($username) && !empty($key))
$this->set_auth($username, $key);
}
function set_auth($username = NULL, $key = NULL){
if(!empty($username))
$this->api_username = $username;
if(!empty($key))
$this->api_key = $key;
}
function call($call_path = "", $param_array = array()){
$full_path = $this->api_root.$this->api_username."/".$this->api_key."/json/".$call_path;
$raw = $this->curl_request($full_path, $param_array);
if(!empty($raw)){
$this->result_array = json_decode($this->result_raw, TRUE);
}else{
$this->result_array = array();
}
return $this->result_array;
}
function curl_request($url, $params = array()){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
if(!empty($params)){
$params = http_build_query($params);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}else{
curl_setopt($curl, CURLOPT_POST, FALSE);
}
$this->result_raw = curl_exec($curl);
$this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $this->result_raw;
}
}