diff --git a/public_html/deployment/classes/foremanapi.class.php b/public_html/deployment/classes/foremanapi.class.php index 31fd9ddc8204005a40258e695b49ba405409dd24..e0e9851efb1eeb08f65c5b5dbabc01ee053ed24c 100644 --- a/public_html/deployment/classes/foremanapi.class.php +++ b/public_html/deployment/classes/foremanapi.class.php @@ -114,8 +114,18 @@ class ForemanApi { ); + /** + * last request + * @var type + */ protected $_aRequest=array(); + /** + * last response + * @var type + */ + protected $_aResponse=array(); + // ---------------------------------------------------------------------- // constructor @@ -144,7 +154,9 @@ class ForemanApi { */ protected function log($sMessage, $sLevel = "info") { global $oCLog; - return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel); + if($oCLog && method_exists($oLog, 'add')){ + return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel); + } } /** @@ -228,6 +240,7 @@ class ForemanApi { if ($aRequest){ $this->_aRequest=$aRequest; } + $this->_aResponse=array(); $this->log(__FUNCTION__ . " start <pre>".print_r($this->_aRequest,1)."</pre>"); if (!function_exists("curl_init")) { die("ERROR: PHP CURL module is not installed."); @@ -262,11 +275,10 @@ class ForemanApi { $aReturn=array('info'=>curl_getinfo($ch), 'error'=>curl_error($ch)); curl_close($ch); $this->log(__FUNCTION__ . " status ".$aReturn['info']['http_code'].' '.$this->_aRequest['method']." $sFullUrl"); - $sHeader=substr($res, 0, $aReturn['info']['header_size']); $aReturn['header']=explode("\n", $sHeader); $aReturn['body']=str_replace($sHeader, "", $res); - + return $aReturn; } @@ -333,7 +345,7 @@ class ForemanApi { // check status $iStatuscode=$aReturn['info']['http_code']; if ($iStatuscode===0){ - $sStatus='wrong host'; + $sStatus='wrong host or no connect'; } if ($iStatuscode>=200 && $iStatuscode<400){ $sStatus='OK'; @@ -363,6 +375,7 @@ class ForemanApi { $aReturn['_OK']=$bOk; $aReturn['_status']=$sStatus; $this->_writeDebug(__FUNCTION__ . ' result of request <pre>'.print_r($aReturn,1).'</pre>'); + $this->_aResponse=$aReturn; return $aReturn; } @@ -512,5 +525,91 @@ class ForemanApi { return $this->makeRequest(); */ } + + // ---------------------------------------------------------------------- + // get response infos + // ---------------------------------------------------------------------- + + /** + * get curl info data from last response + * Array + ( + [url] => https://foreman/api/hostgroups/?&per_page=1000 + [content_type] => application/json; charset=utf-8 + [http_code] => 200 + [header_size] => 1034 + [request_size] => 218 + [filetime] => -1 + [ssl_verify_result] => 0 + [redirect_count] => 0 + [total_time] => 1.644417 + [namelookup_time] => 0.007198 + [connect_time] => 0.009012 + [pretransfer_time] => 0.0332 + [size_upload] => 0 + [size_download] => 119602 + [speed_download] => 72732 + [speed_upload] => 0 + [download_content_length] => -1 + [upload_content_length] => 0 + [starttransfer_time] => 1.642775 + [redirect_time] => 0 + [redirect_url] => + [primary_ip] => 10.0.2.10 + [certinfo] => Array + ( + ) + + [primary_port] => 443 + [local_ip] => 10.0.2.15 + [local_port] => 33906 + ) + * @param string $sKey get value of given key only + * @return any + */ + public function getResponseInfo($sKey=false){ + + if($sKey){ + return isset($this->_aResponse['info'][$sKey]) + ? $this->_aResponse['info'][$sKey] + : false + ; + } + return isset($this->_aResponse['info']) + ? $this->_aResponse['info'] + : false + ; + /* + return isset($sKey) && $sKey + ? isset($this->_aResponse['info'][$sKey]) + ? $this->_aResponse['info'][$sKey] + : false + : isset($this->_aResponse['info']) + ? $this->_aResponse['info'] + : false + ; + */ + } + /** + * get array of last http response header + * @return array + */ + public function getResponseHeader(){ + return isset($this->_aResponse['header']) + ? $this->_aResponse['header'] + : false + ; + } + /** + * get status of last request + * @return string + */ + public function getResponseStatus(){ + // print_r($this->_aResponse); die(); + return isset($this->_aResponse['_status']) + ? $this->_aResponse['_status'] + : false + ; + } }