· 6 years ago · Jan 10, 2020, 01:38 PM
1<?php
2defined('BASEPATH') OR exit('No direct script access allowed');
3class Produk extends CI_Controller {
4 function __construct()
5 {
6 parent::__construct();
7 }
8 function _api_ongkir_post($origin,$des,$qty,$cour)
9 {
10 $curl = curl_init();
11 curl_setopt_array($curl, array(
12 CURLOPT_URL => "https://api.rajaongkir.com/starter/cost",
13 CURLOPT_RETURNTRANSFER => true,
14 CURLOPT_ENCODING => "",
15 CURLOPT_MAXREDIRS => 10,
16 CURLOPT_TIMEOUT => 30,
17 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
18 CURLOPT_CUSTOMREQUEST => "POST",
19 CURLOPT_POSTFIELDS => "origin=".$origin."&destination=".$des."&weight=".$qty."&courier=".$cour,
20 CURLOPT_HTTPHEADER => array(
21 "content-type: application/x-www-form-urlencoded",
22 /* masukan api key disini*/
23 "key: ganti api key"
24 ),
25 ));
26 $response = curl_exec($curl);
27 $err = curl_error($curl);
28 curl_close($curl);
29 if ($err) {
30 return $err;
31 } else {
32 return $response;
33 }
34 }
35 function _api_ongkir($data)
36 {
37 $curl = curl_init();
38 curl_setopt_array($curl, array(
39 //CURLOPT_URL => "https://api.rajaongkir.com/starter/province?id=12",
40 //CURLOPT_URL => "http://api.rajaongkir.com/starter/province",
41 CURLOPT_URL => "http://api.rajaongkir.com/starter/".$data,
42 CURLOPT_RETURNTRANSFER => true,
43 CURLOPT_ENCODING => "",
44 CURLOPT_MAXREDIRS => 10,
45 CURLOPT_TIMEOUT => 30,
46 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
47 CURLOPT_CUSTOMREQUEST => "GET",
48 CURLOPT_HTTPHEADER => array(
49 /* masukan api key disini*/
50 "key: ganti api key"
51 ),
52 ));
53 $response = curl_exec($curl);
54 $err = curl_error($curl);
55 curl_close($curl);
56 if ($err) {
57 return $err;
58 } else {
59 return $response;
60 }
61 }
62 public function provinsi()
63 {
64 $provinsi = $this->_api_ongkir('province');
65 $data = json_decode($provinsi, true);
66 echo json_encode($data['rajaongkir']['results']);
67 }
68 public function lokasi()
69 {
70 $this->load->view('head');
71 $this->load->view('nav');
72 $this->load->view('halaman');
73 $this->load->view('footer');
74
75 }
76 public function kota($provinsi="")
77 {
78 if(!empty($provinsi))
79 {
80 if(is_numeric($provinsi))
81 {
82 $kota = $this->_api_ongkir('city?province='.$provinsi);
83 $data = json_decode($kota, true);
84 echo json_encode($data['rajaongkir']['results']);
85 }
86 else
87 {
88 show_404();
89 }
90 }
91 else
92 {
93 show_404();
94 }
95 }
96 public function tarif($origin,$des,$qty,$cour)
97 {
98 $berat = $qty*1000;
99 $tarif = $this->_api_ongkir_post($origin,$des,$berat,$cour);
100 $data = json_decode($tarif, true);
101 echo json_encode($data['rajaongkir']['results']);
102 }
103}