• Đăng ký
  • Đăng nhập

Lấy tỷ giá và giá vàng tự động cho website

📅 — 👀 1409 — 👦

Trang này có hữu ích không?

👍 Có (24)        👎 Không (19)


Đây là đoạn code đơn giản để lấy tỷ giá & giá vàng từ Ngân hàng Đông Á cho website của mình.

Nguồn dữ liệu cần lấy :

  1. https://www.dongabank.com.vn/exchange/export

Link Demo => https://www.vk2.cc/tin-tuc/

Ở đây, mình áp dụng code cho trang WordPress, bạn chỉ cần copy code vào file sidebar.php (cùng cấp với file function.php) trước dòng <?php if (is_active_sidebar('primary-widget-area')): ?> là được

 

Code CSS:

<style>
.kk-table{
	display: table;
    width: 100%;
}
.kk-table thead tr th {
    text-align: center;
    background: honeydew;	
	padding: 0;
    height: 30px;
    vertical-align: middle;
    border: 1px solid #e8e8e8;
    font-size: 80%;
	font-weight:bold;
	
}
.kk-table tbody tr td {
    padding: 8px;
    font-size: 90%;
}
</style>

 

Code PHP:

function kk_get_data($website) {		
	$curl_handle=curl_init();
	curl_setopt($curl_handle, CURLOPT_URL, $website);
	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_handle, CURLOPT_USERAGENT, 'VK2.cc');
	curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);

	$query = curl_exec($curl_handle);
	curl_close($curl_handle);
	
	return $query;
}

//Nếu trong wordpress thì dòng này có nghĩa là chỉ hiển thị trên trang front end
if (is_front_page()) 
{
	$content = kk_get_data('https://www.dongabank.com.vn/exchange/export');

	$content = str_replace('({"items":', '', $content);
	$content = str_replace('})', '', $content);
	$tygia = json_decode($content, true);		
	
	$tgv = [];
	if (count($tygia) > 0)
	{
		echo '<div class="side-widget ">	';
		echo '<h3 class="heading-1"><span>Tỷ giá ngoại tệ</span></h3>';
		echo '<table class="cart-table table table-bordered kk-table">';
		echo '<thead><tr> <th rowspan="2">Mã</th> <th colspan="2">Mua </th> <th rowspan="2"> <div> Bán </div></th> </tr><tr> <th>Tiền mặt</th> <th>Chuyển khoản</th> </tr></thead>';
		foreach($tygia as $tg)
		{
			if ($tg['type'] =='PNJ_DAB') $tgv = $tg;
			else echo '<tr><td>'.$tg['type'].'</td><td class="text-right">'.number_format((float)$tg['muatienmat'], 0, '', '.').'</td><td class="text-right">'.number_format((float)$tg['muack'], 0, '', '.').'</td><td class="text-right">'.number_format((float)$tg['bantienmat'], 0, '', '.').'</td></tr>';
		}
		echo '</table>';		
		echo '<div class="post-subscribe margin-bottom-30"><h3 class="heading-1">Tỷ giá VÀNG (PNJ)</h3><div class="col-md-6 col-xs-6"><label class="badge">Mua</label><h4>'.number_format((float)$tgv['muatienmat'], 0, '', '.').'</h4></div><div class="col-md-6 col-xs-6"><label class="badge badge-tech">Bán</label><h4>'.number_format((float)$tgv['bantienmat'], 0, '', '.').'</h4></div><span class="margin-top-70 text-right">Nguồn : Đông Á Bank <i>(Ngày: '.date("d-m-Y").')</i></span></div>';		
		echo '</div>';
	}
}

Trả lời


📁 Wordpress
🔖