forked from jesusperiago/fonoapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonoapi-v1.php
113 lines (91 loc) · 2.04 KB
/
fonoapi-v1.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
<?php
/**
* Class fonoApi v1
* Author @shakee93
* Version 1.0.1
*/
class fonoApi
{
static $_ApiKey = null;
static $debug = false;
static $_ApiUrl = "https://fonoapi.freshpixl.com/v1/";
function __construct($ApiKey, $url = null)
{
if($url != null) {
self::$_ApiUrl = $url;
}
self::$_ApiKey = $ApiKey;
}
/**
* Instaniate Self
*/
public static function init($ApiKey) {
$f = new self($ApiKey);
return $f;
}
/**
* Turn Debug On
*/
public static function debug($ApiKey) {
$f = new self($ApiKey);
$f::$debug = true;
return $f;
}
/**
*
* Gets Device Data Object from fonoapi.freshpixl.com
*
* @param $device
* @param null $position
* @param null $brand
*
* @return mixed
* @throws \Exception
*/
public static function getDevice($device, $position = null, $brand = null){
$url = self::$_ApiUrl . "getdevice";
$postData = array(
'brand' => trim($brand),
'device' => trim($device),
'position' => $position,
'token' => self::$_ApiKey
);
$result = json_decode(self::sendPostData($url, $postData));
if (isset($result->status)) {
$innerException ="";
if(self::$debug){
$innerException = " | <strong>innerException</strong> : " . $result->innerException;
}
throw new Exception($result->message . $innerException);
}else {
return $result;
}
}
/**
* Sends Post Data to the Server
*
* @param $url
* @param $post
*
* @return mixed
*/
static function sendPostData($url, $post){
try {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
if (FALSE === $result)
throw new Exception(curl_error($ch), curl_errno($ch));
curl_close($ch);
return $result;
} catch (Exception $e) {
$result["status"] = $e->getCode();
$result["message"] = "Curl Failed" ;
$result["innerException"] = $e->getMessage();
return json_encode($result);
}
}
}