How to grab BCA USD buy/sell rate with cURL?

This will grab current rate of USD rate from BCA using cURL.

function delAttr($tagSource) {
$stripAttrib = "' (style|class)=\"(.*?)\"'i";
$tagSource = stripslashes($tagSource);
$tagSource = preg_replace($stripAttrib, '', $tagSource);
return $tagSource;
}

function delTags($source) {
$allowedTags='';
$source = strip_tags($source, $allowedTags);
return preg_replace('/<(.*?)>/ie', "'<'.delAttr('\\1').'>'", $source);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.klikbca.com/individual/silver/ind/rates.html");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$ret = curl_exec($ch);
$cinfo = curl_getinfo($ch);
curl_close($ch);
if ($cinfo['http_code'] != 200) die("Cannot connect!");
$ret = delTags(str_replace(array(" ", " "), "", $ret));
$time = strstr($ret, 'DD/TT');
$time = explode("\n", $time);
$kurs = strstr($ret, 'USD');
$ret = explode("\n", $kurs);
echo "Kurs BCA per ".$time[1].$time[2]."
";
echo "Jual: ".$ret[1]."
";
echo "Beli: ".$ret[2];

Tagged with 
About sepedatua
I am nothing special, of this I am sure. I am a common man with common thoughts and I’ve led a common life. There are no monuments dedicated to me and my name will soon be forgotten, but I’ve loved another with all my heart and soul, and to me, this has always been enough.

Leave a Reply