-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
129 lines (122 loc) · 3.09 KB
/
api.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* *** TRADEXAT APIs ***
* This class is a way to provide you a few tools that will make your life easier;
* It is simple, but we'll improving it as well...
* @copyright MIT License. Copyright (c) 2018 Paulo Rodriguez
* @author Paulo Rodriguez(xLaming)
* @link https://github.com/xLaming/xat-fairtrade
* @version 1.0 (stable)
*/
class API
{
/**
* This function is used to load pages using cURL method.
* @param string $url
* @return string
*/
private function loadPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* This function is used to search powers in fairtrade's database;
* It works using power name or ID;
* You can set type xml or json
* @param mixed $power
* @param string $type
* @return mixed
*/
public function searchPower($power, $type = 'json')
{
if (empty($power))
{
return 'Power name cannot be empty!';
}
$page = $this->loadPage("http://xatproject.com/fairtrade/api.php?action=search_power&power={$power}&type={$type}");
$json = json_decode($page, true);
if ($json['status'] == 'fail')
{
if ($json['message'] == 'power_not_found')
{
return 'This power does not exists!';
}
else
{
return 'Unknown error!';
}
}
return $json['power'];
}
/**
* This function is used to list all powers by categories;
* Categories available are: unlimited, limited, group, hug, game, epic, allp
* You can set type xml or json
* @param string $cat
* @param string $type
* @return mixed
*/
public function listPowersByCategory($cat, $type = 'json')
{
if (empty($cat))
{
return 'Category cannot be empty!';
}
$page = $this->loadPage("http://xatproject.com/fairtrade/api.php?action=list_powers&category={$cat}&type={$type}");
$json = json_decode($page, true);
if ($json['status'] == 'fail')
{
if ($json['message'] == 'invalid_category')
{
return 'This category does not exists!';
}
else
{
return 'Unknown error!';
}
}
return $json['powers'];
}
/**
* This function will list all powers;
* You can set type xml or json
* @param string $type
* @return array
*/
public function listPowers($type = 'json')
{
$page = $this->loadPage("http://xatproject.com/fairtrade/api.php?action=powers&type={$type}");
$json = json_decode($page, true);
return $json['powers'];
}
}
$API = new API();
/**
* Some examples will be listed above, so you can see how it works:
*
* => Looking for a power via name:
* $API->searchPower('gold');
*
* => Looking for a power via id:
* $API->searchPower(153);
*
* => Listing all powers by categories:
* $API->listPowersByCategory('limited');
* $API->listPowersByCategory('unlimited');
* $API->listPowersByCategory('epic');
* $API->listPowersByCategory('game');
* $API->listPowersByCategory('group');
*
* => Listing all powers:
* $API->listPowers();
*
*/
?>