Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. <?php
  2. /**
  3. Authors:
  4. Júlio Paulillo <julio@agendor.com.br>
  5. Tulio Monte Azul <tulio@agendor.com.br>
  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&aacute;rio-no-site-ou-sistema-pr&oacute;prio-para-Cria&ccedil;&atilde;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. $this->token = $token;
  32. $this->privateToken = $privateToken;
  33. }
  34.  
  35. /**
  36. $type: (String) generic, leads, conversions
  37. **/
  38. protected function getURL($type='generic', $apiVersion='1.2'){
  39. //(POST) https://www.rdstation.com.br/api/1.2/services/PRIVATE_TOKEN/generic //USED TO CHANGE A LEAD STATUS
  40. //(PUT) https://www.rdstation.com.br/api/1.2/leads/:lead_email //USED TO UPDATE A LEAD
  41. //(POST) https://www.rdstation.com.br/api/1.2/conversions //USED TO SEND A NEW LEAD
  42. switch($type){
  43. case 'generic':
  44. return $this->baseURL.$apiVersion."/services/".$this->privateToken."/generic";
  45. case 'leads':
  46. return $this->baseURL.$apiVersion."/leads/";
  47. case 'conversions':
  48. return $this->baseURL.$apiVersion."/conversions";
  49. }
  50. }
  51.  
  52. protected function validateToken(){
  53. if(empty($this->token)) {
  54. throw new Exception("Inform RDStation.token as the second argument when instantiating a new RDStationAPI object.");
  55. }
  56. }
  57.  
  58. /**
  59. $method: (String) POST, PUT
  60. $url: (String) RD Station endpoint returned by $this->getURL()
  61. $data: (Array)
  62. **/
  63. protected function request($method="POST", $url, $data=array()){
  64.  
  65. $data['token_rdstation'] = $this->token;
  66. $JSONData = json_encode($data);
  67. $URLParts = parse_url($url);
  68.  
  69. $fp = fsockopen($URLParts['host'],
  70. isset($URLParts['port'])?$URLParts['port']:80,
  71. $errno, $errstr, 30);
  72.  
  73. $out = $method." ".$URLParts['path']." HTTP/1.1\r\n";
  74. $out .= "Host: ".$URLParts['host']."\r\n";
  75. $out .= "Content-Type: application/json\r\n";
  76. $out .= "Content-Length: ".strlen($JSONData)."\r\n";
  77. $out .= "Connection: Close\r\n\r\n";
  78. $out .= $JSONData;
  79. $written = fwrite($fp, $out);
  80. fclose($fp);
  81.  
  82. // return ($written==false)?false:true;
  83. return (bool) $written;
  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)) {
  107. throw new Exception("Inform at least the lead email as the first argument.");
  108. }
  109.  
  110. if(empty($data['identificador'])) {
  111. $data['identificador'] = $this->defaultIdentifier;
  112. }
  113.  
  114. if(empty($data["client_id"]) && !empty($_COOKIE["rdtrk"])) {
  115. $data["client_id"] = json_decode($_COOKIE["rdtrk"])->{'id'};
  116. }
  117.  
  118. if(empty($data["traffic_source"]) && !empty($_COOKIE["__trf_src"])) {
  119. $data["traffic_source"] = $_COOKIE["__trf_src"];
  120. }
  121.  
  122. $data['email'] = $email;
  123.  
  124. return $this->request("POST", $this->getURL('conversions'), $data);
  125. }
  126.  
  127. /**
  128. Helper function to update lead properties
  129. **/
  130. public function updateLead($email, $data=array()){
  131. $newData = array();
  132. $url = $this->getURL('leads', '1.3').$email;
  133. $newData['lead'] = $data;
  134. $newData['auth_token'] = $this->privateToken;
  135.  
  136. return $this->request("PUT", $url, $newData);
  137. }
  138.  
  139. /**
  140. $email: (String) Lead email
  141. $newStage: (Integer) 0 - Lead, 1 - Qualified Lead, 2 - Customer
  142. $opportunity: (Integer) true or false
  143. **/
  144.  
  145. public function updateLeadStageAndOpportunity($email, $newStage=0, $opportunity=false){
  146. if(empty($email)) {
  147. throw new Exception("Inform lead email as the first argument.");
  148. }
  149.  
  150. $url = $this->getURL('leads').$email;
  151.  
  152. $data = array(
  153. "auth_token" => $this->privateToken,
  154. "lead" => array(
  155. "lifecycle_stage" => $newStage,
  156. "opportunity" => $opportunity
  157. )
  158. );
  159.  
  160. return $this->request("PUT", $url, $data);
  161. }
  162.  
  163. /**
  164. $emailOrLeadId: (String / Integer) Lead email OR Lead unique custom ID
  165. $status: (String) won / lost
  166. $value: (Integer/Decimal) Purchase value
  167. $lostReason: (String)
  168. **/
  169. public function updateLeadStatus($emailOrLeadId, $status, $value=NULL, $lostReason=NULL) {
  170. if(empty($emailOrLeadId)) {
  171. throw new Exception("Inform lead email or unique custom ID as the first argument.");
  172. }
  173.  
  174. if(empty($status)) {
  175. throw new Exception("Inform lead status as the second argument.");
  176. } else if($status!="won"&&$status!="lost") {
  177. throw new Exception("Lead status (second argument) should be 'won' or 'lost'.");
  178. }
  179.  
  180. $data = array(
  181. "status" => $status,
  182. "value" => $value,
  183. "lost_reason" => $lostReason,
  184. );
  185.  
  186. if(is_integer($emailOrLeadId)) {
  187. $data["lead_id"] = $emailOrLeadId;
  188. } else {
  189. $data["email"] = $emailOrLeadId;
  190. }
  191.  
  192. return $this->request("POST", $this->getURL('generic'), $data);
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement