├── README.md ├── LICENSE └── RDStationAPI.class.php /README.md: -------------------------------------------------------------------------------- 1 | #Usage 2 | ```php 3 | require("RDStationAPI.class.php"); 4 | $rdAPI = new RDStationAPI("RD_PRIVATE_TOKEN", "RD_TOKEN"); 5 | 6 | //SEND NEW LEAD TO RD STATION 7 | $return1 = $rdAPI->sendNewLead("customer@customer.com", array( 8 | "name" => "Julio", 9 | "job_title" => "Cofounder", 10 | "company"=> "Agendor", 11 | "identificador" => "contact-form", 12 | "subscribe_newsletter" => true //custom data is accepted 13 | )); 14 | 15 | // UPDATE ANY LEAD INFO 16 | $return2 = $rdAPI->updateLead("customer@customer.com", array( 17 | "name" => "New Name", 18 | "company"=> "Agendor", 19 | "opportunity" => true, 20 | "lifecycle_stage" => 1, 21 | "Any Custom Field" => "Custom Field Value" 22 | )); 23 | 24 | //UPDATE LEAD STATUS TO 'won' 25 | $return3 = $rdAPI->updateLeadStatus("customer@customer.com", 'won', 999.99); 26 | 27 | //UPDATE LEAD STATUS TO 'lost' 28 | $return4 = $rdAPI->updateLeadStatus("customer@customer.com", 'lost', 999.99, 'Lost reason'); 29 | 30 | //UPDATE LEAD FUNNEL STAGE AND OPPORTUNITY FLAG 31 | $return5 = $rdAPI->updateLeadStageAndOpportunity("customer@customer.com", 1, true); 32 | ``` 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Agendor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /RDStationAPI.class.php: -------------------------------------------------------------------------------- 1 | 5 | Tulio Monte Azul 6 | 7 | DOCS 8 | ############# 9 | Guia de integrações 10 | http://ajuda.rdstation.com.br/hc/pt-br/articles/200310549-Guia-de-integra%C3%A7%C3%B5es-com-o-RD-Station 11 | 12 | Marcar venda e lost via formulário próprio ou sistema (API) 13 | http://ajuda.rdstation.com.br/hc/pt-br/articles/202640385-Marcar-venda-e-lost-via-formul%C3%A1rio-pr%C3%B3prio-ou-sistema-API- 14 | 15 | Alterar estado do Lead no funil do RD Station (API) 16 | http://ajuda.rdstation.com.br/hc/pt-br/articles/200310699-Alterar-estado-do-Lead-no-funil-do-RD-Station-API- 17 | 18 | Integrar formulário no site ou sistema próprio para Criação de Lead (API) 19 | 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- 20 | **/ 21 | 22 | class RDStationAPI { 23 | 24 | public $token; 25 | public $privateToken; 26 | public $baseURL = "https://www.rdstation.com.br/api/"; 27 | public $defaultIdentifier = "rdstation-php-integration"; 28 | 29 | public function __construct($privateToken=NULL, $token=NULL){ 30 | if(empty($privateToken)) throw new Exception("Inform RDStationAPI.privateToken as the first argument."); 31 | 32 | $this->token = $token; 33 | $this->privateToken = $privateToken; 34 | } 35 | 36 | /** 37 | $type: (String) generic, leads, conversions 38 | **/ 39 | protected function getURL($type='generic', $apiVersion='1.2'){ 40 | //(POST) https://www.rdstation.com.br/api/1.2/services/PRIVATE_TOKEN/generic //USED TO CHANGE A LEAD STATUS 41 | //(PUT) https://www.rdstation.com.br/api/1.2/leads/:lead_email //USED TO UPDATE A LEAD 42 | //(POST) https://www.rdstation.com.br/api/1.2/conversions //USED TO SEND A NEW LEAD 43 | switch($type){ 44 | case 'generic': return $this->baseURL.$apiVersion."/services/".$this->privateToken."/generic"; 45 | case 'leads': return $this->baseURL.$apiVersion."/leads/"; 46 | case 'conversions': return $this->baseURL.$apiVersion."/conversions"; 47 | } 48 | } 49 | 50 | protected function validateToken(){ 51 | if(empty($this->token)) throw new Exception("Inform RDStation.token as the second argument when instantiating a new RDStationAPI object."); 52 | } 53 | 54 | /** 55 | $method: (String) POST, PUT 56 | $url: (String) RD Station endpoint returned by $this->getURL() 57 | $data: (Array) 58 | **/ 59 | protected function request($method="POST", $url, $data=array()){ 60 | 61 | $data['token_rdstation'] = $this->token; 62 | $JSONData = json_encode($data); 63 | $URLParts = parse_url($url); 64 | 65 | if ($URLParts['scheme'] == "https") { 66 | $targetHost = "tls://".$URLParts['host']; 67 | $targetPort = isset($URLParts['port'])?$URLParts['port']:443; 68 | } else { 69 | $targetHost = $URLParts['host']; 70 | $targetPort = isset($URLParts['port'])?$URLParts['port']:80; 71 | } 72 | $fp = fsockopen($targetHost, $targetPort, $errno, $errstr, 30); 73 | $out = $method." ".$URLParts['path']." HTTP/1.1\r\n"; 74 | $out .= "Host: ".$URLParts['host']."\r\n"; 75 | $out .= "User-Agent: rdstation-php-client\r\n"; 76 | $out .= "Content-Type: application/json\r\n"; 77 | $out .= "Content-Length: ".strlen($JSONData)."\r\n"; 78 | $out .= "Connection: Close\r\n\r\n"; 79 | $out .= $JSONData; 80 | $written = fwrite($fp, $out); 81 | fclose($fp); 82 | 83 | return ($written==false)?false:true; 84 | } 85 | 86 | /** 87 | $email: (String) The email of the lead 88 | $data: (Array) Custom data array, example: 89 | array( 90 | "identificador" => "contact-form", 91 | "nome" => "Júlio Paulillo", 92 | "empresa" => "Agendor", 93 | "cargo" => "Cofounder", 94 | "telefone" => "(11) 3280-8090", 95 | "celular" => "(11) 99999-9999", 96 | "website" => "www.agendor.com.br", 97 | "twitter" => "twitter.com/paulillo", 98 | "facebook" => "facebook.com/paulillo", 99 | "c_utmz" => "", 100 | "created_at" => "", 101 | "tags" => "cofounder, hotlead" 102 | ); 103 | **/ 104 | public function sendNewLead($email, $data=array()){ 105 | $this->validateToken(); 106 | if(empty($email)) throw new Exception("Inform at least the lead email as the first argument."); 107 | if(empty($data['identificador'])) $data['identificador'] = $this->defaultIdentifier; 108 | if(empty($data["client_id"]) && !empty($_COOKIE["rdtrk"])) $data["client_id"] = json_decode($_COOKIE["rdtrk"])->{'id'}; 109 | if(empty($data["traffic_source"]) && !empty($_COOKIE["__trf_src"])) $data["traffic_source"] = $_COOKIE["__trf_src"]; 110 | 111 | $data['email'] = $email; 112 | 113 | return $this->request("POST", $this->getURL('conversions'), $data); 114 | } 115 | 116 | /** 117 | Helper function to update lead properties 118 | **/ 119 | public function updateLead($email, $data=array()){ 120 | $newData = array(); 121 | $url = $this->getURL('leads', '1.3').$email; 122 | $newData['lead'] = $data; 123 | $newData['auth_token'] = $this->privateToken; 124 | 125 | return $this->request("PUT", $url, $newData); 126 | } 127 | 128 | /** 129 | $email: (String) Lead email 130 | $newStage: (Integer) 0 - Lead, 1 - Qualified Lead, 2 - Customer 131 | $opportunity: (Integer) true or false 132 | **/ 133 | 134 | public function updateLeadStageAndOpportunity($email, $newStage=0, $opportunity=false){ 135 | if(empty($email)) throw new Exception("Inform lead email as the first argument."); 136 | 137 | $url = $this->getURL('leads').$email; 138 | 139 | $data = array( 140 | "auth_token" => $this->privateToken, 141 | "lead" => array( 142 | "lifecycle_stage" => $newStage, 143 | "opportunity" => $opportunity 144 | ) 145 | ); 146 | 147 | return $this->request("PUT", $url, $data); 148 | } 149 | 150 | /** 151 | $emailOrLeadId: (String / Integer) Lead email OR Lead unique custom ID 152 | $status: (String) won / lost 153 | $value: (Integer/Decimal) Purchase value 154 | $lostReason: (String) 155 | **/ 156 | public function updateLeadStatus($emailOrLeadId, $status, $value=NULL, $lostReason=NULL) { 157 | if(empty($emailOrLeadId)) throw new Exception("Inform lead email or unique custom ID as the first argument."); 158 | if(empty($status)) throw new Exception("Inform lead status as the second argument."); 159 | else if($status!="won"&&$status!="lost") throw new Exception("Lead status (second argument) should be 'won' or 'lost'."); 160 | 161 | $data = array( 162 | "status" => $status, 163 | "value" => $value, 164 | "lost_reason" => $lostReason, 165 | ); 166 | 167 | if(is_integer($emailOrLeadId)) $data["lead_id"] = $emailOrLeadId; 168 | else $data["email"] = $emailOrLeadId; 169 | 170 | return $this->request("POST", $this->getURL('generic'), $data); 171 | } 172 | } 173 | 174 | ?> 175 | --------------------------------------------------------------------------------