Skip to content

Commit

Permalink
Adding a more general query
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Witwicki committed Apr 22, 2021
1 parent 8f64488 commit bb9df4d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fromtheoutfit/ipstack",
"description": "Simple custom wrapper around the ipstack api.",
"type": "craft-plugin",
"version": "1.0.0",
"version": "1.0.3",
"keywords": [
"craft",
"cms",
Expand Down
33 changes: 33 additions & 0 deletions src/services/IpstackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ public function getCountry()
return $data;
}

public function get()
{
$ip = $this->getIpAddress();
$data = '';

// set IP address and API access key

// Initialize CURL:
$ch = curl_init('http://api.ipstack.com/' . $ip . '?access_key=' . $this->access_key . '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Store the data:
$json = curl_exec($ch);
curl_close($ch);

// make sure we got a curl response
if ($json) {
// Decode JSON response:
$api_result = json_decode($json, true);

// The api only returns a success key if that key is set to false...which is fun.
// So we look for that key and then we look to make sure it is false
// and then we set the data to an empty string
if (array_key_exists('success', $api_result) && !$api_result['success']) {
$data = [];
} else {
$data = $api_result;
}
}

return $data;
}


private function getIpAddress()
{
Expand Down
5 changes: 5 additions & 0 deletions src/variables/IpstackVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function country($optional = null)
{
return Ipstack::$plugin->ipstackService->getCountry();
}

public function get($optional = null)
{
return Ipstack::$plugin->ipstackService->get();
}
}

0 comments on commit bb9df4d

Please sign in to comment.