From a1ed45be55e1679428c7a7db7a447e879e22de56 Mon Sep 17 00:00:00 2001 From: cristianp6 Date: Fri, 3 Apr 2015 11:16:17 +0200 Subject: [PATCH 1/2] Fixed "Notice: Undefined variable: ch" on curl_setopt cookieFile --- src/Unirest/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index b95bb89..ff83782 100644 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -374,8 +374,8 @@ class Request } if (self::$cookieFile) { - curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile); - curl_setopt($ch, CURLOPT_COOKIEJAR, self::$cookieFile); + curl_setopt(self::$handle, CURLOPT_COOKIEFILE, self::$cookieFile); + curl_setopt(self::$handle, CURLOPT_COOKIEJAR, self::$cookieFile); } // supporting deprecated http auth method From 0293eb258ccb40cce21ed74a65a489781ee55999 Mon Sep 17 00:00:00 2001 From: cristianp6 Date: Fri, 3 Apr 2015 12:04:49 +0200 Subject: [PATCH 2/2] Add cookie string support, aka CURLOPT_COOKIE --- README.md | 6 ++++++ src/Unirest/Request.php | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 0d2e55a..7a5e779 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ $response = Unirest\Request::get("http://mockbin.com/request", null, null, "user ### Cookies +Set a cookie string to specify the contents of a cookie header. Multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red") + +```php +Unirest\Request::cookie($cookie) +``` + Set a cookie file path for enabling cookie reading and storing cookies across multiple sequence of requests. ```php diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index ff83782..32bbcbd 100644 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -7,6 +7,7 @@ use Unirest\Response; class Request { + private static $cookie = null; private static $cookieFile = null; private static $defaultHeaders = array(); private static $handle = null; @@ -103,6 +104,16 @@ class Request return self::defaultHeader('X-Mashape-Key', $key); } + /** + * Set a coockie string for enabling coockie handling + * + * @param string $cookie + */ + public static function cookie($cookie) + { + self::$cookie = $cookie; + } + /** * Set a coockie file path for enabling coockie handling * @@ -373,6 +384,10 @@ class Request curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout); } + if (self::$cookie) { + curl_setopt(self::$handle, CURLOPT_COOKIE, self::$cookie); + } + if (self::$cookieFile) { curl_setopt(self::$handle, CURLOPT_COOKIEFILE, self::$cookieFile); curl_setopt(self::$handle, CURLOPT_COOKIEJAR, self::$cookieFile);