Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/Wheniwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}