Assalaamu ‘alaikum warahmatullaahi wabarakaatuh semua Teman-teman yang hobicodingdotcom  ,

Kemaren oprek2 caranya gimana yah untuk menampilkan API dari rajaongkir yang PRO ke select dropdown di framework codeigniter, alhamdullilah allah.swt memudahkan semuanya, akhirnya ketemu juga hihi,

fungsi didalamnya :

get_provinsi, get_kota/kabupate, get kecamatan, dan harga setiap kurir bersarakan dari kota/kabupaten ke kecamatan.

Semoga bermanfaat yah untuk semua.. #ga akan jatuh miskin ketika kita berbagi ilmu..

berikut scriptnya controller, view, dan function javascript ,

Controller :

function rajaongkir_get_provinsi(){
        
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://pro.rajaongkir.com/api/province",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "key: key-rajaongkir"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          //echo $response;
        }
        
        $obj = json_decode($response, true);
        $select_prov = `<option value=0>- Pilih Provinsi -</option>`;
        for($i=0; $i < count($obj[`rajaongkir`][`results`]); $i++){
             $select_prov .= "<option value=`".$obj[`rajaongkir`][`results`][$i][`province_id`]."`>".$obj[`rajaongkir`][`results`][$i][`province`]."</option>";
        }
        
        echo $select_prov;
        
    }
    
    function rajaongkir_get_kota(){
        
        $id_province = $this->input->post(`id_province`,TRUE);
        
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://pro.rajaongkir.com/api/city?province=$id_province",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "key: key-rajaongkir"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          //echo $response;
        }
        
        $obj = json_decode($response, true);
        $select_kotkab = `<option value=0>- Pilih Kota / Kabupaten -</option>`;
        for($i=0; $i < count($obj[`rajaongkir`][`results`]); $i++){
             $select_kotkab .= "<option value=`".$obj[`rajaongkir`][`results`][$i][`city_id`]."`>".$obj[`rajaongkir`][`results`][$i][`city_name`]."</option>";
        }
        
        echo $select_kotkab;
        
    }
    
    
    function rajaongkir_get_kecamatan(){
        
        $id_kota = $this->input->post(`id_kota`,TRUE);
        
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://pro.rajaongkir.com/api/subdistrict?city=$id_kota",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "key: key-rajaongkir"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          //echo $response;
        }
        
        $obj = json_decode($response, true);
        $select_kecamatan = `<option value=0>- Pilih Kecamatan -</option>`;
        for($i=0; $i < count($obj[`rajaongkir`][`results`]); $i++){
             $select_kecamatan .= "<option value=`".$obj[`rajaongkir`][`results`][$i][`subdistrict_id`]."`>".$obj[`rajaongkir`][`results`][$i][`subdistrict_name`]."</option>";
        }
        
        echo $select_kecamatan;
        
    }
    
    
    function rajaongkir_get_cost(){
        //origin : kota asal ( ini karna kita dari tangerang selatan, kita default aja kota tangerang selatan = 457)
        $kota_asal      = 457;
        //$kecamatan_asal      = 6314;
        //destination : kota tujuan
        $kecamatan_tujuan    = $this->input->post(`kecamatan_tujuan`,TRUE);
        //kurir pengiriman
        $kurir          = $this->input->post(`kurir_pengiriman`,TRUE);
        //berat
        $berat          = `900`;
        //&courier=jne
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://pro.rajaongkir.com/api/cost",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "origin=$kota_asal&originType=city&destination=$kecamatan_tujuan&destinationType=subdistrict&weight=$berat&courier=$kurir",
            CURLOPT_HTTPHEADER => array(
              "content-type: application/x-www-form-urlencoded",
              "key: key-rajaongkir"
            ),
          ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          //echo $response;
        }
        
        $obj = json_decode($response, true);
        
        $datas;
        for($i=0; $i < count($obj[`rajaongkir`][`results`]); $i++){
            //$datas .= $obj[`rajaongkir`][`results`][$i][`name`];
            
            for($j=0; $j < count($obj[`rajaongkir`][`results`][$i][`costs`]); $j++){
                
                $nama_pengiriman = $obj[`rajaongkir`][`results`][$i][`name`];
                $service         = $obj[`rajaongkir`][`results`][$i][`costs`][$j][`service`];
                $biaya           = $obj[`rajaongkir`][`results`][$i][`costs`][$j][`cost`][0][`value`];
                $biaya_format    = number_format($obj[`rajaongkir`][`results`][$i][`costs`][$j][`cost`][0][`value`]);
            
                $datas .=`<li id="serv-`.$service.`" onclick="klikongkir(``.$nama_pengiriman.``,``.$service.``,``.$biaya.``,``.$biaya_format.``)" class="list-group-item clearall-kurir" style="cursor:pointer;margin:1px;"><span style="color:black;font-weight:bold">`.$obj[`rajaongkir`][`results`][$i][`name`].` - `.$service.`</span> <br> <small><b style="color:red">Rp. `.number_format($obj[`rajaongkir`][`results`][$i][`costs`][$j][`cost`][0][`value`]).`</b></small> / <small>Pengiriman : `.$obj[`rajaongkir`][`results`][$i][`costs`][$j][`cost`][0][`etd`].` hari</small></li>`;

            }
            
        }
        
        echo $datas;
        
    }

