Script PHP Mengambil Nilai Tukar Valuta Asing dari Beacukai

This item was filled under [ Code Snippet, PHP ]

Mirip seperti sebelumnya yg mengambil lewat BCA, akan tetapi skrg mengambil dari sumber beacukai.
Berikut adalah script php untuk menampilkan kondisi mata uang valuta asing mengambil data yang bersumber dari http://www.beacukai.go.id

Dengan menggunakan dua metode, menggunakan curl atau juga bisa menggunakan file_get_content().
Fungsi script PHP nya adalah sbb:

<?php
/*
Ambil Nilai Valuta Kurs Dari Beacukai Versi 1.0
Tanggal: 2008-12-30 17:04
oleh: jinbatsu (http://www.nusansifor.com) 
*/
error_reporting (E_ALL);
//
// Ubah menjadi 3600 untuk cache 1 jam, ketika semuanya sudah berjalan normal.
// Menggunakan cache berarti tidak perlu membuka koneksi ke klikbca
// setiap kali halaman dibuka << ini PENTING! menghemat waktu, dan mengurangi proses server.
//
$nkurs['cachetime'] = 3600; /* ubah jadi 3600 atau lebih */
//
// menggunakan CURL, jika file_get_contents tidak bisa dihostingan Anda, baca manual PHP untuk selengkapnya
// Untuk Cara penggunaan file get content:
// file_get_contents($url, "r")
// Untuk Cara penggunaan CURL:
// curl_get_file_contents($url)
$url = "http://www.beacukai.go.id/rates/exchRateID.php";	
$handle_url_metod = file_get_contents($url, "r");
//$handle_url_metod = curl_get_file_contents($url);
$header = '
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>Nilai Valuta Kurs Dari Beacukai Versi 1.0</title>
			<style>
			#kurs_beacukai {
				width: 350px;
				background: #FFFFFF;
				padding: 0;
				border: 0;
				font-size: 11px;
				font-family: Verdana, Arial, Helvetica, sans-serif;
			}
			#kurs_beacukai table {
				border: 0 solid #ccc;
				padding: 2px 2px;
			}
			#kurs_beacukai table tr td table {
				border: 1px solid #ccc;
				padding: 0;
			}
			#kurs_beacukai .style6 {
				font-size: 13px;
			}
			</style>
			</head>
 
			<body>
 
			<div id="kurs_beacukai">
';
 
$footer = '
			<br />
			Sumber: <a href="http://www.beacukai.go.id">beacukai</a>
			</div>
			</body>
			</html>
';	
 
 
 
 
// Dari sini kebawah, ubah kalau mengerti aja.
$nkurs['curr'] = array ('USD', 'SGD', 'HKD', 'CHF', 'GBP', 'AUD', 'JPY', 'SEK', 'DKK', 'CAD', 'EUR', 'SAR', 'MYR', 'NZD', 'NOK', 'BUK', 'INR', 'KWD', 'PKR', 'PHP', 'LKR', 'THB', 'BND', 'CNY', 'KRW');
 
 
function curl_get_file_contents($URL) {
	$c = curl_init();
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($c, CURLOPT_URL, $URL);
	$contents = curl_exec($c);
	curl_close($c);
	if ($contents) return $contents;
	else return FALSE;
}
 
