CurrencyExchange.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace App\Utils;
  3. use Cache;
  4. use Exception;
  5. use Http;
  6. use Illuminate\Http\Client\PendingRequest;
  7. use Log;
  8. class CurrencyExchange
  9. {
  10. private static PendingRequest $basicRequest;
  11. private static array $apis = ['fixer', 'exchangerateApi', 'wise', 'currencyData', 'exchangeRatesData', 'duckduckgo', 'wsj', 'xRates', 'valutafx', 'baidu', 'unionpay', 'jsdelivrFile', 'it120', 'k780'];
  12. /**
  13. * @param string $target target Currency
  14. * @param float|int $amount exchange amount
  15. * @param string|null $base Base Currency
  16. * @return float|null amount in target currency
  17. */
  18. public static function convert(string $target, float|int $amount, ?string $base = null): ?float
  19. {
  20. $base = $base ?? (string) sysConfig('standard_currency');
  21. $cacheKey = "Currency_{$base}_{$target}_ExRate";
  22. if (Cache::has($cacheKey)) {
  23. return round($amount * Cache::get($cacheKey), 2);
  24. }
  25. self::setClient();
  26. foreach (self::$apis as $api) {
  27. try {
  28. $rate = self::$api($base, $target);
  29. if ($rate !== null) {
  30. Cache::put($cacheKey, $rate, Day);
  31. return round($amount * $rate, 2);
  32. }
  33. } catch (Exception $e) {
  34. Log::error("[$api] 币种汇率信息获取报错: ".$e->getMessage());
  35. }
  36. }
  37. return null;
  38. }
  39. private static function setClient(): void
  40. {
  41. self::$basicRequest = Http::timeout(15)->withOptions(['http_errors' => false])->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
  42. }
  43. public static function unionTest(string $target, ?string $base = null): void
  44. {
  45. $base = $base ?? (string) sysConfig('standard_currency');
  46. self::setClient();
  47. foreach (self::$apis as $api) {
  48. try {
  49. echo $api.': '.self::$api($base, $target).PHP_EOL;
  50. } catch (Exception $e) {
  51. echo $api.': error'.PHP_EOL;
  52. Log::error("[$api] 币种汇率信息获取报错: ".$e->getMessage());
  53. continue;
  54. }
  55. }
  56. }
  57. private static function exchangerateApi(string $base, string $target): ?float
  58. { // Reference: https://www.exchangerate-api.com/docs/php-currency-api
  59. $key = config('services.currency.exchangerate-api_key');
  60. $url = $key ? "https://v6.exchangerate-api.com/v6/$key/pair/$base/$target" : "https://open.er-api.com/v6/latest/$base";
  61. $response = self::$basicRequest->get($url);
  62. if ($response->ok()) {
  63. $data = $response->json();
  64. if ($data['result'] === 'success') {
  65. return $key ? $data['conversion_rate'] : $data['rates'][$target];
  66. }
  67. Log::emergency('[CurrencyExchange]exchangerateApi exchange failed with following message: '.$data['error-type'] ?? '');
  68. } else {
  69. Log::emergency('[CurrencyExchange]exchangerateApi request failed '.var_export($response, true));
  70. }
  71. return null;
  72. }
  73. private static function k780(string $base, string $target): ?float
  74. { // Reference: https://www.nowapi.com/api/finance.rate
  75. $response = self::$basicRequest->get("https://sapi.k780.com/?app=finance.rate&scur=$base&tcur=$target&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json");
  76. if ($response->ok()) {
  77. $data = $response->json();
  78. if ($data['success'] === '1') {
  79. return $data['result']['rate'];
  80. }
  81. Log::emergency('[CurrencyExchange]Nowapi exchange failed with following message: '.$data['msg']);
  82. } else {
  83. Log::emergency('[CurrencyExchange]Nowapi request failed'.var_export($response, true));
  84. }
  85. return null;
  86. }
  87. private static function it120(string $base, string $target): ?float
  88. { // Reference: https://www.it120.cc/help/fnun8g.html
  89. $key = config('services.currency.it120_key');
  90. if ($key) {
  91. $response = self::$basicRequest->get("https://api.it120.cc/$key/forex/rate?fromCode=$target&toCode=$base");
  92. if ($response->ok()) {
  93. $data = $response->json();
  94. if ($data['code'] === 0) {
  95. return $data['data']['rate'];
  96. }
  97. Log::emergency('[CurrencyExchange]it120 exchange failed with following message: '.$data['msg']);
  98. } else {
  99. Log::emergency('[CurrencyExchange]it120 request failed'.var_export($response, true));
  100. }
  101. }
  102. return null;
  103. }
  104. private static function fixer(string $base, string $target): ?float
  105. { // Reference: https://apilayer.com/marketplace/fixer-api RATE LIMIT: 100 Requests / Monthly!!!!
  106. $key = config('services.currency.apiLayer_key');
  107. if ($key) {
  108. $response = self::$basicRequest->withHeader('apikey', $key)->get("https://api.apilayer.com/fixer/latest?symbols=$target&base=$base");
  109. if ($response->ok()) {
  110. $data = $response->json();
  111. if ($data['success']) {
  112. return $data['rates'][$target];
  113. }
  114. Log::emergency('[CurrencyExchange]Fixer exchange failed with following message: '.$data['error']['type'] ?? '');
  115. } else {
  116. Log::emergency('[CurrencyExchange]Fixer request failed'.var_export($response, true));
  117. }
  118. }
  119. return null;
  120. }
  121. private static function currencyData(string $base, string $target): ?float
  122. { // Reference: https://apilayer.com/marketplace/currency_data-api RATE LIMIT: 100 Requests / Monthly
  123. $key = config('services.currency.apiLayer_key');
  124. if ($key) {
  125. $response = self::$basicRequest->withHeader('apikey', $key)->get("https://api.apilayer.com/currency_data/live?source=$base&currencies=$target");
  126. if ($response->ok()) {
  127. $data = $response->json();
  128. if ($data['success']) {
  129. return $data['quotes'][$base.$target];
  130. }
  131. Log::emergency('[CurrencyExchange]Currency Data exchange failed with following message: '.$data['error']['info'] ?? '');
  132. } else {
  133. Log::emergency('[CurrencyExchange]Currency Data request failed'.var_export($response, true));
  134. }
  135. }
  136. return null;
  137. }
  138. private static function exchangeRatesData(string $base, string $target): ?float
  139. { // Reference: https://apilayer.com/marketplace/exchangerates_data-api RATE LIMIT: 250 Requests / Monthly
  140. $key = config('services.currency.apiLayer_key');
  141. if ($key) {
  142. $response = self::$basicRequest->withHeaders(['apikey' => $key])->get("https://api.apilayer.com/exchangerates_data/latest?symbols=$target&base=$base");
  143. if ($response->ok()) {
  144. $data = $response->json();
  145. if ($data['success']) {
  146. return $data['rates'][$target];
  147. }
  148. Log::emergency('[CurrencyExchange]Exchange Rates Data exchange failed with following message: '.$data['error']['message'] ?? '');
  149. } else {
  150. Log::emergency('[CurrencyExchange]Exchange Rates Data request failed'.var_export($response, true));
  151. }
  152. }
  153. return null;
  154. }
  155. private static function jsdelivrFile(string $base, string $target): ?float
  156. { // Reference: https://github.com/fawazahmed0/currency-api
  157. $response = self::$basicRequest->get('https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/'.strtolower($base).'.min.json');
  158. if ($response->ok()) {
  159. $data = $response->json();
  160. return $data[strtolower($base)][strtolower($target)];
  161. }
  162. return null;
  163. }
  164. private static function duckduckgo(string $base, string $target): ?float
  165. { // Reference: https://duckduckgo.com http://www.xe.com/
  166. $response = self::$basicRequest->get("https://duckduckgo.com/js/spice/currency_convert/1/$base/$target");
  167. if ($response->ok()) {
  168. $data = $response->json();
  169. return $data['to'][0]['mid'];
  170. }
  171. return null;
  172. }
  173. private static function wise(string $base, string $target): ?float
  174. { // Reference: https://wise.com/zh-cn/currency-converter/
  175. $response = self::$basicRequest->withHeader('Authorization', 'Basic OGNhN2FlMjUtOTNjNS00MmFlLThhYjQtMzlkZTFlOTQzZDEwOjliN2UzNmZkLWRjYjgtNDEwZS1hYzc3LTQ5NGRmYmEyZGJjZA==')->get("https://api.wise.com/v1/rates?source=$base&target=$target");
  176. if ($response->ok()) {
  177. $data = $response->json();
  178. return $data[0]['rate'];
  179. }
  180. return null;
  181. }
  182. private static function wsj(string $base, string $target): ?float
  183. { // Reference: https://www.wsj.com/market-data/quotes/fx/USDCNY
  184. $response = self::$basicRequest->get("https://www.wsj.com/market-data/quotes/ajax/fx/9/$base$target?source=$base&target=$base$target&value=1");
  185. if ($response->ok()) {
  186. $data = $response->body();
  187. preg_match('/<span[^>]*>([\d\.]+)<\/span>/', $data, $matches);
  188. return $matches[1];
  189. }
  190. return null;
  191. }
  192. private static function xRates(string $base, string $target): ?float
  193. { // Reference: https://www.x-rates.com/
  194. $response = self::$basicRequest->get("https://www.x-rates.com/calculator/?from=$base&to=$target&amount=1");
  195. if ($response->ok()) {
  196. $data = $response->body();
  197. preg_match('/<span class="ccOutputRslt">([\d.]+)/', $data, $matches);
  198. return $matches[1];
  199. }
  200. return null;
  201. }
  202. private static function valutafx(string $base, string $target): ?float
  203. { // Reference: https://www.valutafx.com/convert/
  204. $response = self::$basicRequest->get("https://www.valutafx.com/api/v2/rates/lookup?isoTo=$target&isoFrom=$base&amount=1");
  205. if ($response->ok()) {
  206. $data = $response->json();
  207. if (! $data['ErrorMessage']) {
  208. return $data['Rate'];
  209. }
  210. }
  211. return null;
  212. }
  213. private static function unionpay(string $base, string $target): ?float
  214. { // Reference: https://www.unionpayintl.com/cn/rate/
  215. $response = self::$basicRequest->get('https://www.unionpayintl.com/upload/jfimg/'.date('Ymd').'.json');
  216. if (! $response->ok()) {
  217. $response = self::$basicRequest->get('https://www.unionpayintl.com/upload/jfimg/'.date('Ymd', strtotime('-1 day')).'.json');
  218. }
  219. if ($response->ok()) {
  220. $data = $response->json();
  221. return collect($data['exchangeRateJson'])->where('baseCur', $target)->where('transCur', $base)->pluck('rateData')->first();
  222. }
  223. return null;
  224. }
  225. private static function baidu(string $base, string $target): ?float
  226. {
  227. $response = self::$basicRequest->get("https://finance.pae.baidu.com/vapi/async/v1?from_money=$base&to_money=$target&srcid=5293");
  228. if ($response->ok()) {
  229. $data = $response->json();
  230. if ($data['ResultCode'] !== -1) {
  231. return $data['Result'][0]['DisplayData']['resultData']['tplData']['money2_num'];
  232. }
  233. }
  234. return null;
  235. }
  236. }