---
title: "อัตราแลกเปลี่ยนใน PHP และ Laravel"
description: "ดึงและแคชอัตราด้วย Laravel HTTP client, bearer auth, timeout และข้อมูลความใหม่ โดยเก็บคีย์ของ exchangerate.dev ไว้ฝั่งเซิร์ฟเวอร์"
canonical: "https://exchangerate.dev/th/learn/php-laravel-exchange-rates"
---

> Index of every exchangerate.dev page and doc for AI readers: [llms.txt](https://exchangerate.dev/llms.txt)

# อัตราแลกเปลี่ยนใน PHP และ Laravel

ดึงและแคชอัตราด้วย Laravel HTTP client, bearer auth, timeout และข้อมูลความใหม่ โดยเก็บคีย์ของ exchangerate.dev ไว้ฝั่งเซิร์ฟเวอร์

17 ก.ค. 2569 · อ่าน 6 นาที

บริการ Laravel ปฏิเสธ error และแคชข้อมูล FX ครบ 60 วินาที เก็บ key ใน config และคืน `source`, `market_session`, `data_updated_at`

- ใช้ bearer auth, timeout และ `throw()`
- แคช response ที่สำเร็จ 60 วินาที
- อ่าน key จาก `EXCHANGERATE_API_KEY`
- ใช้ไลบรารี decimal เมื่อต้องการความแม่นยำ

## สร้างบริการ FX ที่มีแคช

บริการสร้าง cache key จาก base และ symbols ที่เรียงแล้ว โดย `throw()` ทำงานก่อนเขียนแคช

```phpappservicesexchangerateservicephp title="php · app/Services/ExchangeRateService.php"
<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

final class ExchangeRateService
{
    public function latest(string $base, array $symbols): array
    {
        sort($symbols);
        $symbolList = implode(',', $symbols);
        $cacheKey = "fx:latest:{$base}:{$symbolList}";

        return Cache::remember($cacheKey, now()->addSeconds(60), function () use (
            $base,
            $symbolList,
        ): array {
            return Http::acceptJson()
                ->withToken(config('services.exchangerate.key'))
                ->timeout(5)
                ->get(
                    "https://api.exchangerate.dev/v1/latest/{$base}",
                    ['symbols' => $symbolList],
                )
                ->throw()
                ->json();
        });
    }
}
```

## ตั้ง key นอก source code

```phpconfigservicesphpandenv title="php · config/services.php and .env"
// config/services.php
'exchangerate' => [
    'key' => env('EXCHANGERATE_API_KEY'),
],

// .env (never commit the real value)
EXCHANGERATE_API_KEY=exr_live_...
```

## อ่านอัตราและข้อมูลการสังเกต

หลัง `$service->latest("USD", ["EUR", "GBP"])` ให้อ่าน `$result["rates"]["EUR"]` และเก็บฟิลด์แหล่งที่มาและเวลา

**อย่าแคช error เป็นอัตรา**

401 คือ key ผิด และ 429 คือเกิน limit ให้เก็บค่าเก่าได้เฉพาะพร้อมเวลาเดิม

- เรียง symbols
- ตั้ง timeout
- เรียก throw ก่อน cache
- ปัดเฉพาะยอดสุดท้าย

## คำถาม

### Laravel ต้องมี SDK ไหม

ไม่ต้อง HTTP client ในตัวส่ง token, query, timeout และ JSON ได้

### ควรแคชนานเท่าไร

60 วินาทีเหมาะกับคู่ที่ active และควรตรวจ data_updated_at สำหรับข้อมูลรายวัน

### PHP float แม่นพอไหม

ใช้ไลบรารี decimal หรือ arbitrary precision เมื่อต้องการยอดเงินแม่นยำ

### Free limit เท่าไร

5,000 ครั้งต่อเดือนและ 10 ครั้งต่อนาที โดยไม่ต้องใช้บัตร

## อ่านต่อ

- [อัตราแลกเปลี่ยนย้อนหลังและข้อมูลอนุกรมเวลา](https://exchangerate.dev/th/learn/historical-fx-time-series)

Source: https://exchangerate.dev/th/learn/php-laravel-exchange-rates
