Last active
June 27, 2020 06:11
-
-
Save blogcacanid/ee6cdeb17824e16b25f143455356d66d to your computer and use it in GitHub Desktop.
KotaController.php Cek Ongkos Kirim Dengan RajaOngkir Laravel 7
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\Http\Controllers; | |
use Illuminate\Http\Request; | |
class KotaController extends Controller | |
{ | |
/** | |
* Display the specified resource. | |
* | |
* @param string $q | |
* @return \Illuminate\Http\Response | |
*/ | |
public 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