1Kurulum
altinapi, standart HTTP istekleriyle çalışır. PHP'nin yerleşik cURL uzantısı yeterlidir. Guzzle tercih edenler için:
composer require guzzlehttp/guzzle
API key almak için ücretsiz hesap oluşturun. Key, her istekte x-api-key header'ı olarak gönderilir.
2cURL ile REST API
Tek Sembol Fiyatı
PHP — cURL (tek sembol)
<?php function getAltinapiPrice(string $symbol, string $apiKey): array { $url = "https://altinapi.com/api/prices/" . urlencode($symbol); $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_HTTPHEADER => [ "x-api-key: $apiKey", "Accept: application/json", ], ]); $body = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status !== 200) { throw new RuntimeException("API hatası: HTTP $status"); } return json_decode($body, true); } // Kullanım $apiKey = "hapi_xxxx"; $data = getAltinapiPrice("ALTIN", $apiKey); echo "Has Altın — Alış: {$data['bid']} TL, Satış: {$data['ask']} TL\n";
Tüm Fiyatları Çekme
PHP — cURL (tüm fiyatlar)
<?php function getAllAltinapiPrices(string $apiKey): array { $ch = curl_init("https://altinapi.com/api/prices"); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_HTTPHEADER => ["x-api-key: $apiKey"], ]); $body = curl_exec($ch); curl_close($ch); $prices = json_decode($body, true); return array_column($prices, null, "symbol"); // symbol => data } $prices = getAllAltinapiPrices("hapi_xxxx"); echo $prices["USDTRY"]["bid"] . "\n"; // USD/TRY alış echo $prices["CEYREK_YENI"]["ask"] . "\n"; // Çeyrek altın satış
3Guzzle HTTP
PHP — Guzzle
<?php use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; $client = new Client([ "base_uri" => "https://altinapi.com/api/", "timeout" => 5.0, "headers" => ["x-api-key" => "hapi_xxxx"], ]); try { $response = $client->get("prices/ALTIN"); $data = json_decode($response->getBody(), true); echo "Has Altın — Alış: {$data['bid']} TL\n"; } catch (RequestException $e) { error_log("altinapi hatası: " . $e->getMessage()); }
4WordPress — Widget Örneği
WordPress temalarında veya eklentilerinde wp_remote_get() kullanarak altinapi'ye bağlanabilirsiniz:
PHP — WordPress (functions.php veya eklenti)
<?php function altinapi_get_gold_price(): ?array { $api_key = get_option("altinapi_api_key"); $cache = get_transient("altinapi_gold_price"); if ($cache !== false) { return $cache; // 60 sn cache — API kotasını korur } $response = wp_remote_get( "https://altinapi.com/api/prices/ALTIN", ["headers" => ["x-api-key" => $api_key], "timeout" => 5] ); if (is_wp_error($response)) return null; $data = json_decode(wp_remote_retrieve_body($response), true); set_transient("altinapi_gold_price", $data, 60); // 60 sn sakla return $data; } // Shortcode: [altin_fiyat] add_shortcode("altin_fiyat", function() { $price = altinapi_get_gold_price(); if (!$price) return "<p>Fiyat alınamadı.</p>"; return sprintf( '<div class="altinapi-price">Has Altın: <strong>%s TL</strong></div>', number_format($price["bid"], 2, ",", ".") ); });
5Laravel — Service Örneği
PHP — Laravel (app/Services/AltinApiService.php)
<?php namespace App\Services; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Cache; class AltinApiService { private string $apiKey; private string $baseUrl = "https://altinapi.com/api"; public function __construct() { $this->apiKey = config("services.altinapi.api_key"); } public function getPrice(string $symbol): array { return Cache::remember( "altinapi_price_$symbol", 60, fn() => Http::withHeaders(["x-api-key" => $this->apiKey]) ->get("$this->baseUrl/prices/$symbol") ->json() ); } public function getAllPrices(): array { return Cache::remember( "altinapi_prices_all", 30, fn() => Http::withHeaders(["x-api-key" => $this->apiKey]) ->get("$this->baseUrl/prices") ->json() ); } } // Controller'da kullanım: // $price = app(AltinApiService::class)->getPrice("ALTIN");