62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
$playerId = $_GET['id'] ?? '';
|
|
|
|
if (empty($playerId)) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Player ID is required.']);
|
|
exit;
|
|
}
|
|
|
|
$apiKey = '1'; // Free public API key for testing
|
|
|
|
// --- URLs for concurrent fetching ---
|
|
$detailsUrl = 'https://www.thesportsdb.com/api/v1/json/' . $apiKey . '/lookupplayer.php?id=' . urlencode($playerId);
|
|
$eventsUrl = 'https://www.thesportsdb.com/api/v1/json/' . $apiKey . '/lookuppast5events.php?id=' . urlencode($playerId);
|
|
|
|
// --- cURL multi-handle for concurrent requests ---
|
|
$mh = curl_multi_init();
|
|
|
|
$detailsCh = curl_init($detailsUrl);
|
|
$eventsCh = curl_init($eventsUrl);
|
|
|
|
curl_setopt($detailsCh, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($eventsCh, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_multi_add_handle($mh, $detailsCh);
|
|
curl_multi_add_handle($mh, $eventsCh);
|
|
|
|
// Execute the handles
|
|
$running = null;
|
|
do {
|
|
curl_multi_exec($mh, $running);
|
|
} while ($running);
|
|
|
|
curl_multi_remove_handle($mh, $detailsCh);
|
|
curl_multi_remove_handle($mh, $eventsCh);
|
|
curl_multi_close($mh);
|
|
|
|
// --- Process responses ---
|
|
$detailsResponse = curl_multi_getcontent($detailsCh);
|
|
$eventsResponse = curl_multi_getcontent($eventsCh);
|
|
|
|
$detailsData = json_decode($detailsResponse, true);
|
|
$eventsData = json_decode($eventsResponse, true);
|
|
|
|
// --- Combine and return data ---
|
|
if (isset($detailsData['players']) && count($detailsData['players']) > 0) {
|
|
$playerDetails = $detailsData['players'][0];
|
|
$recentGames = $eventsData['results'] ?? []; // 'results' is the key for past events
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'player' => [
|
|
'details' => $playerDetails,
|
|
'recent_games' => $recentGames
|
|
]
|
|
]);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_encode(['status' => 'error', 'message' => 'Player not found.']);
|
|
} |