· 6 years ago · Dec 06, 2019, 04:48 PM
1<?php
2class Api {
3 public $api_url = 'https://localhost/api/'; // API Url
4 public $api_key = 'apikey'; // API KEY
5
6 public function profile() {
7 return json_decode($this->connect($this->api_url.'profile', array('api_key' => $this->api_key)));
8 }
9
10 public function services() {
11 return json_decode($this->connect($this->api_url.'services', array('api_key' => $this->api_key)));
12 }
13
14 public function order($data) {
15 return json_decode($this->connect($this->api_url.'order', array_merge(array('api_key' => $this->api_key), $data)));
16 }
17
18 public function status($order_id) {
19 return json_decode($this->connect($this->api_url.'status', array('api_key' => $this->api_key, 'id' => $order_id)));
20 }
21
22 private function connect($end_point, $post) {
23 $_post = Array();
24 if (is_array($post)) {
25 foreach ($post as $name => $value) {
26 $_post[] = $name.'='.urlencode($value);
27 }
28 }
29 $ch = curl_init($end_point);
30 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
31 curl_setopt($ch, CURLOPT_POST, 1);
32 curl_setopt($ch, CURLOPT_HEADER, 0);
33 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
34 if (is_array($post)) {
35 curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
36 }
37 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
38 $result = curl_exec($ch);
39 if (curl_errno($ch) != 0 && empty($result)) {
40 $result = false;
41 }
42 curl_close($ch);
43 return $result;
44 }
45}
46
47// contoh penggunaan
48
49$api = new Api();
50
51// cek profil
52$profile = $api->profile();
53print_r($profile);
54print('<br /><br />');
55
56// daftar layanan
57$services = $api->services();
58print_r($services);
59print('<br /><br />');
60
61// membuat pesanan
62$order = $api->order(array(
63 'service' => 1, // id layanan
64 'data' => 'mahirdpy_', // target pesanan
65 'quantity' => 100 // jumlah pesan
66));
67print_r($order);
68print('<br /><br />');
69
70// cek status pesanan
71$status = $api->status('123'); // 123 = id pesanan
72print_r($status);