1
1
<?php
2
- /**
2
+ /**
3
3
* HTTP Client library
4
4
*
5
5
* PHP version 5.2
@@ -28,10 +28,10 @@ class Response
28
28
function __construct ($ status_code = null , $ response_body = null , $ response_headers = null )
29
29
{
30
30
$ this ->_status_code = $ status_code ;
31
- $ this ->_response_body = $ response_body ;
32
- $ this ->_response_headers = $ response_headers ;
31
+ $ this ->_body = $ response_body ;
32
+ $ this ->_headers = $ response_headers ;
33
33
}
34
-
34
+
35
35
/**
36
36
* The status code
37
37
*
@@ -47,19 +47,19 @@ public function statusCode()
47
47
*
48
48
* @return array
49
49
*/
50
- public function responseBody ()
50
+ public function body ()
51
51
{
52
- return $ this ->_response_body ;
52
+ return $ this ->_body ;
53
53
}
54
54
55
55
/**
56
56
* The response headers
57
57
*
58
58
* @return array
59
59
*/
60
- public function responseHeaders ()
60
+ public function headers ()
61
61
{
62
- return $ this ->_response_headers ;
62
+ return $ this ->_headers ;
63
63
}
64
64
}
65
65
@@ -68,22 +68,22 @@ public function responseHeaders()
68
68
*/
69
69
class Client
70
70
{
71
-
72
- public
71
+
72
+ public
73
73
$ host ,
74
74
$ request_headers ,
75
75
$ version ,
76
76
$ url_path ,
77
- $ methods ;
78
-
77
+ $ methods ;
78
+
79
79
/**
80
80
* Initialize the client
81
81
*
82
82
* @param string $host the base url (e.g. https://api.sendgrid.com)
83
83
* @param array $request_headers global request headers
84
84
* @param string $version api version (configurable)
85
85
* @param array $url_path holds the segments of the url path
86
- */
86
+ */
87
87
function __construct ($ host , $ request_headers = null , $ version = null , $ url_path = null )
88
88
{
89
89
$ this ->host = $ host ;
@@ -110,7 +110,7 @@ private function _buildClient($name = null)
110
110
$ this ->url_path = [];
111
111
return new Client ($ this ->host , $ this ->request_headers , $ this ->version , $ url_path );
112
112
}
113
-
113
+
114
114
/**
115
115
* Subclass this function for your own needs.
116
116
* Or just pass the version as part of the URL
@@ -120,7 +120,7 @@ private function _buildClient($name = null)
120
120
*
121
121
* @return string
122
122
*/
123
- private function _buildVersionedUrl ($ url )
123
+ private function _buildVersionedUrl ($ url )
124
124
{
125
125
return sprintf ("%s%s%s " , $ this ->host , $ this ->version , $ url );
126
126
}
@@ -129,10 +129,10 @@ private function _buildVersionedUrl($url)
129
129
* Build the final URL to be passed
130
130
*
131
131
* @param array $query_params an array of all the query parameters
132
- *
132
+ *
133
133
* @return string
134
134
*/
135
- private function _buildUrl ($ query_params = null )
135
+ private function _buildUrl ($ query_params = null )
136
136
{
137
137
$ url = '/ ' .implode ('/ ' , $ this ->url_path );
138
138
if (isset ($ query_params )) {
@@ -155,10 +155,10 @@ private function _buildUrl($query_params = null)
155
155
* @param string $url the final url to call
156
156
* @param array $request_body request body
157
157
* @param array $request_headers any additional request headers
158
- *
158
+ *
159
159
* @return Response object
160
160
*/
161
- public function makeRequest ($ method , $ url , $ request_body = null , $ request_headers = null )
161
+ public function makeRequest ($ method , $ url , $ request_body = null , $ request_headers = null )
162
162
{
163
163
$ curl = curl_init ($ url );
164
164
curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
@@ -179,9 +179,9 @@ public function makeRequest($method, $url, $request_body = null, $request_header
179
179
$ status_code = curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
180
180
$ response_body = substr ($ curl_response , $ header_size );
181
181
$ response_header = substr ($ curl_response , 0 , $ header_size );
182
-
182
+
183
183
curl_close ($ curl );
184
-
184
+
185
185
return new Response ($ status_code , $ response_body , $ response_header );
186
186
}
187
187
@@ -192,10 +192,10 @@ public function makeRequest($method, $url, $request_body = null, $request_header
192
192
* in your url, you must use this method.
193
193
*
194
194
* @param string $name name of the url segment
195
- *
195
+ *
196
196
* @return Client object
197
197
*/
198
- public function _ ($ name = null )
198
+ public function _ ($ name = null )
199
199
{
200
200
return $ this ->_buildClient ($ name );
201
201
}
@@ -206,24 +206,24 @@ public function _($name = null)
206
206
*
207
207
* @param string $name name of the dynamic method call or HTTP verb
208
208
* @param array $args parameters passed with the method call
209
- *
209
+ *
210
210
* @return Client or Response object
211
211
*/
212
212
public function __call ($ name , $ args )
213
- {
213
+ {
214
214
if ($ name == 'version ' ) {
215
215
$ this ->version = $ args [0 ];
216
216
return $ this ->_ ();
217
217
}
218
-
218
+
219
219
if (in_array ($ name , $ this ->methods )) {
220
220
$ query_params = ((count ($ args ) >= 2 ) ? $ args [1 ] : null );
221
221
$ url = $ this ->_buildUrl ($ query_params );
222
222
$ request_body = ($ args ? $ args [0 ] : null );
223
223
$ request_headers = ((count ($ args ) == 3 ) ? $ args [2 ] : null );
224
224
return $ this ->makeRequest ($ name , $ url , $ request_body , $ request_headers );
225
225
}
226
-
226
+
227
227
return $ this ->_ ($ name );
228
228
}
229
229
}
0 commit comments