JPN NRIC checker php wrapper | semak ic

This code is for educational purpose only. I don’t know whether it is legal or not.. so I just release it for an educational purpose only especially for university students who doing final year project. You can make your project look cool by integrating with live government system.. ;)

test here http://api.ibnuyahya.com/jpn.class.php
or download http://api.ibnuyahya.com/jpn.class.php.txt

/**
 * JPN NRIC wrapper
 *
 * This class created for Educational purpose only. Use on your own risk!
 *
 *
 * @owner      http://ibnuyahya.com
 * @license    GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * @version    1.0
 */

class jpn {

    private $jpnUrl     = 'http://www.jpn.gov.my/semakic/check.php';
    private $jpnReferer = 'http://www.jpn.gov.my/semakic/index.php';

    function __construct() {

    }

    function nricGrabber($nric = '') {
        $ckfile = tempnam("/tmp", "CURLCOOKIE");
        $post_data = "nokpt=$nric&Submit=Semak";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->jpnUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        curl_setopt($ch, CURLOPT_REFERER, $this->jpnReferer);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $return_data = curl_exec($ch);
        curl_close($ch);

        return $return_data;
    }

    function getNric($nric = '') {
        $data = $this->nricGrabber($nric);
        $pattern = '/<table.*?>(.*?)<\/table>/';
        preg_match($pattern, $data, $matches, PREG_OFFSET_CAPTURE);
        $data = $matches[1][0];
        $pattern = '/<span.*?>(.*?)<\/span>/';
        preg_match_all($pattern, $data, $matches);

        return $matches[0];
    }

}

//Sample how to use it.
$ayam = new jpn();
$tekek = $ayam->getNric('790111011123');
echo '<pre>';
print_r($tekek);
echo '</pre>';

*update 29/12/2011 – this code is no longer valid since JPN changed their page and filter the result.

About ibnuyahya

Ibnuyahya has written 104 post in this blog.

Ibnuyahya merupakan seorang software developer dan freelance consultant. Berpengalaman lebih dari 10 tahun dalam membangunkan perisian dari embedded sistem menggunakan micro-controller, windows application, mobile, image processing, artificial intelligent dan akhirnya menumpukan sepenuh tenaga dalam web application.