-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_interface.php
More file actions
126 lines (103 loc) · 3.44 KB
/
api_interface.php
File metadata and controls
126 lines (103 loc) · 3.44 KB
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
<?
// API call class
class api_call
{
// Private Vars
private $api = array();
// Constructor
public function __construct()
{
$this->api['URL'] = 'api.midealervirtual.com/s/';
$this->api['FORMAT'] = 'format/json';
$this->api['AUTH'] = (object) array( 'uname' => 'dev', 'pword' => 'code123' );
}
# Public methods
// Get dealer data
// Return: dealer (std object)
public function getDealer( $cid /* client id */ )
{
// This call's API segment
$api_segment = "dealer/";
// API url
$url_to_use = $this->api['URL'].$api_segment.$this->api['FORMAT']."?cid=".$cid;
return $this->authAPICall( $url_to_use );
}
// Get dealer vehicle count
// Return: total vehicle count (int)
public function getDealerCount( $cid /* client id */ )
{
// This call's API segment
$api_segment = "dealer_vehicle_count/";
// API url
$url_to_use = $this->api['URL'].$api_segment.$this->api['FORMAT']."?cid=".$cid;
return $this->authAPICall( $url_to_use );
}
// Get dealer vehicles
// Return: dealer vehicles (std object)
public function getDealerVehicles( $cid /* client id */,
$offset = false /* offest of results */,
$limit = false /* limit results */,
$order_by = false /* order results by */,
$order_dir = false /* ordering direction */ )
{
// This call's API segment
$api_segment = "dealer_vehicles/";
// Prepare appended varibales
$extra_vars = ( ( $order_by ) ? '&order_by='.$order_by : '' ).
( ( $order_dir ) ? '&order_dir='.$order_dir : '' ).
( ( $offset ) ? '&offset='.$offset : '' ).
( ( $limit ) ? '&limit='.$limit : '' );
// API url
$url_to_use = $this->api['URL'].$api_segment.$this->api['FORMAT']."?cid=".$cid.$extra_vars;
return $this->authAPICall( $url_to_use );
}
// Get dealer's vehicle's information
// Return: vehicle (std object)
public function getVehicle( $cid /* client id */, $vid /* vehicle id */ )
{
// This call's API segment
$api_segment = "vehicle/";
// API url
$url_to_use = $this->api['URL'].$api_segment.$this->api['FORMAT']."?cid=".$cid."&vid=".$vid;
return $this->authAPICall( $url_to_use );
}
// Get dealer's vehicle's images
// Return: vehicle images (std object)
public function getVehicleImages( $vid /* vehicle id */ )
{
// This call's API segment
$api_segment = "vehicle_images/";
// API url
$url_to_use = $this->api['URL'].$api_segment.$this->api['FORMAT']."?vid=".$vid;
return $this->authAPICall( $url_to_use );
}
// Make call and process result
// Return: result(s) (std object)
public function makeCall( $rest_call /* REST api url to execute */ )
{
// Fetch data
$json = file_get_contents( $rest_call );
// Return decoded json object
return json_decode( $json ); // Convert json string into php object
}
# Private methods
// Authorize API call
private function authAPICall( $call_url )
{
// Finish `api_url`
return 'http://' . $this->api['AUTH']->uname . ':' . $this->api['AUTH']->pword . '@' . $call_url;
}
}
/* # Example - paginate results:
// create api interface
$api = new api_call();
// fetch paginated results
$vehicles = $api->makeCall( $api->getDealerVehicles( 5 /* client id * /, 5 /* offset * /, 10 /* limit * / ) );
// display results
$i = 1;
foreach( $vehicles as $v )
{
echo $i++ ."- ". $v->MAKE." ".$v->MODEL."<br />";
}
# End example */
?>