$nkurs['scriptpath'] = dirname (__FILE__);
$nkurs['cachefile'] = $nkurs['scriptpath'] . '/cache.txt';
$output = "";
if (!file_exists ($nkurs['cachefile']) || !is_writable ($nkurs['cachefile'])){ die ('File cache.txt belum ada atau belum writable.<br />Buat file:
 
<div>::CODECOLORER_BLOCK_1::</div>
 
<br />Lalu CHMOD ke 666'); }
if (filemtime ($nkurs['cachefile']) <= ( time () - $nkurs['cachetime'] ) && $handle = $handle_url_metod)  {
	$output .= '<table width="100%"  border="0" cellspacing="0" cellpadding="0">';
	$handle = explode ('<table width="100%"  border="0" cellspacing="0" cellpadding="0">', $handle);
	$hasil2 = $handle[3];
	$hasil1 = explode('<tr>', $handle[3]);
	$i = 0;
	foreach($hasil1 as $hasil) {
		if ($i == 30) {continue;}
		$output .= "<tr>";
		$hasil = str_replace('untuk setiap', '', $hasil);
		$hasil = str_replace('1,00', '', $hasil);
		$hasil = str_replace('100,00', '', $hasil);
		$hasil = str_replace('border="1"', 'border="0"', $hasil);
		foreach($nkurs['curr'] as $ilangan) {
			$hasil = str_replace($ilangan, '', $hasil);
		}
		$output .= $hasil;
		//$output .= "<br />" . $i;
		//$output .= "</tr>";
		$i++;
	}
	$output .= '</table>';
	$tocache = $output;
	$handle = fopen ($nkurs['cachefile'], 'w');
	fwrite ($handle, $tocache);
	fclose ($handle);
	//$output .= $hasil2;
} else {
	$handle = file ($nkurs['cachefile']);
	foreach($handle as $out) {
		$output .= $out;
	}
}
 
 
 
echo $header;
echo $output;
echo $footer;
?>

Demo: klik disini
Download file contoh script komplit nya:

  kurs_beacukaiV1.rar (1.8 KiB, 1,659 hits)

semoga bermanfaat :mrgreen:

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 30,616 views
Tagged with: [ , , , , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Artikel Sejenis

14 Comments on “Script PHP Mengambil Nilai Tukar Valuta Asing dari Beacukai”

  • Mozilla Firefox Mozilla Firefox 3.0.5
    Windows Windows XP
    30 Dec 2008 19:44

    Thanks banget telah buatin ambil content bea cukainya. inilah ditunggu-tunggu :wink:

    Maju trus ya

    Thanks

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.0.5
    Windows Windows XP
    30 Dec 2008 22:15

    sama2 ya ndre :grin: semoga bermanfaat ya… :wink:

    Like or Dislike: Thumb up 0 Thumb down 0
  • verens
    Mozilla Firefox Mozilla Firefox 3.0.5
    Windows Windows XP
    16 Jan 2009 13:36

    kalo ngambil secara otomatis tiap ada perubahan nilai valas di http://www.beacukai.go.id/rates/exchRateID.php gimana ya?

    thx

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.0.5
    Windows Windows XP
    16 Jan 2009 18:27

    @verens,
    Script PHP Mengambil Nilai Tukar Valuta Asing dari Beacukai dan alamat url yg dipakai adalah: “http://www.beacukai.go.id/rates/exchRateID.php”

    Dan diambil secara berkala tergantung settingan:

    $nkurs['cachetime'] = 3600;

    3600 = 1 jam.

    Jadi tiap 1 jam ambil data dari website beacukai nya… jadi hasilnya akan sama terus dai sumber url datanya… :)

    Like or Dislike: Thumb up 0 Thumb down 0
  • verens
    Mozilla Firefox Mozilla Firefox 3.0.5
    Windows Windows XP
    17 Jan 2009 9:16

    ow.. makasi banyak, mas..

    Like or Dislike: Thumb up 0 Thumb down 0
  • hariyanto
    Mozilla Firefox Mozilla Firefox 2.0.0.3
    Ubuntu Linux Ubuntu Linux
    12 Feb 2009 12:30

    thx atas scriptnya, btw kalo ambil kurs dari http://www.depkeu.go.id url nya diganti apa ya, sebab kadang di http://www.beacukai.go.id lelet.

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.0.6
    Windows Windows XP
    24 Feb 2009 21:27

    @hariyanto,
    terimakasih atas komen nya..
    OK, nanti saya akan coba ya..

    Like or Dislike: Thumb up 0 Thumb down 0
  • sasa
    Mozilla Firefox Mozilla Firefox 3.0.14
    Windows Windows XP
    27 Oct 2009 9:01

    mas, kalau data yang mau diambil harus login, trus script nya gmn?

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.5.4
    Windows Windows XP
    31 Oct 2009 23:43

    @sasa, harus menggunakan teknik CURL yg menggunakan POST data login informasinya..
    berikut contohnya saya kasih url linknya:
    http://snipplr.com/view/17733/php-curl-post/

    Like or Dislike: Thumb up 0 Thumb down 0
  • eljawawi
    Mozilla Firefox Mozilla Firefox 3.6.13
    Windows Windows 7
    12 Feb 2011 23:47

    mas, kalo hasil grab tsb disimpan di database mysql gimana caranya?
    terima kasih

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.6.8
    Windows Windows XP
    14 Feb 2011 17:45

    @eljawawi, maaf saya blom meiliki scriptnya, tapi bisa saja, sesuaikan dengan database, lalu insert pada tabel data per tanggal.

    Like or Dislike: Thumb up 0 Thumb down 0
  • Fathur
    Google Chrome Google Chrome 15.0.874.121
    Windows Windows XP
    16 Dec 2011 8:26

    Mas, sekarang link dari bea cukainya udh ganti ya? ada update ga scriptnya?

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 8.0
    Ubuntu Linux Ubuntu Linux
    17 Dec 2011 1:16

    @Fathur, wah iya mas, ternyata website beacukai berubah total yah, lebih keren skrng, nanti kl saya ada waktu saya coba2 untuk update.

    Makasih atas info nya :mrgreen:

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 11.0
    Windows Windows 7
    20 Apr 2012 1:07

    ikut nyimak.. kunjungan perdana nih salam kenal gan :)

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Halaman ini di eksekusi dalam waktu 1.578 detik! (koneksi mayan bagus nih...)