Created
June 26, 2020 08:59
-
-
Save blogcacanid/4c0958e2ab1d621f0d72bd2eb5073f43 to your computer and use it in GitHub Desktop.
Kota.php Cek Ongkos Kirim Dengan RajaOngkir CodeIgniter 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controllers; | |
use CodeIgniter\Controller; | |
class Kota extends Controller { | |
function get_kota($q) { | |
switch ($q) { | |
case 'kotaasal': | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "http://api.rajaongkir.com/starter/city", | |
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: --ganti-dengan-api-key-anda--" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
for ($i = 0; $i < count($data['rajaongkir']['results']); $i++) { | |
echo "<option></option>"; | |
echo "<option value='" . $data['rajaongkir']['results'][$i]['city_id'] . "'>" . $data['rajaongkir']['results'][$i]['city_name'] . "</option>"; | |
} | |
break; | |
case 'kotatujuan': | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "http://api.rajaongkir.com/starter/city", | |
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: --ganti-dengan-api-key-anda--" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
for ($i = 0; $i < count($data['rajaongkir']['results']); $i++) { | |
echo "<option></option>"; | |
echo "<option value='" . $data['rajaongkir']['results'][$i]['city_id'] . "'>" . $data['rajaongkir']['results'][$i]['city_name'] . "</option>"; | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment