Senin, 03 Januari 2011

grab kurs vallas BCA dengan PHP

bca.php

<?php
function grab_kurs(){
    $url = 'http://www.klikbca.com/individual/silver/ind/rates.html';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    set_time_limit(0);
    $handle = curl_exec($ch);

    $handle = explode ('<table border="0" cellspacing="0" cellpadding="0" class="testL">', $handle);
    $handle = explode ('</table>', $handle[1]);
    $handle = explode ('<TR', $handle[0]);
    $er = array ();
    $out = array ('&nbsp;&nbsp;&nbsp;&nbsp;', '</center>', '</td>', '<td valign="top" align="right">');
    foreach ($handle as $rows)
    {
        if (!isset ($er_last_update))
        {
            $n = 0;
            $last_update = explode ('<font color="black" size="1" face="Arial">', $rows);
            $last_update = explode ('</font></div></td>', $last_update[1]);
            $last_update = explode ("\n", $last_update[0]);
            $er_last_update = trim ($last_update[0]) . ' ' . trim ($last_update[1]) . '</span>';
        }
        else
        {
            $line = explode ("\n", $rows);
            $er[$n] = array (
            'mata_uang' => trim (str_replace ($out, "", $line[2])),
            'jual' => trim (str_replace ($out, "", $line[3])),
            'beli' => trim (str_replace ($out, "", $line[4]))
            );
            $n = $n + 1;
        }
    }
    $line = array ();
    foreach ($er as $lines)
    {
        $line[] = implode ("|", $lines);
    }
    $line[] = $er_last_update;
    $handle = implode ("\n", $line);

    return $er;

}

$er_array = grab_kurs();

// create a new XML document
$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('kurs');
$root = $doc->appendChild($root);

foreach ($er_array as $data) {
   
    // add node for each row
    $occ = $doc->createElement('nilai');
    $occ = $root->appendChild($occ);

    $child = $doc->createElement('mata_uang');
    $child = $occ->appendChild($child);
    $value = $doc->createTextNode($data['mata_uang']);
    $value = $child->appendChild($value);

    $child = $doc->createElement('jual');
    $child = $occ->appendChild($child);
    $value = $doc->createTextNode($data['jual']);
    $value = $child->appendChild($value);

      $child = $doc->createElement('beli');
    $child = $occ->appendChild($child);
    $value = $doc->createTextNode($data['beli']);
      $value = $child->appendChild($value);
}


// get completed xml document
$xml_string = $doc->saveXML();

header('Content-Type: application/xml; charset=ISO-8859-1');
header('Cache-Control: no-cache');
header('Pragma: no-cache');

echo $xml_string


?>




thejs.js

$(document).ready(function() {
 
  var data = "";
  
   loadPage();
 
  $("#getData").click(function(){

   loadPage();

  });

});

function loadPage(){
 
  $.get("http://www.xxxx.com/bca.php", function(theXML){

   $('nilai', theXML).each(function(i){

   var mata_uang = $(this).find("mata_uang").text();
   var jual = $(this).find("jual").text();
   var beli = $(this).find("beli").text();

   data = data + "<li><a href=\"#page\">" + mata_uang + "<small>" + beli + "</small><small>" + jual + "</small></li>";

   });

   $("#container").html(data);
  });
  
}




code HTML

<html>
    <head>
        <meta http-equiv="Pragma" content="no-cache">
        <title>The HTML</title>
        <link rel="stylesheet" type="text/css" href="jqtouch.css">
        <link rel="stylesheet" type="text/css" href="jqt/theme-edit.css">

       
        <script type="text/javascript" src="js/jquery.1.3.2.min.js"></script>
        <script type="text/javascript" src="js/jqtouch.js"></script>
        <script type="text/javascript" src="js/thejs.js"></script>
       
        <script type="text/javascript">
              var jQT = $.jQTouch({
            icon: 'icon.png',
            statusBar: 'black-translucent',
            startupScreen: 'startup.png'
        });
        </script>
       

    </head>
<body>
    <div id="home">
        <div class="toolbar">
        <h1>KURS</h1>
        <input type="button" id="getData" name="getData" value="Get!" />
        </div>
        <ul class="edgetoedge">
            <li class="sep">Mata uang<small>Beli</small><small>Jual</small></li>
        </ul>
        <ul class="plastic">
            <div id="container"></div>
        </ul>
    </div>
</body>
</html>

Tidak ada komentar:

Posting Komentar