OverviewAPI ReferenceTutorialChangelogStatus
Tutorial · Pemula · ±10 mnt

SDK PHP & Laravel

Pakai Laravel HTTP Client atau Guzzle untuk memanggil API.

1. Cara A — Laravel HTTP Client

Simpan key di .env sebagai SM_API_KEY.

use Illuminate\Support\Facades\Http;

$res = Http::withToken(config('services.sm.key'))
    ->timeout(60)
    ->post('https://sahabatmobile.com/api/v1/chat/completions', [
        'model' => 'google/gemini-2.5-flash',
        'messages' => [
            ['role' => 'user', 'content' => 'Halo!'],
        ],
        'max_tokens' => 300,
    ]);

if ($res->failed()) {
    $msg = $res->json('error.message', 'Gagal memanggil AI');
    throw new \Exception("AI error ({$res->status()}): {$msg}");
}

echo $res->json('choices.0.message.content');

2. Cara B — Guzzle (PHP native / CodeIgniter / WP)

Cocok untuk framework apa pun termasuk plugin WordPress.

$client = new \GuzzleHttp\Client(['timeout' => 60]);
$res = $client->post('https://sahabatmobile.com/api/v1/chat/completions', [
    'headers' => ['Authorization' => 'Bearer ' . getenv('SM_API_KEY')],
    'json' => [
        'model' => 'google/gemini-2.5-flash',
        'messages' => [['role' => 'user', 'content' => 'Halo!']],
    ],
]);
$body = json_decode((string) $res->getBody(), true);
echo $body['choices'][0]['message']['content'];

3. Tips produksi

Cache jawaban untuk pertanyaan berulang (hemat kredit), dan catat usage.total_tokens ke log untuk rekonsiliasi billing.

← SEBELUMNYA
SDK Node.js (fetch & OpenAI)
SELANJUTNYA →
Respons Streaming Real-Time (SSE)
← Semua tutorial · API Reference
© 2026 SahabatMobile — PT BINA SEJAHTERA INOVASI DIGITAL Docs · API · Tutorial · Changelog · Status