From 14aa9aa6919e4b70c59cd20722ac113c0ffeb45b Mon Sep 17 00:00:00 2001 From: thenetexperts Date: Fri, 24 Feb 2017 18:06:01 +0100 Subject: [PATCH] feat(HEAD): CURLOPT_NOBODY option for HEAD requests * When using HEAD requests set the appropriate curl header to not wait for a response body. See http://www.php.net/manual/en/function.curl-setopt.php on CURLOPT_NOBODY. * Add simple test for HEAD request. --- src/Unirest/Request.php | 3 +++ tests/Unirest/RequestTest.php | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index ba8b8c0..b49780d 100755 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -399,6 +399,9 @@ class Request if ($method === Method::POST) { curl_setopt(self::$handle, CURLOPT_POST, true); } else { + if ($method === Method::HEAD) { + curl_setopt(self::$handle, CURLOPT_NOBODY, true); + } curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method); } diff --git a/tests/Unirest/RequestTest.php b/tests/Unirest/RequestTest.php index d9b5633..a46d5a6 100644 --- a/tests/Unirest/RequestTest.php +++ b/tests/Unirest/RequestTest.php @@ -251,6 +251,16 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase $this->assertEquals('John', $response->body->queryString->name[1]); } + // HEAD + public function testHead() + { + $response = Request::head('http://mockbin.com/request?name=Mark', array( + 'Accept' => 'application/json' + )); + + $this->assertEquals(200, $response->code); + } + // POST public function testPost() {