From 68f4193bcca3bfe315f1140213eb585a659e5ee5 Mon Sep 17 00:00:00 2001 From: theStormWinter Date: Thu, 30 Jan 2020 18:33:19 +0100 Subject: [PATCH 1/5] Added methods to add or remove uri parameters --- src/Httpful/Request.php | 74 +++++++++++++++++++++++++- tests/Httpful/HttpfulTest.php | 98 ++++++++++++++++++++++++++++------- 2 files changed, 153 insertions(+), 19 deletions(-) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index c04d230..01a8169 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -221,8 +221,10 @@ public function doNotFollowRedirects() */ public function send() { - if (!$this->hasBeenInitialized()) + $this->processUriParams(); + if (!$this->hasBeenInitialized()) { $this->_curlPrep(); + } $result = curl_exec($this->_ch); @@ -247,6 +249,7 @@ public function sendIt() public function uri($uri) { $this->uri = $uri; + return $this; } @@ -1150,6 +1153,7 @@ public static function get($uri, $mime = null) * @param string $uri optional uri to use * @param string $mime expected * @return Response + * @throws ConnectionErrorException */ public static function getQuick($uri, $mime = null) { @@ -1221,4 +1225,72 @@ public static function options($uri) { return self::init(Http::OPTIONS)->uri($uri); } + + /** + * Adds parameter to uri + * @param string $name + * @param string $value + * @param bool $withEqualSign + * @return Request + */ + public function addUriParameter(string $name, ?string $value, bool $withEqualSign = true): Request + { + $this->uriParameters[$name] = $value; + if ($withEqualSign == false) { + $this->uriParametersWithoutEqualSign[$name] = $withEqualSign; + } + + return $this; + } + + /** + * Removes parameter from uri + * @param string $name + * @return Request + */ + public function removeUriParameter(string $name): Request + { + $this->removedUriParameters[] = $name; + unset($this->uriParameters[$name]); + + return $this; + } + + /** + * Add or remove parameters to/from uri. This method is automatically called in send request + */ + public function processUriParams(): void + { + $paramsInUri = []; + $uri = $this->uri; + $paramsToUri = []; + $explodedUri = explode('?', $uri, 2); + if (count($explodedUri) > 1) { + [$uri, $params] = $explodedUri; + $explodedParams = explode('&', $params); + foreach ($explodedParams as $explodedParam) { + $explodedValues = explode('=', $explodedParam, 2); + if (count($explodedValues) == 1) { + $this->uriParametersWithoutEqualSign[$explodedValues[0]] = false; + $paramsInUri[$explodedValues[0]] = null; + continue; + } + [$name, $value] = $explodedValues; + $paramsInUri[$name] = $value; + } + } + $parameters = array_merge($paramsInUri, $this->uriParameters); + foreach ($parameters as $key => $parameter) { + if (in_array($key, $this->removedUriParameters) === false) { + $equalSign = array_key_exists($key, $this->uriParametersWithoutEqualSign) ? '' : '='; + $paramsToUri[] = $key.$equalSign.$parameter; + } + } + if (count($paramsToUri) > 0) { + $this->uri = $uri.'?'.implode('&', $paramsToUri); + + return; + } + $this->uri = $uri; + } } diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 062973e..e1fc314 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -23,9 +23,10 @@ class HttpfulTest extends \PHPUnit\Framework\TestCase { - const TEST_SERVER = TEST_SERVER; - const TEST_URL = 'http://127.0.0.1:8008'; - const TEST_URL_400 = 'http://127.0.0.1:8008/400'; + const TEST_SERVER = TEST_SERVER; + const TEST_URL = 'http://127.0.0.1:8008'; + const TEST_URL_400 = 'http://127.0.0.1:8008/400'; + const TEST_URL_WITH_PARAMS = self::TEST_URL.'?paramName1=paramValue1¶mName2=paramValue2'; const SAMPLE_JSON_HEADER = "HTTP/1.1 200 OK @@ -70,6 +71,14 @@ class HttpfulTest extends \PHPUnit\Framework\TestCase Transfer-Encoding: chunked X-My-Header:Value1 X-My-Header:Value2\r\n"; + const PARAM_NAME_1 = 'paramName1'; + const PARAM_NAME_2 = 'paramName2'; + const PARAM_NAME_3 = 'paramName3'; + const PARAM_VALUE_1 = 'paramValue1'; + const PARAM_VALUE_2 = 'paramValue2'; + const PARAM_VALUE_3 = 'paramValue3'; + const NEW_PARAM_VALUE_1 = 'newParamValue1'; + const PARAM_NAME_WITHOUT_VALUE = 'paramNameWithoutValue'; function testInit() { @@ -608,21 +617,74 @@ public function testParseJSON() $this->fail('Expected an exception to be thrown due to invalid json'); } - // /** - // * Skeleton for testing against the 5.4 baked in server - // */ - // public function testLocalServer() - // { - // if (!defined('WITHOUT_SERVER') || (defined('WITHOUT_SERVER') && !WITHOUT_SERVER)) { - // // PHP test server seems to always set content type to application/octet-stream - // // so force parsing as JSON here - // Httpful::register('application/octet-stream', new \Httpful\Handlers\JsonHandler()); - // $response = Request::get(TEST_SERVER . '/test.json') - // ->sendsAndExpects(MIME::JSON); - // $response->send(); - // $this->assertTrue(...); - // } - // } + public function testUriWithParams(): void + { + // Test with params in uri + $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request->processUriParams(); + $this->assertSame(self::TEST_URL_WITH_PARAMS, $request->uri); + // Test with params in uri and add new param + $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request->addUriParameter(self::PARAM_NAME_3, self::PARAM_VALUE_3); + $request->processUriParams(); + $this->assertSame(self::TEST_URL_WITH_PARAMS.'&'.self::PARAM_NAME_3.'='.self::PARAM_VALUE_3, $request->uri); + // Test remove params from uri + $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request->removeUriParameter(self::PARAM_NAME_1); + $request->removeUriParameter(self::PARAM_NAME_2); + $request->processUriParams(); + $this->assertSame(self::TEST_URL, $request->uri); + // Test add new param to uri + $request = Request::get(self::TEST_URL); + $request->addUriParameter(self::PARAM_NAME_1, self::PARAM_VALUE_1); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'='.self::PARAM_VALUE_1, $request->uri); + // Test set new value of param + $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request->addUriParameter(self::PARAM_NAME_1, self::NEW_PARAM_VALUE_1); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'='.self::NEW_PARAM_VALUE_1.'&'.self::PARAM_NAME_2.'='.self::PARAM_VALUE_2, $request->uri); + // Test remove one param + $request->removeUriParameter(self::PARAM_NAME_1); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_2.'='.self::PARAM_VALUE_2, $request->uri); + // Test add param with null value + $request = Request::get(self::TEST_URL); + $request->addUriParameter(self::PARAM_NAME_1, null); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'=', $request->uri); + // Test process uri param without value + $request = Request::get(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE.''); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE, $request->uri); + // Test add param without value + $request = Request::get(self::TEST_URL); + $request->addUriParameter(self::PARAM_NAME_WITHOUT_VALUE, null, false); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'. self::PARAM_NAME_WITHOUT_VALUE, $request->uri); + // Test add mixed params + $request = Request::get(self::TEST_URL); + $request->addUriParameter(self::PARAM_NAME_WITHOUT_VALUE, null, false); + $request->addUriParameter(self::PARAM_NAME_1, self::PARAM_VALUE_1); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE.'&'.self::PARAM_NAME_1.'='. self::PARAM_VALUE_1, $request->uri); + } + + // /** + // * Skeleton for testing against the 5.4 baked in server + // */ + // public function testLocalServer() + // { + // if (!defined('WITHOUT_SERVER') || (defined('WITHOUT_SERVER') && !WITHOUT_SERVER)) { + // // PHP test server seems to always set content type to application/octet-stream + // // so force parsing as JSON here + // Httpful::register('application/octet-stream', new \Httpful\Handlers\JsonHandler()); + // $response = Request::get(TEST_SERVER . '/test.json') + // ->sendsAndExpects(MIME::JSON); + // $response->send(); + // $this->assertTrue(...); + // } + // } } class DemoMimeHandler extends \Httpful\Handlers\MimeHandlerAdapter From af73885ccc6c7de9247fab14512ac0d5929c6c19 Mon Sep 17 00:00:00 2001 From: theStormWinter Date: Thu, 30 Jan 2020 18:55:57 +0100 Subject: [PATCH 2/5] Repaired mistake with code cleanup --- src/Httpful/Request.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index 01a8169..6ac0a01 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -87,6 +87,13 @@ class Request // Template Request object private static $_template; + /** @var array */ + protected $uriParameters = []; + /** @var array */ + protected $removedUriParameters = []; + /** @var array */ + protected $uriParametersWithoutEqualSign = []; + /** * We made the constructor protected to force the factory style. This was * done to keep the syntax cleaner and better the support the idea of From 10f3dd0a1c9bf7b6cb5c2a6982569dc2f981fa3c Mon Sep 17 00:00:00 2001 From: SiViN Date: Tue, 4 Feb 2020 13:52:18 +0100 Subject: [PATCH 3/5] Request->hasBody() now return isset instead of is-empty --- src/Httpful/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Httpful/Response.php b/src/Httpful/Response.php index 09996b6..fa4a9ca 100644 --- a/src/Httpful/Response.php +++ b/src/Httpful/Response.php @@ -69,7 +69,7 @@ public function hasErrors() */ public function hasBody() { - return !empty($this->body); + return isset($this->body); } /** From 9af97f741dc9500684ae1f32010d72db1cc1b192 Mon Sep 17 00:00:00 2001 From: Jiri Zima Date: Mon, 23 Mar 2020 11:39:36 +0100 Subject: [PATCH 4/5] Uri params are now urlencoded --- src/Httpful/Request.php | 8 ++++---- tests/Httpful/HttpfulTest.php | 36 +++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index 6ac0a01..021833d 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -1242,9 +1242,9 @@ public static function options($uri) */ public function addUriParameter(string $name, ?string $value, bool $withEqualSign = true): Request { - $this->uriParameters[$name] = $value; + $this->uriParameters[urlencode($name)] = urlencode($value); if ($withEqualSign == false) { - $this->uriParametersWithoutEqualSign[$name] = $withEqualSign; + $this->uriParametersWithoutEqualSign[urlencode($name)] = $withEqualSign; } return $this; @@ -1257,8 +1257,8 @@ public function addUriParameter(string $name, ?string $value, bool $withEqualSig */ public function removeUriParameter(string $name): Request { - $this->removedUriParameters[] = $name; - unset($this->uriParameters[$name]); + $this->removedUriParameters[] = urlencode($name); + unset($this->uriParameters[urlencode($name)]); return $this; } diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index e1fc314..fb39154 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -26,7 +26,8 @@ class HttpfulTest extends \PHPUnit\Framework\TestCase const TEST_SERVER = TEST_SERVER; const TEST_URL = 'http://127.0.0.1:8008'; const TEST_URL_400 = 'http://127.0.0.1:8008/400'; - const TEST_URL_WITH_PARAMS = self::TEST_URL.'?paramName1=paramValue1¶mName2=paramValue2'; + const TEST_URL_PARAMS = '?paramName1=paramValue1¶mName2=paramValue2'; + const TEST_URL_PARAMS_ENCODED = '?paramName1=paramValue1¶mName2=paramValue2'; const SAMPLE_JSON_HEADER = "HTTP/1.1 200 OK @@ -620,16 +621,16 @@ public function testParseJSON() public function testUriWithParams(): void { // Test with params in uri - $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request = Request::get(self::TEST_URL.self::TEST_URL_PARAMS); $request->processUriParams(); - $this->assertSame(self::TEST_URL_WITH_PARAMS, $request->uri); + $this->assertSame(self::TEST_URL.self::TEST_URL_PARAMS_ENCODED, $request->uri); // Test with params in uri and add new param - $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request = Request::get(self::TEST_URL.self::TEST_URL_PARAMS); $request->addUriParameter(self::PARAM_NAME_3, self::PARAM_VALUE_3); $request->processUriParams(); - $this->assertSame(self::TEST_URL_WITH_PARAMS.'&'.self::PARAM_NAME_3.'='.self::PARAM_VALUE_3, $request->uri); + $this->assertSame(self::TEST_URL.self::TEST_URL_PARAMS_ENCODED.'&'.urlencode(self::PARAM_NAME_3).'='.urlencode(self::PARAM_VALUE_3), $request->uri); // Test remove params from uri - $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request = Request::get(self::TEST_URL.self::TEST_URL_PARAMS); $request->removeUriParameter(self::PARAM_NAME_1); $request->removeUriParameter(self::PARAM_NAME_2); $request->processUriParams(); @@ -638,36 +639,43 @@ public function testUriWithParams(): void $request = Request::get(self::TEST_URL); $request->addUriParameter(self::PARAM_NAME_1, self::PARAM_VALUE_1); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'='.self::PARAM_VALUE_1, $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_1).'='.urlencode(self::PARAM_VALUE_1), $request->uri); // Test set new value of param - $request = Request::get(self::TEST_URL_WITH_PARAMS); + $request = Request::get(self::TEST_URL.self::TEST_URL_PARAMS); $request->addUriParameter(self::PARAM_NAME_1, self::NEW_PARAM_VALUE_1); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'='.self::NEW_PARAM_VALUE_1.'&'.self::PARAM_NAME_2.'='.self::PARAM_VALUE_2, $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_1).'='.urlencode(self::NEW_PARAM_VALUE_1).'&'.urlencode(self::PARAM_NAME_2).'='.urlencode(self::PARAM_VALUE_2), $request->uri); // Test remove one param + $request = Request::get(self::TEST_URL.self::TEST_URL_PARAMS); + $request->addUriParameter(self::PARAM_NAME_1, self::NEW_PARAM_VALUE_1); $request->removeUriParameter(self::PARAM_NAME_1); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_2.'='.self::PARAM_VALUE_2, $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_2).'='.urlencode(self::PARAM_VALUE_2), $request->uri); // Test add param with null value $request = Request::get(self::TEST_URL); $request->addUriParameter(self::PARAM_NAME_1, null); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_1.'=', $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_1).'=', $request->uri); // Test process uri param without value $request = Request::get(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE.''); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE, $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_WITHOUT_VALUE), $request->uri); // Test add param without value $request = Request::get(self::TEST_URL); $request->addUriParameter(self::PARAM_NAME_WITHOUT_VALUE, null, false); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'. self::PARAM_NAME_WITHOUT_VALUE, $request->uri); + $this->assertSame(self::TEST_URL.'?'. urlencode(self::PARAM_NAME_WITHOUT_VALUE), $request->uri); // Test add mixed params $request = Request::get(self::TEST_URL); $request->addUriParameter(self::PARAM_NAME_WITHOUT_VALUE, null, false); $request->addUriParameter(self::PARAM_NAME_1, self::PARAM_VALUE_1); $request->processUriParams(); - $this->assertSame(self::TEST_URL.'?'.self::PARAM_NAME_WITHOUT_VALUE.'&'.self::PARAM_NAME_1.'='. self::PARAM_VALUE_1, $request->uri); + $this->assertSame(self::TEST_URL.'?'.urlencode(self::PARAM_NAME_WITHOUT_VALUE).'&'.urlencode(self::PARAM_NAME_1).'='. urlencode(self::PARAM_VALUE_1), $request->uri); + // Test add request body object + $request = Request::get(self::TEST_URL); + $request->addUriParameter('filter', '{"limit":10,"skip":200}'); + $request->processUriParams(); + $this->assertSame(self::TEST_URL.'?filter=%7B%22limit%22%3A10%2C%22skip%22%3A200%7D', $request->uri); } // /** From 50ac03f2be75764eeab244388a7dc63295cd128e Mon Sep 17 00:00:00 2001 From: Jiri Zima Date: Tue, 21 Apr 2020 11:52:05 +0200 Subject: [PATCH 5/5] Private methods in Request class are now protected for extending --- src/Httpful/Request.php | 81 ++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/Httpful/Request.php b/src/Httpful/Request.php index 021833d..079f791 100644 --- a/src/Httpful/Request.php +++ b/src/Httpful/Request.php @@ -2,7 +2,14 @@ namespace Httpful; +use Closure; +use Exception; use Httpful\Exception\ConnectionErrorException; +use function curl_version; +use function implode; +use function parse_url; +use function preg_replace; + /** * Clean, simple class for sending HTTP requests @@ -85,7 +92,7 @@ class Request $_debug; // Template Request object - private static $_template; + protected static $template; /** @var array */ protected $uriParameters = []; @@ -125,7 +132,7 @@ protected function __construct($attrs = null) */ public static function ini(Request $template) { - self::$_template = clone $template; + self::$template = clone $template; } /** @@ -134,7 +141,7 @@ public static function ini(Request $template) */ public static function resetIni() { - self::_initializeDefaults(); + self::initializeDefaults(); } /** @@ -145,7 +152,7 @@ public static function resetIni() */ public static function d($attr) { - return isset($attr) ? self::$_template->$attr : self::$_template; + return isset($attr) ? self::$template->$attr : self::$template; } // Accessors @@ -651,11 +658,11 @@ public function withAutoParsing() /** * Use a custom function to parse the response. - * @param \Closure $callback Takes the raw body of + * @param Closure $callback Takes the raw body of * the http response and returns a mixed * @return Request */ - public function parseWith(\Closure $callback) + public function parseWith(Closure $callback) { $this->parse_callback = $callback; return $this; @@ -663,10 +670,10 @@ public function parseWith(\Closure $callback) /** * @see Request::parseResponsesWith() - * @param \Closure $callback + * @param Closure $callback * @return Request */ - public function parseResponsesWith(\Closure $callback) + public function parseResponsesWith(Closure $callback) { return $this->parseWith($callback); } @@ -674,10 +681,10 @@ public function parseResponsesWith(\Closure $callback) /** * Callback called to handle HTTP errors. When nothing is set, defaults * to logging via `error_log` - * @param \Closure $callback (string $error) + * @param Closure $callback (string $error) * @return Request */ - public function whenError(\Closure $callback) + public function whenError(Closure $callback) { $this->error_callback = $callback; return $this; @@ -686,10 +693,10 @@ public function whenError(\Closure $callback) /** * Callback invoked after payload has been serialized but before * the request has been built. - * @param \Closure $callback (Request $request) + * @param Closure $callback (Request $request) * @return Request */ - public function beforeSend(\Closure $callback) + public function beforeSend(Closure $callback) { $this->send_callback = $callback; return $this; @@ -703,11 +710,11 @@ public function beforeSend(\Closure $callback) * 'application/json' would take precedence over the '*' callback. * * @param string $mime mime type we're registering - * @param \Closure $callback takes one argument, $payload, + * @param Closure $callback takes one argument, $payload, * which is the payload that we'll be * @return Request */ - public function registerPayloadSerializer($mime, \Closure $callback) + public function registerPayloadSerializer($mime, Closure $callback) { $this->payload_serializers[Mime::getFullMime($mime)] = $callback; return $this; @@ -715,10 +722,10 @@ public function registerPayloadSerializer($mime, \Closure $callback) /** * @see Request::registerPayloadSerializer() - * @param \Closure $callback + * @param Closure $callback * @return Request */ - public function serializePayloadWith(\Closure $callback) + public function serializePayloadWith(Closure $callback) { return $this->registerPayloadSerializer('*', $callback); } @@ -793,7 +800,7 @@ public function __call($method, $args) * Request instantiation), it promotes readability * and flexibility within the class. */ - private static function _initializeDefaults() + protected static function initializeDefaults() { // This is the only place you will // see this constructor syntax. It @@ -801,10 +808,10 @@ private static function _initializeDefaults() // recusion. Do not use this syntax elsewhere. // It goes against the whole readability // and transparency idea. - self::$_template = new Request(array('method' => Http::GET)); + self::$template = new Request(array('method' => Http::GET)); // This is more like it... - self::$_template + self::$template ->withoutStrictSSL(); } @@ -813,18 +820,18 @@ private static function _initializeDefaults() * Doesn't copy variables prefixed with _ * @return Request */ - private function _setDefaults() + protected function setDefaults() { - if (!isset(self::$_template)) - self::_initializeDefaults(); - foreach (self::$_template as $k=>$v) { + if (!isset(self::$template)) + self::initializeDefaults(); + foreach (self::$template as $k=> $v) { if ($k[0] != '_') $this->$k = $v; } return $this; } - private function _error($error) + private function error($error) { // TODO add in support for various Loggers that follow // PSR 3 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md @@ -849,12 +856,12 @@ public static function init($method = null, $mime = null) Bootstrap::init(); // Setup the default template if need be - if (!isset(self::$_template)) - self::_initializeDefaults(); + if (!isset(self::$template)) + self::initializeDefaults(); $request = new Request(); return $request - ->_setDefaults() + ->setDefaults() ->method($method) ->sendsType($mime) ->expectsType($mime); @@ -865,13 +872,13 @@ public static function init($method = null, $mime = null) * library cURL to set up the HTTP request. * Note: It does NOT actually send the request * @return Request - * @throws \Exception + * @throws Exception */ public function _curlPrep() { // Check for required stuff if (!isset($this->uri)) - throw new \Exception('Attempting to send a request before defining a URI endpoint.'); + throw new Exception('Attempting to send a request before defining a URI endpoint.'); if (isset($this->payload)) { $this->serialized_payload = $this->_serializePayload($this->payload); @@ -895,10 +902,10 @@ public function _curlPrep() if ($this->hasClientSideCert()) { if (!file_exists($this->client_key)) - throw new \Exception('Could not read Client Key'); + throw new Exception('Could not read Client Key'); if (!file_exists($this->client_cert)) - throw new \Exception('Could not read Client Certificate'); + throw new Exception('Could not read Client Certificate'); curl_setopt($ch, CURLOPT_SSLCERTTYPE, $this->client_encoding); curl_setopt($ch, CURLOPT_SSLKEYTYPE, $this->client_encoding); @@ -971,12 +978,12 @@ public function _curlPrep() $headers[] = "$header: $value"; } - $url = \parse_url($this->uri); + $url = parse_url($this->uri); $path = (isset($url['path']) ? $url['path'] : '/').(isset($url['query']) ? '?'.$url['query'] : ''); $this->raw_headers = "{$this->method} $path HTTP/1.1\r\n"; $host = (isset($url['host']) ? $url['host'] : 'localhost').(isset($url['port']) ? ':'.$url['port'] : ''); $this->raw_headers .= "Host: $host\r\n"; - $this->raw_headers .= \implode("\r\n", $headers); + $this->raw_headers .= implode("\r\n", $headers); $this->raw_headers .= "\r\n"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); @@ -1025,7 +1032,7 @@ public function isUpload() public function buildUserAgent() { $user_agent = 'User-Agent: Httpful/' . Httpful::VERSION . ' (cURL/'; - $curl = \curl_version(); + $curl = curl_version(); if (isset($curl['version'])) { $user_agent .= $curl['version']; @@ -1036,7 +1043,7 @@ public function buildUserAgent() $user_agent .= ' PHP/'. PHP_VERSION . ' (' . PHP_OS . ')'; if (isset($_SERVER['SERVER_SOFTWARE'])) { - $user_agent .= ' ' . \preg_replace('~PHP/[\d\.]+~U', '', + $user_agent .= ' ' . preg_replace('~PHP/[\d\.]+~U', '', $_SERVER['SERVER_SOFTWARE']); } else { if (isset($_SERVER['TERM_PROGRAM'])) { @@ -1065,7 +1072,7 @@ public function buildResponse($result) { if ($result === false) { if ($curlErrorNumber = curl_errno($this->_ch)) { $curlErrorString = curl_error($this->_ch); - $this->_error($curlErrorString); + $this->error($curlErrorString); $exception = new ConnectionErrorException('Unable to connect to "'.$this->uri.'": ' . $curlErrorNumber . ' ' . $curlErrorString); @@ -1076,7 +1083,7 @@ public function buildResponse($result) { throw $exception; } - $this->_error('Unable to connect to "'.$this->uri.'".'); + $this->error('Unable to connect to "'.$this->uri.'".'); throw new ConnectionErrorException('Unable to connect to "'.$this->uri.'".'); }