function fetch_commodity_prices() {
// Replace this with your API key and endpoint
$api_key = 'your_api_key_here';
$api_url = 'https://www.alphavantage.co/query?function=COMMODITY_EXCHANGE_RATE&apikey=' . $api_key;
// Fetch data from the API
$response = wp_remote_get($api_url);
// Check for errors
if (is_wp_error($response)) {
return "Error fetching commodity prices. Please try again later.";
}
// Decode the JSON response
$data = json_decode(wp_remote_retrieve_body($response), true);
// Check if the required data exists
if (!isset($data['commodities'])) {
return "Commodity data not available at the moment.";
}
// Extract prices
$cotton_price = $data['commodities']['cotton'] ?? 'N/A';
$bean_price = $data['commodities']['soybeans'] ?? 'N/A';
$wheat_price = $data['commodities']['wheat'] ?? 'N/A';
// Generate output
$output = "
- ";
$output .= "
- Cotton Price: $cotton_price USD "; $output .= "
- Beans Price: $bean_price USD "; $output .= "
- Wheat Price: $wheat_price USD "; $output .= "