callfi_api/dexscreener.php
2024-06-20 23:34:56 +08:00

41 lines
1.1 KiB
PHP

<?php
// Function to get pair address for a token paired with USDC using Dexscreener API
function getPairAddress($tokenSymbol) {
// Dexscreener Search API URL
$searchApiUrl = "https://api.dexscreener.com/latest/dex/search?q=" . urlencode($tokenSymbol);
// Initialize cURL session for the search API
$ch = curl_init();
// Set the URL and other options for the cURL session
curl_setopt($ch, CURLOPT_URL, $searchApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL session and store the result
$searchResponse = curl_exec($ch);
// Check for errors in cURL execution
if ($searchResponse === false) {
echo 'cURL Error: ' . curl_error($ch);
return null;
}
// Decode the JSON search response
$searchData = json_decode($searchResponse, true);
$pairs = $searchData['pairs'];
$highestPair = $pairs[0];
foreach($pairs as $pair){
$vol = $pair["volume"]["h24"];
if($vol > $highestPair["volume"]["h24"]){
$highestPair =$pair;
}
}
echo json_encode($highestPair);
// Return null if no pair found
return null;
}
?>