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 ''; echo ''; if(isset($this->_response)) : echo ''; endif; echo '
Success
Status code'.$this->_response_code.'
Response
'.utf8_decode(print_r($this->_response,1)).'
'; elseif($this->_response_code == 304) : echo ''; echo ''; echo ''; echo ''; echo '
Error
Error no'.$this->_response_code.'
MessageNot Modified
'; else : echo ''; echo ''; echo ''; if(isset($this->_response)) : if( is_array($this->_response) OR is_object($this->_response) ): echo ''; else: echo ''; endif; endif; echo '
Error
Error no'.$this->_response_code.'
Status
'.print_r($this->_response,true).'
Status
'.$this->_response.'
'; endif; endif; $call_url = parse_url($this->call_url); echo ''; echo ''; echo ''; echo ''; echo ''; echo '
API config
Protocole'.$call_url['scheme'].'
Host'.$call_url['host'].'
Version'.$this->version.'
'; echo ''; echo ''; echo ''; echo ''; echo ''; if($this->_request_post){ echo ''; } echo ''; echo '
Call infos
Method'.$this->_method.'
Request type'.$this->_request.'
Get Arguments'; $args = explode("&",$call_url['query']); foreach($args as $arg) { $arg = explode("=",$arg); echo ''.$arg[0].' = '.$arg[1].'
'; } echo '
Post Arguments'; foreach($this->_request_post as $k=>$v) { echo $k.' = '.$v.'
'; } echo '
Call url'.$this->call_url.'
'; echo '
'; } }