-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcari.php
66 lines (47 loc) · 1.9 KB
/
cari.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require_once('util.php');
$loc = $_GET['lokasi'] ? $_GET['lokasi'] : ''; // parameter lokasi
$output = $_GET['output'] ? $_GET['output'] : ''; // parameter output
$type = $_GET['tipe'] ? $_GET['tipe'] : ''; // patameter tipe
$clientId = 'paste_client_id_here'; // API Access Four Square
$clientSecret = 'paste_client_secret_here'; // API Access Four Square
$today = date('Ymd'); // ambil tanggal hari ini
// url request api foursquare
$url = 'https://api.foursquare.com/v2/venues/search?client_id='.$clientId.'&client_secret='.$clientSecret.'&v='.$today;
$ch = NULL; // variabel kosong
if ($type == 'coordinates') {
// inisialisasi curl dan menambahkan param ll pada $url
$ch = curl_init($url.'&ll='.$loc);
} else {
// inisialisasi curl dan menambahkan param near pada $url
$ch = curl_init($url.'&near='.$loc);
}
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true // ambil data dari respon body
));
$respon = curl_exec($ch); // jalankan curl dan simpan data ke variabel respon
$data = json_decode($respon, true); // ubah json ke dalam bentuk array
$venues = $data['response']['venues']; // ambil data venues dari foursquare
$totalData = count($venues); // hitung total array yang ada
curl_close($ch);
switch ($output) {
case 'array':
$venues['rows'] = $totalData;
print_r($venues);
break;
case 'xml':
$xml_data = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><data></data>');
$xml_data->addChild('rows', $totalData);
array_to_xml($venues, $xml_data);
header("Content-type: text/xml; charset=utf-8");
echo $xml_data->asXML();
break;
default:
header('Content-type:application/json;charset=utf-8');
$json_output = array(
'rows' => $totalData,
'data' => array_values($venues)
);
echo json_encode($json_output);
break;
}