Squalo api - urejene vse funckcije za komunikacijo z apijem
This commit is contained in:
parent
70f775be27
commit
a4d4fe3ff9
@ -17,53 +17,253 @@ class SqualoApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function executeCall($action, $method, $data){
|
private function executeCall($action, $method, $data){
|
||||||
global $squalo_user;
|
global $squalo_user;
|
||||||
global $squalo_key;
|
global $squalo_key;
|
||||||
|
|
||||||
// Add credentials
|
// Add credentials
|
||||||
$data['apiKey'] = $squalo_key;
|
|
||||||
$data['apiUser'] = $squalo_user;
|
$data['apiUser'] = $squalo_user;
|
||||||
|
$data['apiKey'] = $squalo_key;
|
||||||
$params = '';
|
|
||||||
|
|
||||||
// GET call - set url params
|
// GET call - set url params
|
||||||
if($method == 'GET'){
|
if($method == 'GET'){
|
||||||
|
$response = $this->executeGET($action, $data);
|
||||||
$params .= '?';
|
}
|
||||||
|
// POST call
|
||||||
// GET params
|
else{
|
||||||
foreach($data as $name => $value){
|
$response = $this->executePOST($action, $data);
|
||||||
$params .= '&'.$name.'='.$value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$params = substr($params, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init curl
|
// Decode json response
|
||||||
$ch = curl_init($this->api_url.$action.$params);
|
$response_array = json_decode($response, true);
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
// Error
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
if($response_array['errorCode'] != '0'){
|
||||||
|
$result['error'] = $response_array["errorMessage"]. ' (code '.$response_array["errorCode"].')';
|
||||||
// POST call - set curl data
|
$result['success'] = false;
|
||||||
if($method == 'POST'){
|
}
|
||||||
// JSON string za POST
|
else{
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
$result = $response_array;
|
||||||
|
$result['success'] = true;
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_HEADER , 1);
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1);
|
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
||||||
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Izvedemo klic
|
|
||||||
$result = curl_exec($ch);
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Izvedemo post klic
|
||||||
|
private function executePOST($action, $data){
|
||||||
|
|
||||||
|
// Nastavimo url
|
||||||
|
$url = $this->api_url.$action;
|
||||||
|
|
||||||
|
|
||||||
|
// Init curl
|
||||||
|
$ch = curl_init($url);
|
||||||
|
|
||||||
|
// JSON string za POST
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
// for debug only!
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
|
$headers = array(
|
||||||
|
"Content-Type: application/json",
|
||||||
|
);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
|
|
||||||
|
// Izvedemo klic
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Izvedemo get klic
|
||||||
|
private function executeGET($action, $data){
|
||||||
|
|
||||||
|
// GET params
|
||||||
|
$params = '?';
|
||||||
|
foreach($data as $name => $value){
|
||||||
|
$params .= $name.'='.$value.'&';
|
||||||
|
}
|
||||||
|
$params = substr($params, 0, -1);
|
||||||
|
|
||||||
|
// Nastavimo celoten url s parametri
|
||||||
|
$url = $this->api_url.$action.$params;
|
||||||
|
|
||||||
|
|
||||||
|
// Init curl
|
||||||
|
$ch = curl_init($url);
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
|
||||||
|
// Izvedemo klic
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Ustvarimo seznam uporabnikov za pošiljanje
|
||||||
|
create-list
|
||||||
|
|
||||||
|
"name":"String content",
|
||||||
|
"description":"String content",
|
||||||
|
"listTag":"String content",
|
||||||
|
"color":"String content",
|
||||||
|
"ordering":2147483647,
|
||||||
|
"published":true
|
||||||
|
|
||||||
|
{subtag:name}
|
||||||
|
{subtag:code}
|
||||||
|
{subtag:url}
|
||||||
|
|
||||||
|
{date:4}
|
||||||
|
|
||||||
|
{unsubscribe}{/unsubscribe}
|
||||||
|
*/
|
||||||
|
public function createList($list_name){
|
||||||
|
|
||||||
|
$action = 'create-list';
|
||||||
|
$method = 'POST';
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'name' => $list_name,
|
||||||
|
'ordering' => $list_number,
|
||||||
|
'published' => true
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->executeCall($action, $method, $data);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Ustvarimo email,ki se bo poslal
|
||||||
|
|
||||||
|
"altBody":"String content",
|
||||||
|
"body":"String content",
|
||||||
|
"fromEmail":"String content",
|
||||||
|
"fromName":"String content",
|
||||||
|
"language":"String content",
|
||||||
|
"listIds":[2147483647],
|
||||||
|
"published":2147483647,
|
||||||
|
"replyToEmail":"String content",
|
||||||
|
"replyToName":"String content",
|
||||||
|
"subject":"String content",
|
||||||
|
"visible":true
|
||||||
|
*/
|
||||||
|
public function createNewsletter($list_id, $subject, $body, $body_alt, $from_email, $from_name, $reply_to_email, $language){
|
||||||
|
|
||||||
|
$action = 'create-newsletter';
|
||||||
|
$method = 'POST';
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'listIds' => array($list_id),
|
||||||
|
'subject' => $subject,
|
||||||
|
'body' => $body,
|
||||||
|
'altBody' => $body_alt,
|
||||||
|
'fromEmail' => $from_email,
|
||||||
|
'fromName' => $from_name,
|
||||||
|
'replyToEmail' => $reply_to_email,
|
||||||
|
'language' => $language,
|
||||||
|
'visible' => true
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->executeCall($action, $method, $data);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Dodamo prejemnika
|
||||||
|
|
||||||
|
"accept": true,
|
||||||
|
"confirmed": true,
|
||||||
|
"customAttributes": [{
|
||||||
|
"name": "firstname",
|
||||||
|
"value": "John"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "lastname",
|
||||||
|
"value": "Smith"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"email": "String content",
|
||||||
|
"enabled": true,
|
||||||
|
"html": true,
|
||||||
|
"listIds": [2147483647],
|
||||||
|
"name": "String content",
|
||||||
|
"surname": "String content",
|
||||||
|
"gender": "null | male | female | other",
|
||||||
|
"gdprCanSend": true,
|
||||||
|
"gdprCanTrack": true
|
||||||
|
*/
|
||||||
|
public function addRecipient($email, $list_id, $custom_attributes=array()){
|
||||||
|
|
||||||
|
$action = 'create-recipient';
|
||||||
|
$method = 'POST';
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'email' => $email,
|
||||||
|
'listIds' => array($list_id),
|
||||||
|
'accept' => true,
|
||||||
|
'confirmed' => true,
|
||||||
|
'enabled' => true,
|
||||||
|
'gdprCanSend' => true,
|
||||||
|
'gdprCanTrack' => true
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->executeCall($action, $method, $data);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Pošljemo emaile
|
||||||
|
|
||||||
|
"newsletterId":2147483647,
|
||||||
|
"sendDate":2147483647
|
||||||
|
*/
|
||||||
|
public function sendEmails($newsletter_id){
|
||||||
|
|
||||||
|
$action = 'send-newsletter';
|
||||||
|
$method = 'GET';
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'newsletterId' => $newsletter_id,
|
||||||
|
'sendDate' => time()+30
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->executeCall($action, $method, $data);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Pobrisemo prejemnika
|
||||||
|
public function deleteRecipient($recipient_id){
|
||||||
|
|
||||||
|
$action = 'delete-recipient';
|
||||||
|
$method = 'GET';
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'recipientId' => $recipient_id
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->executeCall($action, $method, $data);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user