Gemini API with PHP

Gemini API allows developers to connect with the Gemini exchange. You can fetch prices, trade, and manage accounts. PHP makes integration quick.

What is Gemini API?

Gemini API is a web interface for crypto data and trading. It provides both public and private endpoints. Public endpoints require no login, private ones need keys.

Why Use PHP with Gemini API?

PHP is popular for building web apps. It handles HTTP requests and JSON easily. This makes it suitable for building dashboards and crypto tools.

Requirements

To start, you need PHP installed on your system. You also need cURL, which is common in most PHP setups. An API key is required for private requests.

First Public API Call

You can fetch Bitcoin price from Gemini’s public endpoint using PHP and cURL. Here’s an example.


<?php
$url = "https://api.gemini.com/v1/pubticker/btcusd";

// Initialize cURL
$ch = curl_init($url);

// Return response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute request
$response = curl_exec($ch);

// Close cURL
curl_close($ch);

// Decode JSON response
$data = json_decode($response, true);

print_r($data);
?>


Array
(
    [bid] => 62000.00
    [ask] => 62100.00
    [last] => 62050.00
)

The function curl_exec() runs the request and returns the API data. The output shows bid, ask, and last price values.

Working with JSON Data

The Gemini API returns JSON. PHP has built-in support for decoding JSON. You can access values by their key.


echo "Bid Price: " . $data['bid'] . "\n";
echo "Ask Price: " . $data['ask'] . "\n";
echo "Last Price: " . $data['last'] . "\n";


Bid Price: 62000.00
Ask Price: 62100.00
Last Price: 62050.00

The function json_decode() converts JSON text into a PHP array. This makes it easy to extract fields for your project.

Private Endpoints

Private endpoints require authentication. You must send a signed payload with your API key and secret. Below is an example for account details.


<?php
$apiKey = "your_api_key";
$apiSecret = "your_api_secret";

$url = "https://api.gemini.com/v1/account";
$payload = [
    "request" => "/v1/account",
    "nonce" => round(microtime(true) * 1000)
];

$encodedPayload = json_encode($payload);
$b64 = base64_encode($encodedPayload);
$signature = hash_hmac('sha384', $b64, $apiSecret);

$headers = [
    "Content-Type: text/plain",
    "X-GEMINI-APIKEY: $apiKey",
    "X-GEMINI-PAYLOAD: $b64",
    "X-GEMINI-SIGNATURE: $signature"
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

print_r(json_decode($response, true));
?>

The function hash_hmac() generates the signature. This confirms your identity when accessing private endpoints.

Error Handling

When using APIs, errors may occur. You should check the HTTP response. PHP provides functions to handle status codes.


if ($response) {
    echo "Success:\n";
    print_r(json_decode($response, true));
} else {
    echo "Error: " . curl_error($ch);
}

The method curl_error() shows error details if the request fails. This helps in debugging issues.

Best Practices

Follow these tips for using Gemini API with PHP:

  • Keep API keys secure.
  • Use environment variables.
  • Test on demo before live trading.
  • Handle rate limits and errors.

Real Use Cases

With PHP and Gemini API, you can create price trackers, crypto dashboards, or trading tools. It’s simple and effective for web developers.

For Python users, see this Anaconda installation guide as a related resource.

Conclusion

Gemini API with PHP is easy to use. Start with public endpoints to learn. Then move to private endpoints for account and trading features.

By using PHP functions like curl_init() and json_decode(), you can build real crypto applications. Practice with small steps, and expand later.