-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI_client.php
102 lines (76 loc) · 2.63 KB
/
API_client.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
<?php
/*********************************************************************
* Web API Client *
* *
* Version: 1.0 *
* Date: 02-10-2018 *
* Author: Dan Machado *
* Require php 5.6 or above *
**********************************************************************/
define('LOG_DIR', '/tmp/');
define('LOG_FILE', 'API_CLIENT_LOG_ERROR');
define('TIMEOUT', 30);
class API_Client{
private $status_code,
$curl_options,
$post_data,
$response_headers,
$body;
public function __construct($URL, $curl_opts=null){
$this->curl_options=array(
CURLOPT_URL => $URL,
CURLOPT_HEADER=>true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_HTTPHEADER=>array('Expect:'),
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_ENCODING=>"", // to accept all supported encoding types
CURLOPT_TIMEOUT=>TIMEOUT, // in seconds
CURLOPT_POST=>1
);
if(is_array($curl_opts)){
foreach($curl_opts as $opt=>$val){
$this->curl_options[$opt]=$val;
}
}
$this->status_code=200;
$this->response_headers=array();
}
public function Resquest($endpoint, $params, $const_param=null){
$result=array('ResponseCode'=>500,'Message'=>'', 'Content'=>'');
$this->post_data=array('endpoint'=>$endpoint, 'params'=>json_encode($params), 'constructor'=>'[]');
if(is_array($const_param)){
$this->post_data['constructor']=json_encode($const_param);
}
$ch = curl_init();
$this->curl_options[CURLOPT_POSTFIELDS]=$this->post_data;
curl_setopt_array($ch, $this->curl_options);
$response=curl_exec($ch);
if(curl_errno($ch)==0 ){
$this->status_code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response=explode("\r\n\r\n", $response);
$header=$response[0];
$tmp=explode("\r\n", $header);
for($i=1; $i<count($tmp); $i++){
$h=explode(':', $tmp[$i]);
$this->response_headers[$h[0]]=trim($h[1]);
}
$this->body=trim($response[1]);
if(!$result=@ json_decode($this->body, true)){
$result['ResponseCode']=$this->status_code;
$result['Meta']=$this->response_headers;
}
}else {
$result['Message']='Error Code: ' . curl_errno($ch) . ' Error: ' . curl_error($ch);
}
curl_close($ch);
return $result;
}
public function HTTP_StatusCode(){
return $this->status_code;
}
public function get_HTTP_headers(){
return $this->response_headers;
}
}
?>