PHP Examples

This page provides examples of how to use TucanAPI with PHP. Examples are provided using both the native cURL extension and the Guzzle HTTP client.

For Guzzle examples, you can install the Guzzle HTTP client using Composer:

composer require guzzlehttp/guzzle

Authentication

All requests to TucanAPI require authentication using an API key. Here's how to include your API key in requests:

Using cURL

<?php
// Your API key
$apiKey = 'YOUR_API_KEY';

// Method 1: Include API key as a query parameter
$url = 'https://api.tucanapi.com/rates/latest?api_key=' . urlencode($apiKey);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$data = json_decode($response, true);
curl_close($curl);

print_r($data);

// Method 2: Include API key in the request header (recommended)
$url = 'https://api.tucanapi.com/rates/latest';

$curl = curl_init($url);
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-Key: ' . $apiKey
    ]
]);
$response = curl_exec($curl);
$data = json_decode($response, true);
curl_close($curl);

print_r($data);

Using Guzzle

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

// Your API key
$apiKey = 'YOUR_API_KEY';

// Create a Guzzle client
$client = new Client([
    'base_uri' => 'https://api.tucanapi.com/',
    'headers' => [
        'X-API-Key' => $apiKey
    ]
]);

// Make a request
$response = $client->request('GET', 'rates/latest');
$data = json_decode($response->getBody(), true);

print_r($data);

Currency Exchange API

Get Latest Exchange Rates

Retrieve the latest exchange rates for a base currency.

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

/**
 * Get the latest exchange rates.
 *
 * @param string $baseCurrency Base currency code (default: USD)
 * @param array|null $currencies Array of currency codes to include (optional)
 * @return array JSON response from the API
 */
function getLatestRates($baseCurrency = 'USD', $currencies = null) {
    $apiKey = 'YOUR_API_KEY';
    
    $client = new Client([
        'base_uri' => 'https://api.tucanapi.com/',
        'headers' => [
            'X-API-Key' => $apiKey
        ]
    ]);
    
    $params = [
        'query' => [
            'base' => $baseCurrency
        ]
    ];
    
    // Add currencies parameter if provided
    if ($currencies && is_array($currencies)) {
        $params['query']['currencies'] = implode(',', $currencies);
    }
    
    try {
        $response = $client->request('GET', 'rates/latest', $params);
        return json_decode($response->getBody(), true);
    } catch (GuzzleException $e) {
        throw new Exception('API request failed: ' . $e->getMessage());
    }
}

// Example usage
try {
    $rates = getLatestRates('EUR', ['USD', 'GBP', 'JPY']);
    print_r($rates);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

IP Geolocation API

Get IP Geolocation

Get location information for an IP address.

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

/**
 * Get geolocation information for an IP address.
 *
 * @param string $ipAddress The IP address to look up
 * @return array JSON response from the API
 */
function getIpGeolocation($ipAddress) {
    $apiKey = 'YOUR_API_KEY';
    
    $client = new Client([
        'base_uri' => 'https://api.tucanapi.com/',
        'headers' => [
            'X-API-Key' => $apiKey
        ]
    ]);
    
    $params = [
        'query' => [
            'ip' => $ipAddress
        ]
    ];
    
    try {
        $response = $client->request('GET', 'ip-geolocation', $params);
        return json_decode($response->getBody(), true);
    } catch (GuzzleException $e) {
        throw new Exception('API request failed: ' . $e->getMessage());
    }
}

// Example usage
try {
    $geolocation = getIpGeolocation('8.8.8.8');
    echo "IP: {$geolocation['ip']}\n";
    echo "Location: {$geolocation['city']}, {$geolocation['country']}\n";
    echo "Coordinates: {$geolocation['latitude']}, {$geolocation['longitude']}\n";
    echo "Timezone: {$geolocation['timezone']}\n";
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

Need Help with PHP Integration?

Our support team is available to help you integrate TucanAPI into your PHP application. If you have any questions or need assistance, don't hesitate to reach out.