├── README.md ├── config └── rajaongkir.php └── libraries ├── RajaOngkir ├── endpoints.php └── restclient.php └── rajaongkir.php /README.md: -------------------------------------------------------------------------------- 1 | # RajaOngkir CodeIgniter 2 | Merupakan library CodeIgniter untuk mengkonsumsi API dari [RajaOngkir](http://rajaongkir.com). 3 | ## Instalasi 4 | Copy file sesuai dengan lokasinya masing-masing. 5 | ## Konfigurasi 6 | Buka "**application/config/rajaongkir.php**" dan masukkan API key di sana. 7 | ## Contoh Penggunaan 8 | ### Memuat library 9 | ```php 10 | $this->load->library('rajaongkir'); 11 | ``` 12 | ### Melakukan request 13 | ```php 14 | //Mendapatkan semua propinsi 15 | $provinces = $this->rajaongkir->province(); 16 | 17 | //Mendapatkan semua kota 18 | $cities = $this->rajaongkir->city(); 19 | 20 | //Mendapatkan data ongkos kirim 21 | $cost = $this->rajaongkir->cost(501, 114, 1000, "jne"); 22 | ``` 23 | ### Response 24 | Response yang dihasilkan berupa string JSON balasan dari RajaOngkir. 25 | ### Dokumentasi lebih lanjut 26 | Silakan lihat code di dalam library, di dalamnya terdapat komentar yang dapat membantu Anda. 27 | ### Referensi 28 | [Dokumentasi RajaOngkir](http://rajaongkir.com/dokumentasi) -------------------------------------------------------------------------------- /config/rajaongkir.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | require_once 'restclient.php'; 9 | 10 | class Endpoints { 11 | 12 | private $api_key; 13 | private $account_type; 14 | 15 | public function __construct($api_key, $account_type) { 16 | $this->api_key = $api_key; 17 | $this->account_type = $account_type; 18 | } 19 | 20 | /** 21 | * Fungsi untuk mendapatkan data propinsi di Indonesia 22 | * @param integer $province_id ID propinsi, jika NULL tampilkan semua propinsi 23 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 24 | */ 25 | function province($province_id = NULL) { 26 | $params = (is_null($province_id)) ? array() : array('id' => $province_id); 27 | $rest_client = new RESTClient($this->api_key, 'province', $this->account_type); 28 | return $rest_client->get($params); 29 | } 30 | 31 | /** 32 | * Fungsi untuk mendapatkan data kota di Indonesia 33 | * @param integer $province_id ID propinsi 34 | * @param integer $city_id ID kota, jika ID propinsi dan kota NULL maka tampilkan semua kota 35 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 36 | */ 37 | function city($province_id = NULL, $city_id = NULL) { 38 | $params = (is_null($province_id)) ? array() : array('province' => $province_id); 39 | if (!is_null($city_id)) { 40 | $params['id'] = $city_id; 41 | } 42 | $rest_client = new RESTClient($this->api_key, 'city', $this->account_type); 43 | return $rest_client->get($params); 44 | } 45 | 46 | /** 47 | * Fungsi untuk mendapatkan data ongkos kirim 48 | * @param integer $origin ID kota asal 49 | * @param integer $destination ID kota tujuan 50 | * @param integer $weight Berat kiriman dalam gram 51 | * @param string $courier Kode kurir 52 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 53 | */ 54 | function cost($origin, $destination, $weight, $courier) { 55 | $params = array( 56 | 'origin' => $origin, 57 | 'destination' => $destination, 58 | 'weight' => $weight, 59 | 'courier' => $courier 60 | ); 61 | $rest_client = new RESTClient($this->api_key, 'cost', $this->account_type); 62 | return $rest_client->post($params); 63 | } 64 | 65 | /** 66 | * Fungsi untuk mendapatkan daftar/nama kota yang mendukung pengiriman Internasional 67 | * 68 | * @param integer $province_id ID propinsi 69 | * @param integer $city_id ID kota, jika ID propinsi dan ID kota NULL maka tampilkan semua kota 70 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 71 | */ 72 | function internationalOrigin($province_id = NULL, $city_id = NULL) { 73 | $params = (is_null($province_id)) ? array() : array('province' => $province_id); 74 | if (!is_null($city_id)) { 75 | $params['id'] = $city_id; 76 | } 77 | $rest_client = new RESTClient($this->api_key, 'internationalOrigin', $this->account_type); 78 | return $rest_client->get($params); 79 | } 80 | 81 | /** 82 | * Fungsi untuk mendapatkan daftar/nama negara tujuan pengiriman internasional 83 | * 84 | * @param integer ID negara, jika kosong maka akan menampilkan semua negara 85 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 86 | */ 87 | function internationalDestination($country_id = NULL) { 88 | $params = (is_null($country_id)) ? array() : array('id' => $country_id); 89 | $rest_client = new RESTClient($this->api_key, 'internationalDestination', $this->account_type); 90 | return $rest_client->get($params); 91 | } 92 | 93 | /** 94 | * Fungsi untuk mendapatkan ongkir internasional (EMS) 95 | * 96 | * @param integer ID kota asal 97 | * @param ineteger ID negara tujuan 98 | * @param integer Berat kiriman dalam gram 99 | * @param string Kode kurir 100 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 101 | */ 102 | function internationalCost($origin, $destination, $weight, $courier) { 103 | $params = array( 104 | 'origin' => $origin, 105 | 'destination' => $destination, 106 | 'weight' => $weight, 107 | 'courier' => $courier 108 | ); 109 | $rest_client = new RESTClient($this->api_key, 'internationalCost', $this->account_type); 110 | return $rest_client->post($params); 111 | } 112 | 113 | /** 114 | * Fungsi untuk mendapatkan nilai kurs rupiah terhadap USD 115 | * 116 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 117 | */ 118 | function currency() { 119 | $rest_client = new RESTClient($this->api_key, 'currency', $this->account_type); 120 | return $rest_client->get(array()); 121 | } 122 | 123 | /** 124 | * Fungsi untuk melacak paket/nomor resi 125 | * 126 | * @param string Nomor resi 127 | * @param string Kode kurir 128 | * @return string Response dari cURL, berupa string JSON balasan dari RajaOngkir 129 | */ 130 | function waybill($waybill_number, $courier) { 131 | $params = array( 132 | 'waybill' => $waybill_number, 133 | 'courier' => $courier 134 | ); 135 | $rest_client = new RESTClient($this->api_key, 'waybill', $this->account_type); 136 | return $rest_client->post($params); 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /libraries/RajaOngkir/restclient.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | class RESTClient { 9 | 10 | private $endpoint; 11 | private $account_type; 12 | private $api_key; 13 | private $api_url; 14 | 15 | public function __construct($api_key, $endpoint, $account_type) { 16 | $this->api_key = $api_key; 17 | $this->endpoint = $endpoint; 18 | $this->account_type = $account_type; 19 | $this->api_url = "http://rajaongkir.com/api/"; 20 | } 21 | 22 | /** 23 | * HTTP POST method 24 | * 25 | * @param array Parameter yang dikirimkan 26 | * @return string Response dari cURL 27 | */ 28 | function post($params) { 29 | $curl = curl_init(); 30 | $header[] = "Content-Type: application/x-www-form-urlencoded"; 31 | $header[] = "key: $this->api_key"; 32 | $query = http_build_query($params); 33 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 34 | curl_setopt($curl, CURLOPT_URL, $this->api_url . "" . $this->account_type . "/" . $this->endpoint); 35 | curl_setopt($curl, CURLOPT_POST, TRUE); 36 | curl_setopt($curl, CURLOPT_POSTFIELDS, $query); 37 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 38 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 39 | $request = curl_exec($curl); 40 | $return = ($request === FALSE) ? curl_error($curl) : $request; 41 | curl_close($curl); 42 | return $return; 43 | } 44 | 45 | /** 46 | * HTTP GET method 47 | * 48 | * @param array Parameter yang dikirimkan 49 | * @return string Response dari cURL 50 | */ 51 | function get($params) { 52 | $curl = curl_init(); 53 | $header[] = "key: $this->api_key"; 54 | $query = http_build_query($params); 55 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 56 | curl_setopt($curl, CURLOPT_URL, $this->api_url . "" . $this->account_type . "/" . $this->endpoint . "?" . $query); 57 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 58 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 59 | $request = curl_exec($curl); 60 | $return = ($request === FALSE) ? curl_error($curl) : $request; 61 | curl_close($curl); 62 | return $return; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /libraries/rajaongkir.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | defined('BASEPATH') OR exit('No direct script access allowed'); 10 | require_once 'RajaOngkir/endpoints.php'; 11 | 12 | class RajaOngkir extends Endpoints { 13 | 14 | private $api_key; 15 | private $account_type; 16 | 17 | public function __construct() { 18 | // Pastikan bahwa PHP mendukung cURL 19 | if (!function_exists('curl_init')) { 20 | log_message('error', 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.'); 21 | } 22 | $this->_ci = & get_instance(); 23 | $this->_ci->load->config('rajaongkir', TRUE); 24 | // Pastikan Anda sudah memasukkan API Key di application/config/rajaongkir.php 25 | if ($this->_ci->config->item('rajaongkir_api_key', 'rajaongkir') == "") { 26 | log_message("error", "Harap masukkan API KEY Anda di config."); 27 | } else { 28 | $this->api_key = $this->_ci->config->item('rajaongkir_api_key', 'rajaongkir'); 29 | $this->account_type = $this->_ci->config->item('rajaongkir_account_type', 'rajaongkir'); 30 | } 31 | parent::__construct($this->api_key, $this->account_type); 32 | } 33 | } 34 | --------------------------------------------------------------------------------