apiKey = $apiKey;
if ($secretKey)
$this->secretKey = $secretKey;
$this->apiUrl = (($this->secure) ? 'https' : 'http') . '://api.mailjet.com/v3/REST';
}
public function curl_setopt_custom_postfields($curl_handle, $postfields, $headers = null) {
$algos = hash_algos();
$hashAlgo = null;
foreach (array('sha1', 'md5') as $preferred) {
if (in_array($preferred, $algos)) {
$hashAlgo = $preferred;
break;
}
}
if ($hashAlgo === null)
list($hashAlgo) = $algos;
$boundary =
'----------------------------' .
substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12);
$body = array();
$crlf = "\r\n";
$fields = array();
foreach ($postfields as $key => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$fields[] = array($key, $v);
}
}
else
$fields[] = array($key, $value);
}
foreach ($fields as $field) {
list($key, $value) = $field;
if (strpos($value, '@') === 0) {
preg_match('/^@(.*?)$/', $value, $matches);
list($dummy, $filename) = $matches;
$body[] = '--' . $boundary;
$body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"';
$body[] = 'Content-Type: application/octet-stream';
$body[] = '';
$body[] = file_get_contents($filename);
}
else {
$body[] = '--' . $boundary;
$body[] = 'Content-Disposition: form-data; name="' . $key . '"';
$body[] = '';
$body[] = $value;
}
}
$body[] = '--' . $boundary . '--';
$body[] = '';
$contentType = 'multipart/form-data; boundary=' . $boundary;
$content = join($crlf, $body);
$contentLength = strlen($content);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array(
'Content-Length: ' . $contentLength,
'Expect: 100-continue',
'Content-Type: ' . $contentType,
));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content);
}
public function __call($resource, $args)
{
# Parameters array
$params = (sizeof($args) > 0) ? $args[0] : array();
# Request method, GET by default
if (isset($params["method"])) {
$request = strtoupper($params["method"]);
unset($params['method']);
}
else
$request = 'GET';
# Request ID, empty by default
$id = isset($params["ID"]) ? $params["ID"] : '';
# Using SendAPI without the to parameter but with cc AND/OR bcc
if ($resource == "sendEmail" && (empty($params["to"]) && (!empty($params["cc"]) || !empty($params["bcc"]))))
$params["to"] = "mailjet@example.org";
if ($id == '')
{
# Request Unique field, empty by default
$unique = isset($params["unique"]) ? $params["unique"] : '';
# Make request
$result = $this->sendRequest($resource, $params, $request, $unique);
}
else
{
# Make request
$result = $this->sendRequest($resource, $params, $request, $id);
}
# Return result
$return = ($result === true) ? $this->_response : false;
if ($this->debug == 2 || ($this->debug == 1 && $return == false)) {
$this->debug();
}
return $return;
}
public function requestUrlBuilder($resource, $params = array(), $request, $id)
{
if ($resource == "sendEmail") {
$this->call_url = "https://api.mailjet.com/v3/send/message";
} elseif ($resource == "managemanycontacts") {
$this->call_url = $this->apiUrl . '/contact/' . $resource;
} else {
$this->call_url = $this->apiUrl . '/' . $resource;
}
$JobID = '';
if (isset($params['JobID'])) {
$JobID = $params['JobID'];
unset($params['JobID']);
}
if ($request == "GET" && count($params) > 0)
$this->call_url .= '?';
foreach ($params as $key => $value) {
if ($request == "GET")
{
$query_string[$key] = $key . '=' . $value;
$this->call_url .= $query_string[$key] . '&';
}
}
if ($request == "GET" && count($params) > 0) {
$this->call_url = substr($this->call_url, 0, -1);
}
if ($JobID != '') {
$this->call_url .= '/' . $JobID;
}
if ($request == "VIEW" || $request == "DELETE" || $request == "PUT")
if ($id != '')
$this->call_url .= '/' . $id;
return $this->call_url;
}
public function sendRequest($resource = false, $params = array(), $request = "GET", $id = '')
{
# Method
$this->_method = $resource;
$this->_request = $request;
# Build request URL
$url = $this->requestUrlBuilder($resource, $params, $request, $id);
# Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->apiKey . ':' . $this->secretKey);
$this->_request_post = false;
if (($request == 'POST') || ($request == 'PUT')):
curl_setopt($curl_handle, CURLOPT_POST, 1);
if ($this->debug == 2)
var_dump($params);
if ($resource == "sendEmail")
$this->curl_setopt_custom_postfields($curl_handle, $params);
else
{
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($params,JSON_UNESCAPED_SLASHES));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
}
$this->_request_post = $params;
endif;
if ($request == 'DELETE') {
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
}
if ($request == 'PUT') {
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "PUT");
}
$buffer = curl_exec($curl_handle);
if ($this->debug == 2)
var_dump($buffer);
# Response code
$this->_response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
# Close curl process
curl_close($curl_handle);
# Return response
$this->_response = json_decode($buffer);
if ($request == 'POST')
return ($this->_response_code == 201) ? true : false;
if ($request == 'DELETE')
return ($this->_response_code == 204) ? true : false;
return ($this->_response_code == 200) ? true : false;
}
public function debug()
{
echo '';
echo '
';
if (isset($this->_response_code)):
if (($this->_response_code == 200) || ($this->_response_code == 201) || ($this->_response_code == 204)):
echo '
';
echo 'Success | |
';
echo 'Status code | ' . $this->_response_code . ' |
';
if (isset($this->_response)):
echo 'Response | ' . utf8_decode(print_r($this->_response, 1)) . ' |
';
endif;
echo '
';
elseif ($this->_response_code == 304):
echo '
';
echo 'Error | |
';
echo 'Error no | ' . $this->_response_code . ' |
';
echo 'Message | Not Modified |
';
echo '
';
else:
echo '
';
echo 'Error | |
';
echo 'Error no | ' . $this->_response_code . ' |
';
if (isset($this->_response)):
if (is_array($this->_response) OR is_object($this->_response)):
echo 'Status | ' . print_r($this->_response, true) . ' |
';
else:
echo 'Status | ' . $this->_response . ' |
';
endif;
endif;
echo '
';
endif;
endif;
$call_url = parse_url($this->call_url);
echo '
';
echo 'API config | |
';
echo 'Protocole | ' . $call_url['scheme'] . ' |
';
echo 'Host | ' . $call_url['host'] . ' |
';
echo 'Version | ' . $this->version . ' |
';
echo '
';
echo '
';
echo 'Call infos | |
';
echo 'Resource | ' . $this->_method . ' |
';
echo 'Request type | ' . $this->_request . ' |
';
echo 'Get Arguments | ';
if (isset($call_url['query'])) {
$args = explode("&", $call_url['query']);
foreach ($args as $arg) {
$arg = explode("=", $arg);
echo '' . $arg[0] . ' = ' . $arg[1] . ' ';
}
}
echo ' |
';
if ($this->_request_post) {
echo 'Post Arguments | ';
foreach ($this->_request_post as $k => $v) {
echo $k . ' = ' . $v . ' ';
}
echo ' |
';
}
echo 'Call url | ' . $this->call_url . ' |
';
echo '
';
echo '
';
}
}