Skip to content

Commit

Permalink
switch to using curl_setopt_array for better readability, added utili…
Browse files Browse the repository at this point in the history
…ty methods Fixes #66
  • Loading branch information
Ahmad Nassri committed Mar 1, 2015
1 parent 139b580 commit 530905c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ Unirest\Request::verifyPeer(false); // Disables SSL cert validation

By default is `true`.

#### Utility Methods

```php
// alias for `curl_getinfo`
Unirest\Request::getInfo()

// returns internal cURL handle
Unirest\Request::getCurlHandle()
```

----

Made with ♥ from the [Mashape][mashape-url] team
Expand Down
77 changes: 48 additions & 29 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 $handle = null;
private static $jsonOpts = array();
private static $verifyPeer = true;
private static $socketTimeout = null;
Expand Down Expand Up @@ -323,15 +324,15 @@ public static function buildHTTPCurlQuery($data, $parent = false)
*/
public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
{
$ch = curl_init();
self::$handle = curl_init();

if ($method !== Method::GET) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);

if (is_array($body) || $body instanceof \Traversable) {
curl_setopt($ch, CURLOPT_POSTFIELDS, self::buildHTTPCurlQuery($body));
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, self::buildHTTPCurlQuery($body));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
}
} elseif (is_array($body)) {
if (strpos($url, '?') !== false) {
Expand All @@ -343,57 +344,75 @@ public static function send($method, $url, $body = null, $headers = array(), $us
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
}

curl_setopt($ch, CURLOPT_URL, self::encodeUrl($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getFormattedHeaders($headers));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer);
curl_setopt($ch, CURLOPT_ENCODING, ''); // If an empty string, '', is set, a header containing all supported encoding types is sent.
curl_setopt_array(self::$handle, array(
CURLOPT_URL => self::encodeUrl($url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers),
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => self::$verifyPeer,
// If an empty string, '', is set, a header containing all supported encoding types is sent
CURLOPT_ENCODING => ''
));

if (self::$socketTimeout !== null) {
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
}

// supporting deprecated http auth method
if (!empty($username)) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt_array(self::$handle, array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username . ':' . $password
));
}

if (!empty(self::$auth['user'])) {
curl_setopt($ch, CURLOPT_HTTPAUTH, self::$auth['method']);
curl_setopt($ch, CURLOPT_USERPWD, self::$auth['user'] . ':' . self::$auth['pass']);
curl_setopt_array(self::$handle, array(
CURLOPT_HTTPAUTH => self::$auth['method'],
CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass']
));
}

if (self::$proxy['address'] !== false) {
curl_setopt($ch, CURLOPT_PROXYTYPE, self::$proxy['type']);
curl_setopt($ch, CURLOPT_PROXY, self::$proxy['address']);
curl_setopt($ch, CURLOPT_PROXYPORT, self::$proxy['port']);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, self::$proxy['tunnel']);

curl_setopt($ch, CURLOPT_PROXYAUTH, self::$proxy['auth']['method']);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']);
curl_setopt_array(self::$handle, array(
CURLOPT_PROXYTYPE => self::$proxy['type'],
CURLOPT_PROXY => self::$proxy['address'],
CURLOPT_PROXYPORT => self::$proxy['port'],
CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'],
CURLOPT_PROXYAUTH => self::$proxy['auth']['method'],
CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']
));
}

$response = curl_exec($ch);
$error = curl_error($ch);
$response = curl_exec(self::$handle);
$error = curl_error(self::$handle);
$info = self::getInfo();

if ($error) {
throw new \Exception($error);
}

// Split the full response in its headers and body
$curl_info = curl_getinfo($ch);
$header_size = $curl_info['header_size'];
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$httpCode = $curl_info['http_code'];
$httpCode = $info['http_code'];

return new Response($httpCode, $body, $header, self::$jsonOpts);
}

public static function getInfo()
{
return curl_getinfo(self::$handle);
}

public static function getCurlHandle()
{
return self::$handle;
}

public static function getFormattedHeaders($headers)
{
$formattedHeaders = array();
Expand Down

0 comments on commit 530905c

Please sign in to comment.