From 0adb7af9a03a16fd45d900c5e15e8d386c454778 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Fri, 24 Jun 2016 15:43:20 +0200 Subject: [PATCH] Allow a quickHead request, will only return headers --- lib/httpclient.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/httpclient.php b/lib/httpclient.php index 1e399bd83e..04a365274d 100644 --- a/lib/httpclient.php +++ b/lib/httpclient.php @@ -206,6 +206,29 @@ class HTTPClient extends HTTP_Request2 return $data; } + /** + * If you want an Accept header, put it in $headers + */ + public static function quickHead($url, array $params=array(), array $headers=array()) + { + if (!empty($params)) { + $params = http_build_query($params, null, '&'); + if (strpos($url, '?') === false) { + $url .= '?' . $params; + } else { + $url .= '&' . $params; + } + } + + $client = new HTTPClient(); + $response = $client->head($url, $headers); + if (!$response->isOk()) { + // TRANS: Exception. %s is the URL we tried to GET. + throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus()); + } + return $response->getHeader(); + } + /** * Convenience function to run a GET request. *