1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Implimentation of WordPress inbuilt API class |
4
|
|
|
* |
5
|
|
|
* Usage: |
6
|
|
|
* |
7
|
|
|
* $api = new PLUGIN_API(); |
8
|
|
|
* $api->endpoint = 'endpoint_url' |
9
|
|
|
* $api->header = array( "key: $val" ) |
10
|
|
|
* $api->data_type = 'xml' or 'json' |
11
|
|
|
* $api->call_type = 'GET' or 'POST' |
12
|
|
|
* $api->call(); |
13
|
|
|
* $data = $api->parse(); |
14
|
|
|
* |
15
|
|
|
* @author Nirjhar Lo |
16
|
|
|
* @version 1.2.1 |
17
|
|
|
* @package wp-plugin-framework |
18
|
|
|
*/ |
19
|
|
|
if ( ! class_exists( 'PLUGIN_API' ) ) { |
20
|
|
|
|
21
|
|
|
class PLUGIN_API { |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var String |
26
|
|
|
*/ |
27
|
|
|
public $endpoint; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Array |
32
|
|
|
*/ |
33
|
|
|
public $header; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var String |
38
|
|
|
*/ |
39
|
|
|
public $data_type; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var String |
44
|
|
|
*/ |
45
|
|
|
public $call_type; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Define the properties inside a instance |
50
|
|
|
* |
51
|
|
|
* @return Void |
52
|
|
|
*/ |
53
|
|
|
public function __construct() { |
54
|
|
|
|
55
|
|
|
$this->endpoint = ''; |
56
|
|
|
$this->header = array(); |
57
|
|
|
$this->data_type = ''; //xml or json |
58
|
|
|
$this->call_type = ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Define the necessary database tables |
64
|
|
|
* |
65
|
|
|
* @return Array |
66
|
|
|
*/ |
67
|
|
|
public function build() { |
68
|
|
|
|
69
|
|
|
$args = array( |
70
|
|
|
CURLOPT_URL => $this->endpoint, |
71
|
|
|
CURLOPT_RETURNTRANSFER => true, |
72
|
|
|
CURLOPT_ENCODING => "", |
73
|
|
|
CURLOPT_MAXREDIRS => 10, |
74
|
|
|
CURLOPT_TIMEOUT => 30, |
75
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
76
|
|
|
CURLOPT_CUSTOMREQUEST => $this->call_type, |
77
|
|
|
CURLOPT_HTTPHEADER => $this->header, |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $args; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Define the variables for db table creation |
86
|
|
|
* |
87
|
|
|
* @return Array |
88
|
|
|
*/ |
89
|
|
|
public function call() { |
90
|
|
|
|
91
|
|
|
$curl = curl_init(); |
92
|
|
|
|
93
|
|
|
curl_setopt_array( $curl, $this->build() ); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$result = curl_exec( $curl ); |
|
|
|
|
96
|
|
|
$err = curl_error( $curl ); |
|
|
|
|
97
|
|
|
|
98
|
|
|
curl_close( $curl ); |
|
|
|
|
99
|
|
|
|
100
|
|
|
if ( $err ) { |
101
|
|
|
$result = "cURL Error #:" . $err; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check options and tables and output the info to check if db install is successful |
110
|
|
|
* |
111
|
|
|
* @return Array |
112
|
|
|
*/ |
113
|
|
|
public function parse($data) { |
114
|
|
|
|
115
|
|
|
call_user_func( array( $this, $this->data_type ), $data ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parse XML data type |
121
|
|
|
* |
122
|
|
|
* @return Array |
123
|
|
|
*/ |
124
|
|
|
public function xml( $data ) { |
125
|
|
|
|
126
|
|
|
libxml_use_internal_errors( true ); |
127
|
|
|
$parsed = ( ! $data || $data == '' ? false : simplexml_load_string( $data ) ); |
128
|
|
|
|
129
|
|
|
if ( ! $parsed ) { |
130
|
|
|
return false; |
|
|
|
|
131
|
|
|
libxml_clear_errors(); |
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
return $parsed; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Parse JSON data type |
140
|
|
|
* |
141
|
|
|
* @return Array |
142
|
|
|
*/ |
143
|
|
|
public function json( $data ) { |
144
|
|
|
|
145
|
|
|
$parsed = ( ! $data || $data == '' ? false : json_decode( $data, 1 ) ); |
146
|
|
|
return $parsed; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} ?> |
|
|
|
|
150
|
|
|
|