Last active
May 8, 2018 14:59
-
-
Save smartdeal/af290edff2341e4a8973065a2078e710 to your computer and use it in GitHub Desktop.
[LPtracker SDK my bicycle] #LPtracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ee($s) { | |
echo '<pre>'; | |
print_r($s); | |
echo '</pre>'; | |
} | |
function vd($s) { | |
echo '<pre>'; | |
var_dump($s); | |
echo '</pre>'; | |
} | |
class LPuse { | |
private $projectID = 1111; | |
private $last_contact = array(); // array('id' => 0, 'name' => '') | |
private $token = ''; | |
private $lp_url = 'direct.lptracker.ru'; | |
private $connect_opt = array( | |
'login' => '[email protected]', | |
'password' => '111', | |
'service' => '1111', | |
'version' => '1.0' | |
); | |
public $out; | |
function __construct() { | |
$curl_fields = json_encode($this->connect_opt); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $this->lp_url.'/login'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_fields); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
$headers = array(); | |
$headers[] = "Content-Type: application/json"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
$arr_result = json_decode($result, true); | |
if ($arr_result['status'] == 'success') { | |
$this->token = $arr_result['result']['token']; | |
} else { | |
$this->token = false; | |
} | |
} | |
private function curl_get($url = '') { | |
if (empty($url)) return false; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $this->lp_url.$url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
$headers = array(); | |
$headers[] = 'Token: '.$this->token; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
return json_decode($result, true); | |
} | |
private function curl_put($url = '', $fields = '') { | |
if (empty($url) || empty($fields)) return false; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $this->lp_url.$url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
$headers = array(); | |
$headers[] = "Content-Type: application/json"; | |
$headers[] = 'Token: '.$this->token; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
return json_decode($result, true); | |
} | |
private function curl_change_lead_field($leadID = 0, $customfieldID = 0, $fields='') { | |
if (empty($leadID) || empty($customfieldID) || empty($fields)) return false; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'direct.lptracker.ru/lead/'.$leadID.'/custom/'.$customfieldID); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
$headers = array(); | |
$headers[] = "Content-Type: application/json"; | |
$headers[] = 'Token: '.$this->token; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close ($ch); | |
return json_decode($result, true); | |
} | |
public function get_token() { | |
return $this->token; | |
} | |
public function get_project_info($project_id = 0) { | |
if ($project_id == 0) $project_id = $this->projectID; | |
return $this->curl_get('/project/'.$project_id); | |
} | |
public function get_contact_info($contact_id = 0) { | |
if ($contact_id == 0) { | |
if ($this->last_contactID == 0) | |
return false; | |
else | |
$contact_id = $this->last_contactID; | |
} | |
return $this->curl_get('/contact/'.$contact_id); | |
} | |
public function get_widget_info() { | |
return $this->curl_get('/project/'.$this->projectID.'/widget'); | |
} | |
/* | |
* Set contact | |
* Return contact ID or false; | |
* Save contactID to $this->last_contactID | |
*/ | |
public function set_contact($fields) { | |
$fields = array_merge(array('project_id' => strval($this->projectID)), $fields); | |
$result = $this->curl_put('/contact', $fields); | |
if ($result['status'] == 'success') { | |
$arrContact = array('id' => $result['result']['id'], 'name' => $fields['name']); | |
$this->last_contact = $arrContact; | |
} else { | |
$arrContact = false; | |
} | |
return $arrContact; | |
} | |
/* | |
* Set Lead | |
* Return contact ID or false; | |
* Save contactID to $this->last_contactID | |
*/ | |
public function set_lead($fields) { | |
$result = $this->curl_put('/lead', $fields); | |
if ($result['status'] == 'success') { | |
$leadID = $result['result']['id']; | |
} else { | |
$leadID = false; | |
} | |
return $leadID; | |
} | |
} | |
function run_112() { | |
$theLP = new LPuse(); | |
vd($theLP->get_token()); | |
$contact_data = array( | |
'name' => 'test', | |
'details' => array( | |
array( | |
'type' => 'phone', | |
'data' => '11111111111' | |
), | |
array( | |
'type' => 'email', | |
'data' => '[email protected]' | |
) | |
) | |
); | |
$result = $theLP->set_contact($contact_data); | |
vd($result); | |
if ($result) { | |
echo 'Contact created<br>'; | |
echo 'set lead</br>'; | |
$lead_data = array( | |
'contact_id' => $result['id'], | |
'name' => $result['name'], | |
'source' => 'test-api', | |
'campaign' => 'Campaign Name', | |
'keyword' => 'Keyword Name' | |
// 'custom' => array( | |
// '525356' => 'testAPI', // Площадка | |
// ) | |
); | |
$result = $theLP->set_lead($lead_data); | |
vd($result); | |
} | |
} | |
// run_112(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment