Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 100 additions & 16 deletions src/SmartEmailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,42 @@ class SmartEmailing extends \Nette\Object

protected $token;

/**
* SmartEmailing constructor.
*
* @param string $username
* @param string $token
*/
public function __construct($username, $token)
{
$this->username = $username;
$this->token = $token;
}


/**
* insert contact to Smartemailing
*
* @param string $email
* @param array $contactlists
* @param array $properties
* @param array $customfields
* @return \SimpleXMLElement
*/
public function contactInsert($email, $contactlists = array(), $properties = array(), $customfields = array()) {
return $this->contactUpdate($email, $contactlists, $properties, $customfields);
}


/**
* update contact in Smartemailing
*
* @param string $email
* @param array $contactlists
* @param array $properties
* @param array $customfields
* @return \SimpleXMLElement
*/
public function contactUpdate($email, $contactlists = array(), $properties = array(), $customfields = array()) {
$details = [];

Expand Down Expand Up @@ -69,7 +94,7 @@ public function contactUpdate($email, $contactlists = array(), $properties = arr
/**
* get Smartemailing contact by email address
*
* @param String $email
* @param string $email
*
* @return \SimpleXMLElement
*/
Expand All @@ -93,7 +118,7 @@ public function getOneByEmail($email) {
/**
* get Smartemailing contact by ID
*
* @param int $id
* @param int $id
*
* @return [ty\SimpleXMLElement
*/
Expand All @@ -117,7 +142,7 @@ public function contactGetOneByID($id) {
/**
* delete Smartemailing contact by email address
*
* @param String $email
* @param string $email
*
* @return \SimpleXMLElement
*/
Expand Down Expand Up @@ -168,8 +193,6 @@ public function getAllUnsubscribedContacts() {
* @return \SimpleXMLElement
*/
public function multipleContactsInsert($contacts) {
$dateTime = new \DateTime();

$contactsArray = [];

foreach ($contacts as $email => $cData) {
Expand Down Expand Up @@ -202,6 +225,9 @@ public function multipleContactsInsert($contacts) {

/**
* convert array to xml
*
* @param array $array
* @param \SimpleXMLElement $xml
*/
protected function arrayToXml($array, &$xml) {
foreach($array as $key => $value) {
Expand All @@ -225,6 +251,10 @@ protected function arrayToXml($array, &$xml) {

/**
* creating simple xml
*
* @param array $array
* @param string $rootElementName
* @return string | bool ... string on success and FALSE on error
*/
protected function createSimpleXml($array, $rootElementName) {
$xml = new \SimpleXMLElement('<' . $rootElementName . '></' . $rootElementName . '>');
Expand All @@ -235,25 +265,79 @@ protected function createSimpleXml($array, $rootElementName) {
}


/**
* connect to Smartemailing API v2
*
* @param array $data
* @return \SimpleXMLElement
*/
protected function callSmartemailingApiWithCurl($data) {
$ch = curl_init();
try {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
$postFields = $this->createSimpleXml($data, 'xmlrequest');

$postFields = $this->createSimpleXml($data, 'xmlrequest');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));

curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$response = curl_exec($ch);

$response = curl_exec($ch);
curl_close($ch);

} catch (\Exception $e) {
return $this->getErrorXml($e->getCode(), $e->getMessage());
}

curl_close($ch);

return new \SimpleXMLElement($response);
if ($this->isValidXmlString($response)) {
return new \SimpleXMLElement($response);

} else {
return $this->getErrorXml('500', 'Unknown Smartemailing API error.');
}
}


/**
* return error XML
*
* @param string $code
* @param string $message
* @return \SimpleXMLElement
*/
public function getErrorXml($code, $message) {
$xml = new \SimpleXMLElement('<response></response>');
$errorData = ['code' => $code, 'message' => $message];

$this->arrayToXml($errorData, $xml);

return $xml;
}


/**
* check if xml string is valid
*
* @param string $xmlString
* @return bool
*/
protected function isValidXmlString($xmlString) {
libxml_use_internal_errors(TRUE);

$doc = simplexml_load_string($xmlString);

if (!$doc) {
$errors = libxml_get_errors();

return empty($errors);
}

return FALSE;
}

}