/** * logs failed request api call including options and error message * @param string $scope * @param string $name * @param array $opts * @param string $message */ public function errorLog($scope,$name,$opts,$message) { // stop measure the response time $this->stop(); // get filename, return if disabled if(!($filename = $this->filename('error_log'))) return false; // remove htmltags, newlines, and coherent blanks $message = preg_replace("/(\s\s+|[\n\r])/",' ',strip_tags($message)); // log file_put_contents($filename, sprintf($this->options['error_format'], date('r'), (Pheal_Config::getInstance()->http_post?'POST':'GET'), $this->responseTime, $this->formatUrl($scope,$name,$opts), $message ), FILE_APPEND ); }
/** * method will do the actual http call using file() * remember: on some installations, file_get_contents(url) might not be available due to * restrictions via allow_url_fopen * @param String $url url beeing requested * @param array $opts an array of query paramters * @return string raw http response */ public static function request_http_file($url,$opts) { $options = array(); // set custom user agent if(($http_user_agent = Pheal_Config::getInstance()->http_user_agent) != false) $options['http']['user_agent'] = $http_user_agent; // set custom http timeout if(($http_timeout = Pheal_Config::getInstance()->http_timeout) != false) $options['http']['timeout'] = $http_timeout; // use post for params if(count($opts) && Pheal_Config::getInstance()->http_post) { $options['http']['method'] = 'POST'; $options['http']['content'] = http_build_query($opts); } // else build url parameters elseif(count($opts)) { $url .= "?" . http_build_query($opts); } // set track errors. needed for $php_errormsg $oldTrackErrors = ini_get('track_errors'); ini_set('track_errors', true); // create context with options and request api call // suppress the 'warning' message which we'll catch later with $php_errormsg if(count($options)) { $context = stream_context_create($options); $result = @file_get_contents($url, false, $context); } else { $result = @file_get_contents($url); } // throw error if($result === false) { $message = ($php_errormsg ? $php_errormsg : 'HTTP Request Failed'); // set track_errors back to the old value ini_set('track_errors',$oldTrackErrors); throw new Exception($message); // return result } else { // set track_errors back to the old value ini_set('track_errors',$oldTrackErrors); return $result; } }