Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save adminho/0159bb53c02bfdee1c4c31de3d8ecd92 to your computer and use it in GitHub Desktop.
Save adminho/0159bb53c02bfdee1c4c31de3d8ecd92 to your computer and use it in GitHub Desktop.
ตัวอย่างโค้ดเรียกใช้งานบริการ API ของธนาคารแห่งประเทศไทย
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//อ้างอิงคู่มือ ตามลิงค์นี้ https://iapi.bot.or.th/Developer?lang=th
// ตัวอย่างดู อัตราแลกเปลี่ยนถัวเฉลี่ยถ่วงน้ำหนักระหว่างธนาคาร (รายวัน)
$("button").click(function(){
$.ajax({
type : "GET",
url : "https://iapi.bot.or.th/Stat/Stat-ReferenceRate/DAILY_REF_RATE_V1/?start_period=2002-01-12&end_period=2002-01-15",
beforeSend: function(xhr){xhr.setRequestHeader('api-key', 'U9G1L457H6DCugT7VmBaEacbHV9RX0PySO05cYaGsm');},
success : function(result) {
$("#div1").html(JSON.stringify(result));
console.log(JSON.stringify(result));
},
error : function(result) {
//handle the error
}
});
});
});
</script>
</head>
<body>
<div id="div1"><h2>ผลลัพธ์ JSON จะแสดงไว้ตรงนี้</h2></div>
<br/>
<button>ทดสอบ API แบงค์ชาติ</button>
</body>
</html>
//อ้างอิงคู่มือ ตามลิงค์นี้ https://iapi.bot.or.th/Developer?lang=th
// ตัวอย่างดู อัตราแลกเปลี่ยนถัวเฉลี่ยถ่วงน้ำหนักระหว่างธนาคาร (รายวัน)
// must intall this package
// npm install request
var request = require('request');
var querystring = require('querystring');
data = {
'start_period': '2002-01-12',
'end_period': '2002-01-15'
}
options = {
url: 'https://iapi.bot.or.th/Stat/Stat-ReferenceRate/DAILY_REF_RATE_V1/?' + querystring.stringify(data),
headers: {
'api-key': 'U9G1L457H6DCugT7VmBaEacbHV9RX0PySO05cYaGsm'
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // print response
}
})
# อ้างอิงคู่มือ ตามลิงค์นี้ https://iapi.bot.or.th/Developer?lang=th
# ตัวอย่างดู อัตราแลกเปลี่ยนถัวเฉลี่ยถ่วงน้ำหนักระหว่างธนาคาร (รายวัน)
import requests
url = 'https://iapi.bot.or.th/Stat/Stat-ReferenceRate/DAILY_REF_RATE_V1/'
querystring = {'start_period': '2002-01-12', 'end_period': '2002-01-15'}
headers = {'api-key': 'U9G1L457H6DCugT7VmBaEacbHV9RX0PySO05cYaGsm'}
response = requests.request('GET', url, headers=headers, params=querystring)
print (response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment