public function action_ongkir() { $result = []; if (!isset($_GET['asal']) and !isset($_GET['tujuan']) and !isset($_GET['berat'])) { $result["status"] = "Please insert search keyword"; } else { $asal = $_GET['asal']; $tujuan = $_GET['tujuan']; $berat = $_GET['berat']; $rest = new REST_Ongkir(array('server' => 'http://api.ongkir.info/')); $get_data = $rest->post('cost/find', array('from' => $asal, 'to' => $tujuan, 'weight' => $berat, 'courier' => 'jne', 'API-Key' => $this->ongkir_api_key, 'format' => 'json')); } // print_r($get_data);die(); if ($get_data and is_object($get_data)) { $result['output'] = $get_data; $result['output']->city = ["origin" => $asal, "destination" => $tujuan]; $result['output']->weight = $berat; } elseif (!is_object($get_data)) { $result["status"] = "Data error ......"; } else { $result['output'] = $get_data; $result['output']->weight = $berat; // $result['output']->city = [] // $result['output']->city['origin'] = $asal ; // $result['output']->city['destination'] = $tujuan ; } echo json_encode($result); }
public function get_cost() { $this->load->library('REST_Ongkir'); $rest = new REST_Ongkir(array('server' => 'http://api.ongkir.info/')); $result = $rest->post('cost/find', array('from' => 'bandung', 'to' => 'bandung', 'weight' => 1000, 'courier' => 'jne', 'API-Key' => 'a1edf4ed1599db0e7cb5b8e23769ba0d')); try { $status = $result['status']; // Handling the data if ($status->code == 0) { $prices = $result['price']; $city = $result['city']; echo 'Ongkos kirim dari ' . $city->origin . ' ke ' . $city->destination . '<br /><br />'; foreach ($prices->item as $item) { echo 'Layanan: ' . $item->service . ', dengan harga : Rp. ' . $item->value . ',- <br />'; } } else { echo 'Tidak ditemukan jalur pengiriman dari bandung ke jakarta'; } } catch (Exception $e) { echo 'Processing error.'; } }