61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
$player1Id = $_GET['p1'] ?? '';
|
|
$player2Id = $_GET['p2'] ?? '';
|
|
|
|
if (empty($player1Id) || empty($player2Id)) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Two player IDs are required.']);
|
|
exit;
|
|
}
|
|
|
|
$apiKey = '1'; // Free public API key for testing
|
|
|
|
// --- URLs for concurrent fetching ---
|
|
$p1_url = 'https://www.thesportsdb.com/api/v1/json/' . $apiKey . '/lookupplayer.php?id=' . urlencode($player1Id);
|
|
$p2_url = 'https://www.thesportsdb.com/api/v1/json/' . $apiKey . '/lookupplayer.php?id=' . urlencode($player2Id);
|
|
|
|
// --- cURL multi-handle for concurrent requests ---
|
|
$mh = curl_multi_init();
|
|
|
|
$p1_ch = curl_init($p1_url);
|
|
$p2_ch = curl_init($p2_url);
|
|
|
|
curl_setopt($p1_ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($p2_ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_multi_add_handle($mh, $p1_ch);
|
|
curl_multi_add_handle($mh, $p2_ch);
|
|
|
|
// Execute the handles
|
|
$running = null;
|
|
do {
|
|
curl_multi_exec($mh, $running);
|
|
} while ($running);
|
|
|
|
curl_multi_remove_handle($mh, $p1_ch);
|
|
curl_multi_remove_handle($mh, $p2_ch);
|
|
curl_multi_close($mh);
|
|
|
|
// --- Process responses ---
|
|
$p1_response = curl_multi_getcontent($p1_ch);
|
|
$p2_response = curl_multi_getcontent($p2_ch);
|
|
|
|
$p1_data = json_decode($p1_response, true);
|
|
$p2_data = json_decode($p2_response, true);
|
|
|
|
// --- Combine and return data ---
|
|
if (isset($p1_data['players'][0]) && isset($p2_data['players'][0])) {
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'players' => [
|
|
$p1_data['players'][0],
|
|
$p2_data['players'][0]
|
|
]
|
|
]);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_encode(['status' => 'error', 'message' => 'One or both players not found.']);
|
|
}
|