Skip to content

Instantly share code, notes, and snippets.

@rcarvs

rcarvs/RdStation.php

Last active Jun 26, 2020
Embed
What would you like to do?
RDStation API PHP - A singleton PHP class to auth and insert/update leads in RDStation with the control of max requests per minute.
<?php
class RdStation{
public static $instance;
private static $max_requests_per_minute = 60;
private static $base_url = "https://api.rd.services";
public $requests_count = null;
private $client_id;
private $client_secret;
private $code;
private $access_token;
private $refresh_token;
public function getRefreshToken(){
return $this->refresh_token;
}
public function __construct($client_id,$secret,$code,$refresh_token = null)
{
$this->client_id = $client_id;
$this->code = $code;
$this->client_secret = $secret;
$this->refresh_token = $refresh_token;
$this->auth();
}
public static function getInstance($client_id,$secret,$code,$refresh_token = null){
if(is_null(self::$instance)){
self::$instance = new RdStation($client_id,$secret,$code,$refresh_token);
}
return self::$instance;
}
private function addRequestCount(){
if(!isset($this->requests_count[date('H:m')])){
$this->requests_count[date('H:m')] = 1;
}else{
$this->requests_count[date('H:m')]++;
if($this->requests_count[date('H:m')] >= self::$max_requests_per_minute-1){
print_r("Chegou no limite. Vai aguardar 60 segundos.".PHP_EOL);
sleep(60);
$this->requests_count[date('H:m')] = 1;
}
}
}
public function auth(){
if(is_null($this->refresh_token) || empty($this->refresh_token)) {
$data = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'code' => $this->code);
}else {
$data = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'refresh_token' => $this->refresh_token);
}
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents(self::$base_url."/auth/token", false, $context);
//if ($result === FALSE) { /* Handle error */ }
$json = json_decode($result);
if(isset($json->access_token)){
$this->access_token = $json->access_token;
$this->expires_in = $json->expires_in;
$this->refresh_token = $json->refresh_token;
}
$this->addRequestCount();
}
public function getLeadByEmail($email){
$options = array(
'http' => array(
'header' =>'Authorization: Bearer ' .$this->access_token."\r\n".
"Content-Type: application/json\r\n",
'method' => 'GET'
)
);
$context = stream_context_create($options);
$result = file_get_contents(self::$base_url."/platform/contacts/email:".$email, false, $context);
$json = json_decode($result);
$this->addRequestCount();
if(isset($json->uuid)){
return $json;
}
return false;
}
private function getDataQueryFromArray($array){
$dataString = "{";
$c = 0;
foreach($array as $k => $d){
$c++;
if (is_array($d)){
$dataString .= '"' . $k . '":["'.implode('","',$d).'"],';
}else {
if(is_int($k)) {
$dataString .= '"' . $d . '",';
}else {
$dataString .= '"' . $k . '":"' . $d . '",';
}
}
if($c == (count($array))){
$dataString = substr($dataString,0,strlen($dataString)-1);
}
}
$dataString .= "}";
return $dataString;
}
public function updateOrInsertLeadByEmail($email,$atributes){
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' =>'Authorization: Bearer ' .$this->access_token."\r\n".
"Content-Type: application/json\r\n",
'method' => 'PATCH',
'content' => $this->getDataQueryFromArray($atributes),
)
);
//print_r($this->getDataQueryFromArray($atributes).PHP_EOL);
$context = stream_context_create($options);
$result = file_get_contents(self::$base_url."/platform/contacts/email:".$email, false, $context);
$json = json_decode($result);
$this->addRequestCount();
if(isset($json->uuid)){
return $json;
}
return false;
}
}
@farinon

This comment has been minimized.

Copy link

@farinon farinon commented Jun 18, 2020

Oi Amigo!

Estou tentando entender melhor o funcionamento da classe...
com o que devo preencher a variável $code?

@rcarvs

This comment has been minimized.

Copy link
Owner Author

@rcarvs rcarvs commented Jun 26, 2020

Oi farinon. O code é um codigo que a RD retorna na autenticação por callback. Acho que funciona como um código de autorização que o usuário dá para a API usar sua conta.

Segue a doc, o code está no passo 4: https://developers.rdstation.com/pt-BR/authentication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment