{"id":3316,"date":"2025-12-01T18:06:35","date_gmt":"2025-12-01T10:06:35","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=3316"},"modified":"2025-12-01T18:06:35","modified_gmt":"2025-12-01T10:06:35","slug":"php-%e5%8f%96%e5%be%97%e7%95%b6%e5%89%8d%e5%8c%af%e7%8e%87","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=3316","title":{"rendered":"PHP \u53d6\u5f97\u7576\u524d\u532f\u7387"},"content":{"rendered":"\n<p>\u9019\u908a\u4f7f\u7528\u7db2\u8def\u4e0a\u7684\u516c\u958b\u8cc7\u8a0a\u67e5\u8a62\uff0c\u6709\u9700\u8981\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<\/p>\n\n\n\n<p>\u67e5\u8a62\u570b\u5bb6 https:\/\/restcountries.com<\/p>\n\n\n\n<p>\u67e5\u8a62\u532f\u7387 <a href=\"https:\/\/open.er-api.com\">https:\/\/open.er-api.com<\/a><\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\nfunction getExchangeRateFree($input, $target = 'TWD') {\n    $debug = \"\";\n    $keywordMap = &#91;\n        \/\/ \u7f8e\u5143\n    '\u7f8e\u5143' => 'USD', '\u7f8e\u91d1' => 'USD', '\u7f8e\u570b' => 'USD',\n    \/\/ \u65e5\u5713\n    '\u65e5\u5713' => 'JPY', '\u65e5\u5e63' => 'JPY', '\u65e5\u672c' => 'JPY',\n    \/\/ \u53f0\u5e63\n    '\u53f0\u5e63' => 'TWD', '\u65b0\u53f0\u5e63' => 'TWD', '\u53f0\u7063' => 'TWD',\n    \/\/ \u6b50\u5143\n    '\u6b50\u5143' => 'EUR', '\u6b50\u6d32' => 'EUR',\n    \/\/ \u4eba\u6c11\u5e63\n    '\u4eba\u6c11\u5e63' => 'CNY', '\u4e2d\u570b' => 'CNY', 'RMB' => 'CNY',\n    \/\/ \u82f1\u938a\n    '\u82f1\u938a' => 'GBP', '\u82f1\u570b' => 'GBP', 'UK' => 'GBP',\n    \/\/ \u6fb3\u5e63\n    '\u6fb3\u5e63' => 'AUD', '\u6fb3\u6d32' => 'AUD', '\u6fb3\u5927\u5229\u4e9e' => 'AUD',\n    \/\/ \u6e2f\u5e63\n    '\u6e2f\u5e63' => 'HKD', '\u9999\u6e2f' => 'HKD', 'HK' => 'HKD',\n    \/\/ \u65b0\u52a0\u5761\u5e63\n    '\u65b0\u5e63' => 'SGD', '\u65b0\u52a0\u5761' => 'SGD', 'Singapore' => 'SGD',\n    \/\/ \u97d3\u5143\n    '\u97d3\u5143' => 'KRW', '\u97d3\u570b' => 'KRW', '\u5357\u97d3' => 'KRW',\n    \/\/ \u6cf0\u9296\n    '\u6cf0\u9296' => 'THB', '\u6cf0\u570b' => 'THB',\n    \/\/ \u52a0\u5e63\n    '\u52a0\u5e63' => 'CAD', '\u52a0\u62ff\u5927' => 'CAD', 'CA' => 'CAD',\n    \/\/ \u745e\u58eb\u6cd5\u90ce\n    '\u745e\u58eb\u6cd5\u90ce' => 'CHF', '\u745e\u58eb' => 'CHF',\n    \/\/ \u7d10\u5143\n    '\u7d10\u5e63' => 'NZD', '\u7d10\u897f\u862d' => 'NZD',\n    \/\/ \u99ac\u4f86\u5e63\n    '\u99ac\u5e63' => 'MYR', '\u99ac\u4f86\u897f\u4e9e' => 'MYR',\n    ];\n\n    $input = trim($input);\n    $currencyCode = null;\n\n    \/\/ Step 0: \u5982\u679c\u8f38\u5165\u662f\u4e09\u500b\u82f1\u6587\u5b57\u6bcd\uff0c\u76f4\u63a5\u7576\u4f5c\u5e63\u5225\n    if (preg_match('\/^&#91;A-Z]{3}$\/i', $input)) {\n        $currencyCode = strtoupper($input);\n    }\n\n    \/\/ Step 1: \u5148\u67e5 keywordMap\n    if (!$currencyCode) {\n        foreach ($keywordMap as $kw => $code) {\n            if (mb_stripos($input, $kw) !== false) {\n                $currencyCode = $code;\n                break;\n            }\n        }\n    }\n\n    \/\/ Step 2: \u5982\u679c\u9084\u6c92\u627e\u5230\uff0c\u67e5\u570b\u5bb6\u5c0d\u61c9\u5e63\u5225\n    if (!$currencyCode) { \n        if($inputCountry_code != null){\n            $input = $inputCountry_code;\n        }\n\n        $apiUrl = \"https:\/\/restcountries.com\/v3.1\/alpha\/\" . urlencode($input);\n        $json = @file_get_contents($apiUrl);\n        if ($json) {\n            $data = json_decode($json, true);\n            if (!empty($data&#91;0]&#91;'currencies'])) {\n                $currencyCode = array_keys($data&#91;0]&#91;'currencies'])&#91;0];\n            }\n        }\n    }\n\n    \/\/ Step 3: \u5982\u679c\u9084\u662f\u6c92\u627e\u5230\uff0c\u76f4\u63a5\u7528\u8f38\u5165\u7684\u503c\n    if (!$currencyCode) {\n        $currencyCode = strtoupper($input);\n    }\n\n    \/\/ Step 4: \u67e5\u532f\u7387\n    $url = \"https:\/\/open.er-api.com\/v6\/latest\/\".$target;\n    $json = @file_get_contents($url);\n    if (!$json) return \"\u26a0\ufe0f \u532f\u7387\u4f3a\u670d\u5668\u7121\u56de\u61c9\";\n    $data = json_decode($json, true);\n\n    if (!isset($data&#91;'rates']&#91;$currencyCode])) {\n        return \"\u26a0\ufe0f \u627e\u4e0d\u5230 {$currencyCode} \u5c0d {$target} \u532f\u7387\";\n    }\n\n    $rate = round((1\/$data&#91;'rates']&#91;$currencyCode]), 4);\n\n    $timestamp = strtotime($data&#91;'time_last_update_utc']);\n    $date = date('Y-m-d', $timestamp);\n\n    return $debug.\"\ud83d\udcb1 {$currencyCode} \u5c0d {$target} \u532f\u7387\uff1a{$rate}\\n\ud83d\udcc5 \u66f4\u65b0\u65e5\u671f\uff1a{$date}\";\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9019\u908a\u4f7f\u7528\u7db2\u8def\u4e0a\u7684\u516c\u958b\u8cc7\u8a0a\u67e5\u8a62\uff0c\u6709\u9700\u8981\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 \u67e5\u8a62\u570b\u5bb6 https:\/\/restcountries.com &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.quantoyo.com\/?p=3316\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008PHP \u53d6\u5f97\u7576\u524d\u532f\u7387\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[49,613,612,76,614],"class_list":["post-3316","post","type-post","status-publish","format-standard","hentry","category-technology","tag-curl","tag-https-open-er-api-com","tag-https-restcountries-com","tag-php","tag-614"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3316"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3316\/revisions"}],"predecessor-version":[{"id":3317,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3316\/revisions\/3317"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}