Skip to content

Commit

Permalink
Merge pull request #1 from AntoineAugusti/use-config-file
Browse files Browse the repository at this point in the history
Use a configuration file for the API URL and the timeout
  • Loading branch information
buonzz committed Sep 7, 2014
2 parents 97bb52f + 121265f commit fcc39fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ Facade

'GeoIP' => 'Buonzz\GeoIP\Laravel4\Facades\GeoIP',

Configuration
============

This library supports optional configuration.

To get started, first publish the package config file:

```bash
$ php artisan config:publish buonzz/laravel-4-freegeoip
```

- `freegeopipURL`: defines the URL of the FreeGeoIP API. Use HTTPS or not. Default to `http://www.freegeoip.net/json/`.
- `timeout`: defines the timeout when calling the FreeGeoIP API (in seconds). Default to 30.


Usage
=====
Expand Down
5 changes: 2 additions & 3 deletions src/Buonzz/GeoIP/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,12 @@ private function retrievefromCache(){
*/
function resolve($ip){

$url = 'https://freegeoip.net/json/'.$ip;
$timeout = 30; // set to zero for no timeout
$url = \Config::get('laravel-4-freegeoip::freegeoipURL').$ip;

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, \Config::get('laravel-4-freegeoip::timeout'));

$file_contents = curl_exec($ch);
curl_close($ch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
*
*/
class GeoIPServiceProvider extends ServiceProvider{

public function boot()
{
$this->package('buonzz/laravel-4-freegeoip', null, __DIR__.'/../../../..');
}

/**
* Bind the class to IoC container
* @return GeoIP;
Expand Down
9 changes: 9 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return array(
// The URL of the FreeGeoIP API
'freegeoipURL' => 'http://www.freegeoip.net/json/',

// Timeout when calling the API (in seconds)
'timeout' => 30,
);

0 comments on commit fcc39fd

Please sign in to comment.