From dae103559df6dd2c58e4640d430e040e038b87f1 Mon Sep 17 00:00:00 2001 From: Curtis Branum Date: Wed, 17 Aug 2016 13:36:18 -0500 Subject: [PATCH] Add support for HTTP HEAD method, getting http response headers Supports new HEAD endpoint for annotations --- src/Wheniwork.php | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Wheniwork.php b/src/Wheniwork.php index 5e689b4..08eb9ae 100644 --- a/src/Wheniwork.php +++ b/src/Wheniwork.php @@ -27,6 +27,7 @@ class Wheniwork const METHOD_PUT = 'put'; const METHOD_PATCH = 'patch'; const METHOD_DELETE = 'delete'; + const METHOD_HEAD = 'head'; private $api_token; private $api_endpoint = 'https://api.wheniwork.com/2'; @@ -190,6 +191,19 @@ public function delete($method, $params = [], $headers = []) return $this->makeRequest($method, self::METHOD_DELETE, $params, $headers); } + /** + * Discover object permissions. Must include the ID. + * + * @param string $method The API method to call, e.g. '/annotations/1' + * @param array $params An array of arguments to pass to the method. + * @param array $headers Array of custom headers to be passed + * @return array Object of json decoded API response. + */ + public function head($method, $params = [], $headers = []) + { + return $this->makeRequest($method, self::METHOD_HEAD, $params, $headers); + } + /** * Performs the underlying HTTP request. Exciting stuff happening here. Not really. @@ -223,11 +237,14 @@ private function makeRequest($method, $request, $params = [], $headers = []) foreach ($headers as $k => $v) { $headers_data[] = $k . ': ' . $v; } - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data); + $is_head = $request === self::METHOD_HEAD; + + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($request)); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_HEADER, $is_head); + curl_setopt($ch, CURLOPT_NOBODY, $is_head); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); @@ -239,6 +256,11 @@ private function makeRequest($method, $request, $params = [], $headers = []) $result = curl_exec($ch); curl_close($ch); + if ($result && $is_head) { + // we are just returning the http response headers, not the body + return $this->parseResponseHeaders($result); + } + return $result ? json_decode($result) : false; } @@ -267,4 +289,28 @@ public static function login($key, $email, $password) return $response; } + + /** + * Parse a set of HTTP headers + * + * @param string The php headers to be parsed + */ + private function parseResponseHeaders($raw_headers) + { + $headers = array_map('trim', explode("\n", $raw_headers)); + $output = []; + + if ('HTTP' === substr($headers[0], 0, 4)) { + list(, $output['status'], $output['status_text']) = explode(' ', $headers[0]); + unset($headers[0]); + } + + foreach ($headers as $v) { + $h = preg_split('/:\s*/', $v); + if (!empty($h[0]) && !empty($h[1])) { + $output[strtolower($h[0])] = $h[1]; + } + } + return (object) $output; + } }