Skip to content

Commit

Permalink
Merge pull request #56 from MindscapeHQ/request-improvements
Browse files Browse the repository at this point in the history
Request improvements
  • Loading branch information
fundead committed Dec 1, 2014
2 parents bd3d46c + 1ff6c5d commit 75151ab
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ Windows default: *false*

*false* is the only effective option on Windows due to platform and library limitations within the supported versions.

### Proxies

A HTTP proxy can be set if your environment can't connect out through PHP or the `curl` binrary natively:

```php
$client = new \Raygun4php\RaygunClient("apiKey");
$client->setProxy('http://someproxy:8080');
```

### Debug mode

The client offers a debug mode in which the HTTP response code can be returned after a POST attempt. This can be useful when adding Raygun to your site. This is accessed by passing in *true* as the third parameter in the client constructor:
Expand Down Expand Up @@ -203,6 +212,7 @@ If, when running a PHP script from the command line on *nix operating systems, y

## Changelog

- 1.6.0: Added HTTP proxy support, support X-Forwarded-For, null server var guards
- 1.5.3: Unify property casing (internal change)
- 1.5.2: Prevent error when query_string isn't present in $_SERVER
- 1.5.1: Guard against intermittent user id cookie being null; overload for disabling user tracking
Expand Down
38 changes: 34 additions & 4 deletions src/Raygun4php/RaygunClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RaygunClient
protected $useAsyncSending;
protected $debugSending;
protected $disableUserTracking;
protected $proxy;

/**
* @var Array Parameter names to filter out of logged form data. Case insensitive.
Expand Down Expand Up @@ -296,12 +297,23 @@ private function post($data_to_send, $cert_path)
$result = stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
}

if ($this->proxy) {
$result = stream_context_set_option($context, 'http', 'proxy', $this->proxy);
}

if ($this->useAsyncSending && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
{
$cmd = "curl -X POST -H 'Content-Type: application/json' -H 'X-ApiKey: " . $this->apiKey . "'";
$cmd .= " -d " . escapeshellarg($data_to_send) . " --cacert '" . realpath(__DIR__ . '/cacert.crt')
. "' 'https://api.raygun.io:443/entries' > /dev/null 2>&1 &";

$curlOpts = array(
"-X POST",
"-H 'Content-Type: application/json'",
"-H 'X-ApiKey: " . $this->apiKey . "'",
"-d " . escapeshellarg($data_to_send),
"--cacert '" . realpath(__DIR__ . '/cacert.crt') . "'"
);
if ($this->proxy) {
$curlOpts[] = "--proxy '" . $this->proxy . "'";
}
$cmd = "curl " . implode(' ', $curlOpts) . " 'https://api.raygun.io:443/entries' > /dev/null 2>&1 &";
exec($cmd, $output, $exit);
return $exit;
}
Expand Down Expand Up @@ -432,6 +444,24 @@ function getFilterParams() {
return $this->filterParams;
}

/**
* Use a proxy for sending HTTP requests to Raygun.
*
* @param String $url URL including protocol and an optional port, e.g. http://myproxy:8080
* @return Raygun4php\RaygunClient
*/
function setProxy($proxy) {
$this->proxy = $proxy;
return $this;
}

/**
* @return String
*/
function getProxy() {
return $this->proxy;
}

public function __destruct()
{
if ($this->httpData)
Expand Down
2 changes: 1 addition & 1 deletion src/Raygun4php/RaygunClientMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RaygunClientMessage
public function __construct()
{
$this->Name = "Raygun4php";
$this->Version = "1.5.3";
$this->Version = "1.6.0";
$this->ClientUrl = "https://github.com/MindscapeHQ/raygun4php";
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/Raygun4php/RaygunRequestMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class RaygunRequestMessage
public function __construct()
{
if (php_sapi_name() !== 'cli') {
$this->HostName = $_SERVER['HTTP_HOST'];
$this->HttpMethod = $_SERVER['REQUEST_METHOD'];
$this->Url = $_SERVER['REQUEST_URI'];
$this->IpAddress = $_SERVER['REMOTE_ADDR'];
$this->HostName = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : null;
$this->HttpMethod = (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : null;
$this->Url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : null;
$this->IpAddress = $this->getRemoteAddr();

if (array_key_exists('QUERY_STRING', $_SERVER))
{
Expand Down Expand Up @@ -99,5 +99,21 @@ private function emu_getAllHeaders()
return getallheaders();
}
}

private function getRemoteAddr()
{
$ip = null;

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else if (!empty($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}

return $ip;
}
}
}

0 comments on commit 75151ab

Please sign in to comment.