function get_gold_prices_from_api() {
$api_url = 'https://api.goldapi.io/api/XAU/USD';
$response = wp_remote_get($api_url, [
'headers' => [
'x-access-token' => 'goldapi-4t0usmavl0mdp-io'
]
]);
if (is_wp_error($response)) {
return [
'24k' => 'N/A (0%)',
'22k' => 'N/A (0%)'
];
}
$body = json_decode(wp_remote_retrieve_body($response), true);
// احسب التغير في السعر (هذا مثال - تحتاج لتعديله حسب ما يوفره API)
$current_price = $body['price'];
$previous_close = $body['previous_close_price']; // افتراضيًا
$change = $current_price - $previous_close;
$change_percent = round(($change / $previous_close) * 100, 2);
// تحويل إلى جرام
$price_24k = round($current_price * 0.05, 2);
$price_22k = round(($current_price * 0.05) * 0.916, 2);
return [
'24k' => $price_24k . ' (' . ($change >= 0 ? '+' : '') . $change_percent . '%)',
'22k' => $price_22k . ' (' . ($change >= 0 ? '+' : '') . $change_percent . '%)'
];
}