Skip to content

Commit

Permalink
Merge pull request #77 from cristianp6/master
Browse files Browse the repository at this point in the history
Fixed "Undefined variable: ch" and add cookie string support
  • Loading branch information
Ahmad Nassri committed Apr 3, 2015
2 parents 8da79e7 + 0293eb2 commit ec5828c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/Unirest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class Request
{
private static $cookie = null;
private static $cookieFile = null;
private static $defaultHeaders = array();
private static $handle = null;
Expand Down Expand Up @@ -103,6 +104,16 @@ public static function setMashapeKey($key)
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
*
Expand Down Expand Up @@ -373,9 +384,13 @@ public static function send($method, $url, $body = null, $headers = array(), $us
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
}

if (self::$cookie) {
curl_setopt(self::$handle, CURLOPT_COOKIE, self::$cookie);
}

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
Expand Down

0 comments on commit ec5828c

Please sign in to comment.