apiKey =$apiKey;
if( $secretKey ) $this->secretKey =$secretKey;
$this->apiUrl = (($this->secure) ? 'https' : 'http').'://api.mailjet.com/'.$this->version.'';
}
public function __call($method,$args) {
# params
$params = (sizeof($args) > 0) ? $args[0] : array();
# request method
$request = isset($params["method"]) ? strtoupper($params["method"]) : 'GET';
# unset useless params
if(isset($params["method"])) unset($params["method"]);
# Make request
$result = $this->sendRequest($method,$params,$request);
# Return result
$return = ($result === true) ? $this->_response : false;
if( $this->debug == 2 || ( $this->debug == 1 && $return == false ) ){
$this->debug();
}
return $return;
}
public function requestUrlBuilder($method,$params=array(),$request) {
$query_string = array('output'=>'output='.$this->output);
foreach($params as $key=>$value) {
if($request == "GET" || in_array($key,array('apikey','output'))) $query_string[$key] = $key.'='.urlencode($value);
if($key == "output") $this->output = $value;
}
$this->call_url = $this->apiUrl.'/'.$method.'/?'.join('&',$query_string);
return $this->call_url;
}
public function sendRequest($method = false,$params=array(),$request="GET") {
# Method
$this->_method = $method;
$this->_request = $request;
# Build request URL
$url = $this->requestUrlBuilder($method,$params,$request);
# 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') :
curl_setopt($curl_handle, CURLOPT_POST, count($params));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
$this->_request_post = $params;
endif;
$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);
# RESPONSE
$this->_response = ($this->output == 'json') ? json_decode($buffer) : $buffer;
return ($this->_response_code == 200) ? true : false;
}
public function debug() {
echo '';
echo '
';
if(isset($this->_response_code)) :
if($this->_response_code == 200) :
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 'Method | '.$this->_method.' |
';
echo 'Request type | '.$this->_request.' |
';
echo 'Get Arguments | ';
$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 '
';
}
}