From 7a62129e0e6f312119bbd243ebe9e4c75bccff46 Mon Sep 17 00:00:00 2001 From: Sebastian Buckpesch Date: Fri, 24 Apr 2015 17:25:29 +0200 Subject: [PATCH] JSON request data support Support for POSTing JSON data and query parameters. E.g. POST /_all/_search?search_type=count HTTP/1.1 Host: localhost:9200 Content-Type: application/json;charset=UTF-8 {"facets":{"1":{"date_histogram":{"field":"@timestamp","interval":"1h"},"global":true,"facet_filter" :{"fquery":{"query":{"filtered":{"query":{"query_string":{"query":"action:user_app_started"}},"filter" :{"bool":{"must":[{"range":{"@timestamp":{"from":1429281606743,"to":1429886406743}}}]}}}}}}},"2":{"date_histogram" :{"field":"@timestamp","interval":"1h"},"global":true,"facet_filter":{"fquery":{"query":{"filtered": {"query":{"query_string":{"query":"action:user_door_open"}},"filter":{"bool":{"must":[{"range":{"@timestamp" :{"from":1429281606744,"to":1429886406744}}}]}}}}}}}},"size":0} --- src/CurlWrapper.php | 89 ++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/src/CurlWrapper.php b/src/CurlWrapper.php index 3a0f636..d726112 100644 --- a/src/CurlWrapper.php +++ b/src/CurlWrapper.php @@ -1,5 +1,5 @@ 0) + */ + function __construct($requestHeaders = array(), $options = array()) + { + if ( count($requestHeaders) > 0 && is_array($requestHeaders) ) { + $this->requestHeaders = $requestHeaders; + $this->requestHeaders[] = "User-Agent: " . self::USER_AGENT; + } else { + $this->requestHeaders = ["User-Agent: " . self::USER_AGENT]; + } + if ( count($options) > 0 && is_array($options) ) { + $this->options = $options; + } + } + + public function doGet($url, $queryString = NULL) { $s = curl_init(); @@ -17,14 +38,19 @@ public function doGet($url, $queryString = NULL) return $this->doMethod($s); } - public function doPost($url, $queryString = NULL) + public function doPost($url, $queryString = NULL, $responseBody = NULL) { $s = curl_init(); - curl_setopt($s, CURLOPT_URL, $url); curl_setopt($s, CURLOPT_POST, TRUE); - if (!is_null($queryString)) { + if (!is_null($responseBody)) { + curl_setopt($s, CURLOPT_POSTFIELDS, $responseBody); + $url .= "?" . $queryString; + $this->responseHeaders[] = 'Content-Length: ' . strlen($responseBody); + } else if (!is_null($queryString)) { curl_setopt($s, CURLOPT_POSTFIELDS, parse_str($queryString)); } + curl_setopt($s, CURLOPT_URL, $url); + return $this->doMethod($s); } @@ -41,6 +67,18 @@ public function doPut($url, $queryString = NULL) return $this->doMethod($s); } + public function doOptions($url, $queryString = NULL) + { + $s = curl_init(); + curl_setopt($s, CURLOPT_URL, $url); + curl_setopt($s, CURLOPT_CUSTOMREQUEST, 'OPTIONS'); + if (!is_null($queryString)) { + curl_setopt($s, CURLOPT_POSTFIELDS, parse_str($queryString)); + } + + return $this->doMethod($s); + } + public function doDelete($url, $queryString = NULL) { $s = curl_init(); @@ -55,16 +93,22 @@ public function doDelete($url, $queryString = NULL) private function doMethod($s) { - $headers = ["User-Agent: " . self::USER_AGENT]; - curl_setopt($s, CURLOPT_HTTPHEADER, $headers); + curl_setopt($s, CURLOPT_HTTPHEADER, $this->requestHeaders); curl_setopt($s, CURLOPT_HEADER, TRUE); + curl_setopt($s, CURLINFO_HEADER_OUT, FALSE); curl_setopt($s, CURLOPT_RETURNTRANSFER, TRUE); - $out = curl_exec($s); - $this->status = curl_getinfo($s, CURLINFO_HTTP_CODE); - $this->responseHeaders = curl_getinfo($s, CURLINFO_HEADER_OUT); + foreach ($this->options as $option => $value) + { + curl_setopt($s, $option, $value); + } + $out = curl_exec($s); + $this->status = curl_getinfo($s, CURLINFO_HTTP_CODE); + $this->responseHeaders = curl_getinfo($s, CURLINFO_HEADER_OUT); + $this->responseHeaderSize = curl_getinfo($s, CURLINFO_HEADER_SIZE); curl_close($s); list($this->responseHeaders, $content) = $this->decodeOut($out); + if ($this->status != self::HTTP_OK) { throw new \Exception("http error: {$this->status}", $this->status); } @@ -74,25 +118,18 @@ private function doMethod($s) private function decodeOut($out) { - // It should be a fancy way to do that :( - $headersFinished = FALSE; - $headers = $content = []; - $data = explode("\n", $out); - foreach ($data as $line) { - if (trim($line) == '') { - $headersFinished = TRUE; - } else { - if ($headersFinished === FALSE && strpos($line, ':') > 0) { - $headers[] = $line; - } - - if ($headersFinished) { - $content[] = $line; - } + // Split content and headers via header-size parameter + $headerString = substr($out, 0, $this->responseHeaderSize); + $content = trim(substr($out, $this->responseHeaderSize)); + $headers = array(); + foreach (explode("\n", $headerString) as $key => $value) + { + if (trim($value) !== '') { + $headers[] = trim($value); } } - return [$headers, implode("\n", $content)]; + return [$headers, $content]; } public function getStatus() @@ -104,4 +141,4 @@ public function getHeaders() { return $this->responseHeaders; } -} \ No newline at end of file +}