| To fetch the current pricing for a cryptocurrency like PEPE using the Coinbase API with PHP and cURL, follow this guide: Prerequisites: - Ensure you have access to the Coinbase Exchange Rates API, which doesn't require authentication for public endpoints.
Example Code for PEPE Pricing <?php
// Define the API endpoint
$currency = 'PEPE'; // Replace with the symbol of the cryptocurrency
$baseCurrency = 'USD'; // Replace with the desired fiat or base currency
$endpoint = "https://api.coinbase.com/v2/prices/{$currency}-{$baseCurrency}/spot";
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
// Parse and display the response
$data = json_decode($response, true);
if (isset($data['data']['amount'])) {
echo "The current price of {$currency} in {$baseCurrency} is: " . $data['data']['amount'];
} else {
echo "Failed to retrieve price. Response: " . $response;
}
}
// Close cURL
curl_close($ch);
?>
Explanation: - Endpoint: The Coinbase
/v2/prices/{currency}-{base_currency}/spot endpoint provides the current spot price for a cryptocurrency. - Example:
- Replace
PEPE with the desired cryptocurrency code. - Replace
USD with the base currency in which you want the price.
Output: - If successful: The current price of PEPE in the specified currency.
- If the cryptocurrency isn't available, the API response will indicate the issue.
|