-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRDStationAPI.class.php
More file actions
174 lines (145 loc) · 6.03 KB
/
RDStationAPI.class.php
File metadata and controls
174 lines (145 loc) · 6.03 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
Authors:
Júlio Paulillo <julio@agendor.com.br>
Tulio Monte Azul <tulio@agendor.com.br>
DOCS
#############
Guia de integrações
http://ajuda.rdstation.com.br/hc/pt-br/articles/200310549-Guia-de-integra%C3%A7%C3%B5es-com-o-RD-Station
Marcar venda e lost via formulário próprio ou sistema (API)
http://ajuda.rdstation.com.br/hc/pt-br/articles/202640385-Marcar-venda-e-lost-via-formul%C3%A1rio-pr%C3%B3prio-ou-sistema-API-
Alterar estado do Lead no funil do RD Station (API)
http://ajuda.rdstation.com.br/hc/pt-br/articles/200310699-Alterar-estado-do-Lead-no-funil-do-RD-Station-API-
Integrar formulário no site ou sistema próprio para Criação de Lead (API)
http://ajuda.rdstation.com.br/hc/pt-br/articles/200310589-Integrar-formulário-no-site-ou-sistema-próprio-para-Criação-de-Lead-API-
**/
class RDStationAPI {
public $token;
public $privateToken;
public $baseURL = "https://www.rdstation.com.br/api/";
public $defaultIdentifier = "rdstation-php-integration";
public function __construct($privateToken=NULL, $token=NULL){
if(empty($privateToken)) throw new Exception("Inform RDStationAPI.privateToken as the first argument.");
$this->token = $token;
$this->privateToken = $privateToken;
}
/**
$type: (String) generic, leads, conversions
**/
protected function getURL($type='generic', $apiVersion='1.2'){
//(POST) https://www.rdstation.com.br/api/1.2/services/PRIVATE_TOKEN/generic //USED TO CHANGE A LEAD STATUS
//(PUT) https://www.rdstation.com.br/api/1.2/leads/:lead_email //USED TO UPDATE A LEAD
//(POST) https://www.rdstation.com.br/api/1.2/conversions //USED TO SEND A NEW LEAD
switch($type){
case 'generic': return $this->baseURL.$apiVersion."/services/".$this->privateToken."/generic";
case 'leads': return $this->baseURL.$apiVersion."/leads/";
case 'conversions': return $this->baseURL.$apiVersion."/conversions";
}
}
protected function validateToken(){
if(empty($this->token)) throw new Exception("Inform RDStation.token as the second argument when instantiating a new RDStationAPI object.");
}
/**
$method: (String) POST, PUT
$url: (String) RD Station endpoint returned by $this->getURL()
$data: (Array)
**/
protected function request($method="POST", $url, $data=array()){
$data['token_rdstation'] = $this->token;
$JSONData = json_encode($data);
$URLParts = parse_url($url);
if ($URLParts['scheme'] == "https") {
$targetHost = "tls://".$URLParts['host'];
$targetPort = isset($URLParts['port'])?$URLParts['port']:443;
} else {
$targetHost = $URLParts['host'];
$targetPort = isset($URLParts['port'])?$URLParts['port']:80;
}
$fp = fsockopen($targetHost, $targetPort, $errno, $errstr, 30);
$out = $method." ".$URLParts['path']." HTTP/1.1\r\n";
$out .= "Host: ".$URLParts['host']."\r\n";
$out .= "User-Agent: rdstation-php-client\r\n";
$out .= "Content-Type: application/json\r\n";
$out .= "Content-Length: ".strlen($JSONData)."\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $JSONData;
$written = fwrite($fp, $out);
fclose($fp);
return ($written==false)?false:true;
}
/**
$email: (String) The email of the lead
$data: (Array) Custom data array, example:
array(
"identificador" => "contact-form",
"nome" => "Júlio Paulillo",
"empresa" => "Agendor",
"cargo" => "Cofounder",
"telefone" => "(11) 3280-8090",
"celular" => "(11) 99999-9999",
"website" => "www.agendor.com.br",
"twitter" => "twitter.com/paulillo",
"facebook" => "facebook.com/paulillo",
"c_utmz" => "",
"created_at" => "",
"tags" => "cofounder, hotlead"
);
**/
public function sendNewLead($email, $data=array()){
$this->validateToken();
if(empty($email)) throw new Exception("Inform at least the lead email as the first argument.");
if(empty($data['identificador'])) $data['identificador'] = $this->defaultIdentifier;
if(empty($data["client_id"]) && !empty($_COOKIE["rdtrk"])) $data["client_id"] = json_decode($_COOKIE["rdtrk"])->{'id'};
if(empty($data["traffic_source"]) && !empty($_COOKIE["__trf_src"])) $data["traffic_source"] = $_COOKIE["__trf_src"];
$data['email'] = $email;
return $this->request("POST", $this->getURL('conversions'), $data);
}
/**
Helper function to update lead properties
**/
public function updateLead($email, $data=array()){
$newData = array();
$url = $this->getURL('leads', '1.3').$email;
$newData['lead'] = $data;
$newData['auth_token'] = $this->privateToken;
return $this->request("PUT", $url, $newData);
}
/**
$email: (String) Lead email
$newStage: (Integer) 0 - Lead, 1 - Qualified Lead, 2 - Customer
$opportunity: (Integer) true or false
**/
public function updateLeadStageAndOpportunity($email, $newStage=0, $opportunity=false){
if(empty($email)) throw new Exception("Inform lead email as the first argument.");
$url = $this->getURL('leads').$email;
$data = array(
"auth_token" => $this->privateToken,
"lead" => array(
"lifecycle_stage" => $newStage,
"opportunity" => $opportunity
)
);
return $this->request("PUT", $url, $data);
}
/**
$emailOrLeadId: (String / Integer) Lead email OR Lead unique custom ID
$status: (String) won / lost
$value: (Integer/Decimal) Purchase value
$lostReason: (String)
**/
public function updateLeadStatus($emailOrLeadId, $status, $value=NULL, $lostReason=NULL) {
if(empty($emailOrLeadId)) throw new Exception("Inform lead email or unique custom ID as the first argument.");
if(empty($status)) throw new Exception("Inform lead status as the second argument.");
else if($status!="won"&&$status!="lost") throw new Exception("Lead status (second argument) should be 'won' or 'lost'.");
$data = array(
"status" => $status,
"value" => $value,
"lost_reason" => $lostReason,
);
if(is_integer($emailOrLeadId)) $data["lead_id"] = $emailOrLeadId;
else $data["email"] = $emailOrLeadId;
return $this->request("POST", $this->getURL('generic'), $data);
}
}
?>