-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth-example.php
More file actions
85 lines (66 loc) · 2.61 KB
/
auth-example.php
File metadata and controls
85 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* FCS API - Authentication Examples
*
* Three authentication methods:
* 1. access_key - Simple API key (default)
* 2. ip_whitelist - IP whitelist (no key needed)
* 3. token - Secure token-based (recommended for frontend)
*
* Get your API key at: https://fcsapi.com
*/
require_once __DIR__ . '/../src/FcsConfig.php';
require_once __DIR__ . '/../src/FcsApi.php';
require_once __DIR__ . '/../src/FCS_Forex.php';
use FcsApi\FcsApi;
use FcsApi\FcsConfig;
// ============================================================
// Method 1: Simple API Key (Backward Compatible)
// ============================================================
echo "<h3>Method 1: Simple API Key</h3>";
$fcsapi = new FcsApi('YOUR_API_KEY');
var_dump($fcsapi->forex->getLatestPrice('FX:EURUSD'));
// ============================================================
// Method 2: IP Whitelist (No key needed)
// ============================================================
echo "<br><br><h3>Method 2: IP Whitelist</h3>";
// First, whitelist your server IP in your account:
// https://fcsapi.com/dashboard/profile -> IP Whitelist
$config = FcsConfig::withIpWhitelist();
$fcsapi = new FcsApi($config);
var_dump($fcsapi->forex->getLatestPrice('FX:EURUSD'));
// ============================================================
// Method 3: Token-Based Authentication (Secure)
// ============================================================
echo "<br><br><h3>Method 3: Token-Based (Secure)</h3>";
// Step 1: On your BACKEND - Generate token
$config = FcsConfig::withToken(
'YOUR_API_KEY', // Private key (keep secret on server)
'YOUR_PUBLIC_KEY', // Public key (can be exposed)
3600 // Token valid for 1 hour
);
$fcsapi = new FcsApi($config);
// Generate token to send to frontend
$tokenData = $fcsapi->generateToken();
echo "<pre>Token for frontend:\n";
print_r($tokenData);
echo "</pre>";
// This token data can be sent to frontend JavaScript:
// {
// '_token' => 'abc123...',
// '_expiry' => 1764164233,
// '_public_key' => 'your_public_key'
// }
// API request with token auth
var_dump($fcsapi->forex->getLatestPrice('FX:EURUSD'));
// ============================================================
// Using Config Object (Advanced)
// ============================================================
echo "<br><br><h3>Advanced: Custom Config</h3>";
$config = new FcsConfig();
$config->authMethod = 'access_key';
$config->accessKey = 'YOUR_API_KEY';
$config->timeout = 60; // Custom timeout
$config->connectTimeout = 10; // Custom connect timeout
$fcsapi = new FcsApi($config);
var_dump($fcsapi->forex->getLatestPrice('FX:EURUSD'));