Last active
September 14, 2017 12:21
-
-
Save leadbi/bd7e0b2a4e36a9dd7cfd84df0ddde124 to your computer and use it in GitHub Desktop.
Swift MTA Simple Logs API
This file contains 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 // example.php | |
// include SwiftMTA.php | |
// new api client | |
$api = new SwiftMTA('test.com', 'test_user', 'test1'); | |
// Filter recipient | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'email_to' => 'test@test.com' | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Filter recipient ends with | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'email_to' => [ | |
'endsWith' => 'test.com' | |
] | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Filter sender | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'email_from' => 'test@test.com' | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Filter all bounced | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'type' => 'bounce' | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Filter by date | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'date_time' => [ | |
'greaterThanOrEqual' => [ 'date' => '2017-09-04', 'format' => 'YYYY-MM-DD' ] | |
] | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Filter between dates | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'date_time' => [ | |
'greaterThanOrEqual' => [ 'date' => '2017-09-04', 'format' => 'YYYY-MM-DD' ], | |
'lessThanOrEqual' => [ 'date' => '2017-09-05', 'format' => 'YYYY-MM-DD' ], | |
] | |
], | |
'projection' => [], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Only list sender and recipient | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'type' => 'bounce' | |
], | |
'projection' => ['email_from', 'email_to'], | |
'order' => [] | |
]); | |
var_dump($logs->toArray()); | |
// Order by date | |
$logs = $api->queryLogs([ | |
'filters' => [ | |
'type' => 'bounce' | |
], | |
'projection' => ['email_from', 'email_to'], | |
'order' => [ 'date_time' => 'asc'] | |
]); | |
var_dump($logs->toArray()); | |
// Load more | |
var_dump($logs->loadMore()->toArray()); | |
This file contains 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 // SwiftMTA.php | |
class SwiftMTALogsCursor { | |
/** | |
* Create result cursor | |
*/ | |
public function __construct($api, $query, $result, $limit, $offset){ | |
$this->api = $api; | |
$this->limit = $limit; | |
$this->offset = $offset; | |
$this->result = $result; | |
$this->query = $query; | |
} | |
/** | |
* Return the current response to array | |
*/ | |
public function toArray(){ | |
return $this->result; | |
} | |
/** | |
* Load more data | |
*/ | |
public function loadMore(){ | |
$offset = $this->offset; | |
if(count($this->result) > 0){ | |
$offset = $this->offset + $this->limit; | |
} | |
return $this->api->queryLogs($this->query, $this->limit, $offset); | |
} | |
} | |
/** | |
* Swift MTA Api wrapper | |
*/ | |
class SwiftMTA { | |
/** | |
* Create new api connection instance | |
*/ | |
public function __construct($host, $username, $password, $https = true) { | |
$this->host = $host; | |
$this->username = $username; | |
$this->password = $password; | |
$this->https = $https; | |
} | |
private function getContext($payload){ | |
$auth = base64_encode("{$this->username}:{$this->password}"); | |
$options = [ | |
'http' => [ | |
'method' => 'POST', | |
'header' => [ | |
"Authorization: Basic $auth", | |
"Content-type: application/json" | |
], | |
'content' => $payload | |
] | |
]; | |
$context = stream_context_create($options); | |
return $context; | |
} | |
/** | |
* Query logs | |
*/ | |
public function queryLogs($query, $limit = 100, $offset = 0){ | |
$protocol = $this->https ? 'https' : 'http'; | |
$uri = "{$protocol}://{$this->host}/api/v1/logs/query.json?limit={$limit}&offset={$offset}"; | |
$context = $this->getContext(json_encode($query)); | |
$response = file_get_contents($uri, false, $context ); | |
$result = json_decode($response); | |
return new SwiftMTALogsCursor($this, $query, $result, $limit, $offset); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment