github github
  • Home
  • Pricing and Signup
  • Training
  • Gist
  • Blog
  • Login

diogok / restserver

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 6
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: b1e641c

click here to add a description

click here to add a homepage

  • Switch Branches (1)
    • master
  • Switch Tags (0)
  • Branch List
Sending Request…

RESTful resource/url mapping and MVC, featuring Request an Response objects. — Read more

  Cancel

http://www.phpclasses.org/browse/package/5080.html

  Cancel
  • HTTP
  • Git Read-Only

This URL has Read+Write access

Fixed redundant methods RestRequest::getInput and getBody and RestResponse::addResponse appendResponse, now they chain call the newer method. Did not removed duplicated due to backward compatibility and stuff like that. This is my longest commit message so far... 
diogok (author)
Thu Aug 12 17:07:00 -0700 2010
commit  b1e641c05d5d507d5810
tree    4326a6d862cc927adfc9
parent  330617e443421d5647aa
restserver / RestClient.class.php RestClient.class.php
Txt 100755 327 lines (300 sloc) 10.141 kb
  • edit
  • raw
  • blame
  • history
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php

/**
* Class RestClient
* Wraps HTTP calls using cURL, aimed for accessing and testing RESTful webservice.
* By Diogo Souza da Silva <manifesto@manifesto.blog.br>
*/
class RestClient {

     private $curl ;
     private $url ;
     private $response ="";
     private $headers = array();

     private $method="GET";
     private $params=null;
     private $contentType = null;
     private $file =null;

     /**
* Private Constructor, sets default options
*/
     private function __construct() {
         $this->curl = curl_init();
         curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($this->curl,CURLOPT_AUTOREFERER,true); // This make sure will follow redirects
         curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION,true); // This too
         curl_setopt($this->curl,CURLOPT_HEADER,true); // This verbose option for extracting the headers
     }

     /**
* Execute the call to the webservice
* @return RestClient
*/
     public function execute() {
         if($this->contentType != null) {
             curl_setopt($this->curl,CURLOPT_HTTPHEADER,array("Content-Type: ".$this->contentType));
         }
         if($this->method === "POST") {
             curl_setopt($this->curl,CURLOPT_POST,true);
             if(is_array($this->params)) {
                 $params = "";
                 foreach($this->params as $k=>$v) {
                     $params .= "$k=$v&";
                 }
             } else {
                 $params = $this->params ;
             }
             curl_setopt($this->curl,CURLOPT_POSTFIELDS,$params);
         } else if($this->method == "GET"){
             curl_setopt($this->curl,CURLOPT_HTTPGET,true);
             $this->treatURL();
         } else if($this->method === "PUT") {
             curl_setopt($this->curl,CURLOPT_PUT,true);
             $this->treatURL();
             $this->file = tmpFile();
             fwrite($this->file,$this->params);
             fseek($this->file,0);
             curl_setopt($this->curl,CURLOPT_INFILE,$this->file);
             curl_setopt($this->curl,CURLOPT_INFILESIZE,strlen($this->params));
         } else {
             curl_setopt($this->curl,CURLOPT_CUSTOMREQUEST,$this->method);
         }
         curl_setopt($this->curl,CURLOPT_URL,$this->url);
         $r = curl_exec($this->curl);
         $this->treatResponse($r); // Extract the headers and response
         return $this ;
     }

     /**
* Treats URL
*/
     private function treatURL(){
         if(is_array($this->params) && count($this->params) >= 1) { // Transform parameters in key/value pars in URL
             if(!strpos($this->url,'?')) {
                 $this->url .= '?' ;
             }
             $i = 0;
             foreach($this->params as $k=>$v) {
                 if($i>0) $this->url .= "&";
                 $this->url .= "&";
                 $this->url .= urlencode($k)."=".urlencode($v);
                 $i++;
             }
         }
        return $this->url;
     }

     /*
* Treats the Response for extracting the Headers and Response
*/
     private function treatResponse($r) {
        if($r == null or strlen($r) < 1) {
            return;
        }
        $parts = explode("\n\r",$r); // HTTP packets define that Headers end in a blank line (\n\r) where starts the body
        while(preg_match('@HTTP/1.[0-1] 100 Continue@',$parts[0]) or preg_match("@Moved@",$parts[0])) {
            // Continue header must be bypass
            for($i=1;$i<count($parts);$i++) {
                $parts[$i - 1] = trim($parts[$i]);
            }
            unset($parts[count($parts) - 1]);
        }
        preg_match("@Content-Type: ([a-zA-Z0-9-]+/?[a-zA-Z0-9-]*)@",$parts[0],$reg);// This extract the content type
        $this->headers['content-type'] = $reg[1];
        preg_match("@HTTP/1.[0-1] ([0-9]{3}) ([a-zA-Z ]+)@",$parts[0],$reg); // This extracts the response header Code and Message
        $this->headers['code'] = $reg[1];
        $this->headers['message'] = $reg[2];
        $this->response = "";
        for($i=1;$i<count($parts);$i++) {//This make sure that exploded response get back togheter
            if(($start = strpos($parts[$i],"\n")) !== false) {
                $parts[$i] = substr($parts[$i],1);
            }
            if($i > 1) {
                $this->response .= "\n\r";
            }
            $this->response .= $parts[$i];
        }
     }

     /*
* Array of headers
* @return array
*/
     public function getHeaders() {
        return $this->headers;
     }

     /*
* Response body
* @return string
*/
     public function getResponse() {
         return $this->response ;
     }

     /*
* HTTP response code (404,401,200,etc)
* @return int
*/
     public function getResponseCode() {
         return (int) $this->headers['code'];
     }
     
     /*
* HTTP response message (Not Found, Continue, etc )
* @return string
*/
     public function getResponseMessage() {
         return $this->headers['message'];
     }

     /*
* Content-Type (text/plain, application/xml, etc)
* @return string
*/
     public function getResponseContentType() {
         return $this->headers['content-type'];
     }

     /**
* This sets that will not follow redirects
* @return RestClient
*/
     public function setNoFollow() {
         curl_setopt($this->curl,CURLOPT_AUTOREFERER,false);
         curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION,false);
         return $this;
     }

     /**
* This closes the connection and release resources
* @return RestClient
*/
     public function close() {
         curl_close($this->curl);
         $this->curl = null ;
         if($this->file !=null) {
             fclose($this->file);
         }
         return $this ;
     }

     /**
* Sets the URL to be Called
* @return RestClient
*/
     public function setUrl($url) {
         $this->url = $url;
         return $this;
     }

     /**
* Set the Content-Type of the request to be send
* Format like "application/xml" or "text/plain" or other
* @param string $contentType
* @return RestClient
*/
     public function setContentType($contentType) {
         $this->contentType = $contentType;
         return $this;
     }

     /**
* Set the Credentials for BASIC Authentication
* @param string $user
* @param string $pass
* @return RestClient
*/
     public function setCredentials($user,$pass) {
         if($user != null) {
             curl_setopt($this->curl,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
             curl_setopt($this->curl,CURLOPT_USERPWD,"{$user}:{$pass}");
         }
         return $this;
     }

     /**
* Set the Request HTTP Method
* For now, only accepts GET and POST
* @param string $method
* @return RestClient
*/
     public function setMethod($method) {
         $this->method=$method;
         return $this;
     }

     /**
* Set Parameters to be send on the request
* It can be both a key/value par array (as in array("key"=>"value"))
* or a string containing the body of the request, like a XML, JSON or other
* Proper content-type should be set for the body if not a array
* @param mixed $params
* @return RestClient
*/
     public function setParameters($params) {
         $this->params=$params;
         return $this;
     }

     /**
* Creates the RESTClient
* @param string $url
* @return RestClient
*/
     public static function createClient($url=null) {
         $client = new RestClient ;
         if($url != null) {
             $client->setUrl($url);
         }
         return $client;
     }

     /**
* Convenience method wrapping a commom POST call
* @param string $url
* @param mixed $params
* @param string $user
* @param string $password
* @param string $contentType
* @return RestClient
*/
     public static function post($url,$params=null,$user=null,$password=null,$contentType="application/x-www-form-urlencoded") {
         return self::call("POST",$url,$params,$user,$password,$contentType);
     }

     /**
* Convenience method wrapping a commom PUT call
* @param string $url
* @param string $body
* @param string $user
* @param string $password
* @param string $contentType
* @return RestClient
*/
     public static function put($url,$body,$user=null,$password=null,$contentType=null) {
         return self::call("PUT",$url,$body,$user,$password,$contentType);
     }

     /**
* Convenience method wrapping a commom GET call
* @param string $url
* @param array params
* @param string $user
* @param string $password
* @return RestClient
*/
     public static function get($url,array $params=null,$user=null,$password=null) {
         return self::call("GET",$url,$params,$user,$password);
     }

     /**
* Convenience method wrapping a commom delete call
* @param string $url
* @param array params
* @param string $user
* @param string $password
* @return RestClient
*/
     public static function delete($url,array $params=null,$user=null,$password=null) {
         return self::call("DELETE",$url,$params,$user,$password);
     }

     /**
* Convenience method wrapping a commom custom call
* @param string $method
* @param string $url
* @param string $body
* @param string $user
* @param string $password
* @param string $contentType
* @return RestClient
*/
     public static function call($method,$url,$body,$user=null,$password=null,$contentType=null) {
         return self::createClient($url)
             ->setParameters($body)
             ->setMethod($method)
             ->setCredentials($user,$password)
             ->setContentType($contentType)
             ->execute()
             ->close();
     }
}

?>

Dedicated Server Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
  • Blog
  • Support
  • Training
  • Job Board
  • Shop
  • Contact
  • API
  • Status
  • © 2010 GitHub Inc. All rights reserved.
  • Terms of Service
  • Privacy
  • Security
  • English
  • Deutsch
  • Français
  • 日本語
  • Português (BR)
  • 中文
  • See all available languages

Your current locale selection: English. Choose another?

  • English
  • Afrikaans
  • Català
  • Čeština
  • Deutsch
  • Español
  • Français
  • Hrvatski
  • Indonesia
  • Italiano
  • 日本語
  • Nederlands
  • Norsk
  • Polski
  • Português (BR)
  • Српски
  • Svenska
  • 中文