diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2016-03-19 09:00:46 -0700 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2016-03-19 09:00:46 -0700 |
commit | 2d1091d3625d0a063880b6f6e616c00c6dd2547d (patch) | |
tree | fba60a70829f4c7d9a48def3e7cd677cd8d61acc /php_http_client/client.php | |
parent | a59e62f9358a46a0c7d4315cdf1db9fc72120d5f (diff) | |
download | php-http-client-2d1091d3625d0a063880b6f6e616c00c6dd2547d.zip php-http-client-2d1091d3625d0a063880b6f6e616c00c6dd2547d.tar.gz php-http-client-2d1091d3625d0a063880b6f6e616c00c6dd2547d.tar.bz2 |
HTTP Client Added
Diffstat (limited to 'php_http_client/client.php')
-rw-r--r-- | php_http_client/client.php | 194 |
1 files changed, 111 insertions, 83 deletions
diff --git a/php_http_client/client.php b/php_http_client/client.php index 34775c9..cc79505 100644 --- a/php_http_client/client.php +++ b/php_http_client/client.php @@ -1,7 +1,37 @@ <?php +class Response { + + function __construct($status_code = null, $response_body = null, $response_headers = null){ + $this->_status_code = $status_code; + $this->_response_body = $response_body; + $this->_response_headers = $response_headers; + } + + /** + * @return: integer, status code of API call + */ + public function status_code() { + return $this->_status_code; + } + + /** + * @return: response from the API + */ + public function response_body() { + return $this->_response_body; + } + + /** + * @return: dict of response headers + */ + public function response_headers() { + return $this->_response_headers; + } +} + class Client { - function __construct($host, $request_headers = [], $version = null){ + function __construct($host, $request_headers = null, $version = null, $url_path = null){ /* @param host: Base URL for the api. (e.g. https://api.sendgrid.com) @type host: string @@ -14,25 +44,12 @@ class Client { */ $this->host = $host; - $this->request_headers = $request_headers; - $this->version = $version; - - # _count and _url_path keep track of the dynamically built url - $this->_count = 0; + $this->request_headers = ($request_headers ? $request_headers : []); + $this->_version = $version; + # _url_path keeps track of the dynamically built url + $this->_url_path = ($url_path ? $url_path : []); + # These are the supported HTTP verbs $this->_methods = ['delete', 'get', 'patch', 'post', 'put']; - - $this->_reset(); - } - - /* - Resets the URL builder, so you can make a fresh new dynamic call. - */ - private function _reset() { - $this->_count = 0; - $this->_url_path = []; - $this->_response = null; - $this->_response_body = null; - $this->_response_headers = null; } /** @@ -40,9 +57,13 @@ class Client { * @param name: The name of the method call * @type name: string */ - private function _add_to_url_path($name) { - $this->_url_path[$this->_count] = $name; - $this->_count += 1; + private function _build_client($name = null) { + if($name != null){ + array_push($this->_url_path, $name); + } + $url_path = $this->_url_path; + $this->_url_path = []; + return new Client($this->host, $this->request_headers, $this->_version, $url_path); } /** @@ -65,42 +86,23 @@ class Client { */ private function _build_url($query_params = null) { - $url = ''; - $count = 0; - - while ($count < count($this->_url_path)) { - $url += sprintf("/%s", $this->_url_path[$count]); - $count+=1; - } + $url = '/'.implode('/', $this->_url_path); if (isset($query_params)) { - $url_values = urlencode(asort($query_params)); + $url_values = http_build_query($query_params); $url = sprintf('%s?%s', $url, $url_values); } - + if (null != $this->_get_version()) { $url = $this->_build_versioned_url($url); } else { - $url = $this->host + url; + $url = sprintf('%s%s', $this->host, $url);; } return $url; } /** - * Build the API call's response - * :param response: The response object from the API call from urllib - * :type response: urllib.Request object - */ - private function _set_response($response) { - - // @todo fix this to handle the response from guzzle - - $this->_status_code = ""; - $this->_response_body = ""; - $this->_response_headers = ""; - } - /** * Build the headers for the request * @param request_headers: headers to set for the API call * @type response: dict @@ -118,19 +120,63 @@ class Client { * @type request: urllib.Request object * @return: */ - private function _make_request($request) { - //@todo make the call using guzzle, and fix the return value - - return null; + private function _make_request($method, $url, $request_body = null, $request_headers = null) { + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HEADER, 1); + switch($method){ + case 'get': + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); + break; + case 'post': + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); + $request_body = json_encode($request_body); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); + $content_length = array('Content-Length: ' . strlen($request_body)); + $this->request_headers = array_merge($this->request_headers, $content_length); + break; + case 'patch': + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); + $request_body = json_encode($request_body); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); + $content_length = array('Content-Length: ' . strlen($request_body)); + $this->request_headers = array_merge($this->request_headers, $content_length); + break; + case 'put': + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); + $request_body = json_encode($request_body); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request_body); + $content_length = array('Content-Length: ' . strlen($request_body)); + $this->request_headers = array_merge($this->request_headers, $content_length); + break; + case 'delete': + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); + break; + default: + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); + break; + } + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + if(isset($request_headers)){ + $this->request_headers = array_merge($this->request_headers, $request_headers); + } + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->request_headers); + $curl_response = curl_exec($curl); + $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $response_header = substr($curl_response, 0, $header_size); + $response_body = substr($curl_response, $header_size); + $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return new Response($status_code, $response_body, $response_header); } /** * Add variable values to the URL * (e.g. /your/api/{variable_value}/call) */ - public function _($value){ - $this->_add_to_url_path($value); - return $this; + public function _($name){ + return $this->_build_client($name); } /** @@ -138,38 +184,20 @@ class Client { * (e.g. client.name.name.method()) */ public function __call($name, $args){ - - // @todo handle args - + + if($name == 'version'){ + return version($name); + } + if (in_array($name, $this->_methods)) { - $this->_make_request(); - return $this; + $request_body = ($args ? $args[0] : null); + $query_params = ((count($args) >= 2) ? $args[1] : null); + $url = $this->_build_url($query_params); + $request_headers = ((count($args) == 3) ? $args[2] : null); + return $this->_make_request($name, $url, $request_body, $request_headers); } - - $this->_cache[$this->_count] = $name; - $this->_count = $this->_count + 1; - return $this; - } - - /** - * @return: integer, status code of API call - */ - public function status_code() { - return $this->_status_code; - } - - /** - * @return: response from the API - */ - public function response_body() { - return $this->_response_body; - } - - /** - * @return: dict of response headers - */ - public function response_headers() { - return $this->_response_headers; + + return $this->_($name); } /** @@ -177,7 +205,7 @@ class Client { */ public function version($version) { $this->_version = $version; - return $this; + return $this->_(null); } private function _get_version() { |