Javascript:

//* select Provinsi */
var base_url    = "<?php echo base_url();?>";
$.ajax({
    type: `post`,
    url: base_url + `nama-controller/rajaongkir_get_provinsi`,
    data: {},
    dataType  : `html`,
    success: function (data) {
        $("#list_provinsi").html(data);
    }
});
/* select Provinsi */

$("#list_provinsi").change(function(){
    var id_province = this.value;
    kota(id_province);
    $("#div_kota").show();
});

/* select Kota */
kota = function(id_province){
    $.ajax({
    type: `post`,
    url: base_url + `nama-controller/rajaongkir_get_kota`,
    data: {id_province:id_province},
    dataType  : `html`,
    success: function (data) {
        $("#list_kotakab").html(data);
    },
    beforeSend: function () {
        
    },
    complete: function () {
      
    }
});

}

$("#list_kotakab").change(function(){
    var id_kota = this.value;
    kecamatan(id_kota);
    $("#div_kecamatan").show();
});

kecamatan = function(id_kota){
    $.ajax({
    type: `post`,
    url: base_url + `nama-controller/rajaongkir_get_kecamatan`,
    data: {id_kota:id_kota},
    dataType  : `html`,
    success: function (data) {
        $("#list_kecamatan").html(data);
    }
});

}
$("#list_kurir").change(function(){
    var id_kurir     = this.value;
    var id_kecamatan      = $("#list_kecamatan").val();
    cost(id_kurir,id_kecamatan);
    $("#div_kurir").show();
});


cost = function(id_kurir,id_kecamatan){
    $.ajax({
    type: `post`,
    url: base_url + `nama-controller/rajaongkir_get_cost`,
    data: {kurir_pengiriman:id_kurir,kecamatan_tujuan:id_kecamatan},
    dataType  : `html`,
    success: function (data) {
        $("#list_kurir_div").html(data);
    }
});

}

View :

<div class="form-group required">
                                            <label for="input-payment-zone" class="control-label">Provinsi</label>
                                            <select class="form-control" id="list_provinsi"></select>
                                        </div>
                                        <div class="form-group required" style="display:none" id="div_kota">
                                              <label for="input-payment-city" class="control-label">Kota / Kabupaten</label>
                                              <select class="form-control" id="list_kotakab"></select>
                                        </div>
                                        <div class="form-group required" style="display:none" id="div_kecamatan">
                                              <label for="input-payment-city" class="control-label">Kecamatan</label>
                                              <select class="form-control" id="list_kecamatan"></select>
                                        </div>



                                        <label for="input-payment-postcode" class="control-label"><b>Kurir Pengiriman</b></label>
                                        <select class="form-control" id="list_kurir" style="margin-bottom: 2px">
                                            <option value="0">- Pilih Kurir Pengiriman -</option>
                                            <option value="jne">JNE</option>
                                            <option value="tiki">TIKI</option>
                                            <option value="sicepat">SiCepat Express</option>
                                            <option value="pos">POS Indonesia</option>
                                            <option value="jet">JET Express</option>
                                            <option value="lion">Lion Parcel</option>
                                            <option value="ninja">Ninja Xpress</option>
                                            <option value="wahana">Wahana Prestasi Logistik</option>
                                            <option value="rpx">RPX Holding</option>
                                        </select>
                                        
                                        <ul class="list-group list-group-flush">
                                            <div id="list_kurir_div"></div>
                                        </ul>