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];

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: