Created
March 22, 2016 06:59
-
-
Save faytranevozter/2569f8cd508c7f363c54 to your computer and use it in GitHub Desktop.
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 | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Ongkir { | |
function __construct(){ | |
$this->ci =& get_instance(); | |
$this->api_key = get_profil('raja_ongkir_api_key'); | |
$this->origin = get_profil('raja_ongkir_origin'); | |
$this->province_url = "http://api.rajaongkir.com/starter/province"; | |
$this->city_url = "http://api.rajaongkir.com/starter/city"; | |
$this->costs_url = "http://api.rajaongkir.com/starter/cost"; | |
} | |
function get_province($id=FALSE) | |
{ | |
$url = $this->province_url; | |
if ($id) { | |
$url .= '?id='.$id; | |
} | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $url, | |
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: {$this->api_key}" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
// echo "cURL Error #:" . $err; | |
return FALSE; | |
} else { | |
// echo $response; | |
$response = json_decode($response, TRUE); | |
if ($response['rajaongkir']['status']['code'] == 200) { | |
return $response['rajaongkir']['results']; | |
} else { | |
return FALSE; | |
} | |
} | |
} | |
function get_city($id=FALSE, $province=FALSE) | |
{ | |
$url = $this->city_url; | |
if ($id) { | |
$url .= '?id='.$id; | |
} | |
if ($province) { | |
$url .= '?province='.$province; | |
} | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $url, | |
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: {$this->api_key}" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
// echo "cURL Error #:" . $err; | |
return FALSE; | |
} else { | |
// echo $response; | |
$response = json_decode($response, TRUE); | |
if ($response['rajaongkir']['status']['code'] == 200) { | |
return $response['rajaongkir']['results']; | |
} else { | |
return FALSE; | |
} | |
} | |
} | |
function costs($destination, $weight=1000, $courier='jne', $list_only=FALSE, $service=FALSE) | |
{ | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $this->costs_url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => "origin={$this->origin}&destination={$destination}&weight={$weight}&courier={$courier}", | |
CURLOPT_HTTPHEADER => array( | |
"content-type: application/x-www-form-urlencoded", | |
"key: {$this->api_key}" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
// echo "cURL Error #:" . $err; | |
return FALSE; | |
} else { | |
// echo $response; | |
$response = json_decode($response, TRUE); | |
if ($response['rajaongkir']['status']['code'] == 200) { | |
if ($list_only) { | |
$out = array(); | |
foreach ($response['rajaongkir']['results'][0]['costs'] as $kk => $vv) { | |
$out[$vv['service']] = $vv; | |
} | |
if ($service) { | |
return $out[$service]; | |
} else { | |
return $out; | |
} | |
} else { | |
return $response['rajaongkir']['results']; | |
} | |
} else { | |
return FALSE; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment