9 The HttpRequest class can be used to execute any HTTP request method.
10 The following example shows a simple GET request where a few query
11 parameters are supplied. Additionally potential cookies will be
12 read from and written to a file.
15 $r = new HttpRequest('http://www.google.com/search');
17 // store Googles cookies in a dedicated file
20 array( 'cookiestore' => 'google.txt',
25 array( 'q' => '+"pecl_http" -msg -cvs -list',
30 // HttpRequest::send() returns an HttpMessage object
31 // of type HttpMessage::TYPE_RESPONSE or throws an exception
33 print $r->send()->getBody();
34 } catch (HttpException $e) {
41 The following example shows an multipart POST request, with two form
42 fields and an image that's supposed to be uploaded to the server.
43 It's a bad habit as well as common practice to issue a redirect after
44 an received POST request, so we'll allow a redirect by enabling the
48 $r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
50 // if redirects is set to true, a single redirect is allowed;
51 // one can set any reasonable count of allowed redirects
53 array( 'cookies' => array('MyCookie' => 'has a value'),
60 array( 'name' => 'Mike',
61 'mail' => 'mike@php.net',
64 // add the file to post (form name, file name, file type)
66 $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
69 print $r->send()->getBody();
70 } catch (HttpException $e) {
77 It's possible to execute several HttpRequests in parallel with the
78 HttpRequestPool class. HttpRequests to send, do not need to perform
79 the same request method, but can only be attached to one HttpRequestPool
84 $p = new HttpRequestPool;
85 // if you want to set _any_ options of the HttpRequest object,
86 // you need to do so *prior attaching* to the request pool!
87 $p->attach(new HttpRequest('http://pear.php.net', HTTP_METH_HEAD));
88 $p->attach(new HttpRequest('http://pecl.php.net', HTTP_METH_HEAD));
89 } catch (HttpException $e) {
96 // HttpRequestPool implements an iterator over attached HttpRequest objects
98 echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
100 } catch (HttpException $e) {
107 You can use a more advanced approach by using the protected interface of
108 the HttpRequestPool class. This allows you to perform some other tasks
109 while the requests are executed.
112 class Pool extends HttpRequestPool
114 public function __construct()
117 new HttpRequest('http://pear.php.net', HTTP_METH_HEAD),
118 new HttpRequest('http://pecl.php.net', HTTP_METH_HEAD)
121 // HttpRequestPool methods socketPerform() and socketSelect() are
122 // protected; one could use this approach to do something else
123 // while the requests are being executed
124 print "Executing requests";
125 for ($i = 0; $this->socketPerform(); $i++) {
126 $i % 10 or print ".";
127 if (!$this->socketSelect()) {
128 throw new HttpException("Socket error!");
136 foreach (new Pool as $r) {
137 echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
139 } catch (HttpException $ex) {
146 One of the main key features of HttpResponse is HTTP caching. HttpResponse
147 will calculate an ETag based on the http.etag_mode INI setting as well as
148 it will determine the last modification time of the sent entity. It uses
149 those two indicators to decide if the cache entry on the client side is
150 still valid and will emit an "304 Not Modified" response if applicable.
153 HttpResponse::setCacheControl('public');
154 HttpResponse::setCache(true);
155 HttpResponse::capture();
157 print "This will be cached until content changes!\n";
158 print "Note that this approach will only save the clients download time.\n";
161 - Bandwidth Throttling
163 HttpResponse supports a basic throttling mechanism, which is enabled by
164 setting a throttle delay and a buffer size. PHP will sleep the specified
165 amount of seconds after each sent chunk of specified bytes.
168 // send 5000 bytes every 0.2 seconds, i.e. max ~25kByte/s
169 HttpResponse::setThrottleDelay(0.2);
170 HttpResponse::setBufferSize(5000);
171 HttpResponse::setCache(true);
172 HttpResponse::setContentType('application/x-zip');
173 HttpResponse::setFile('../archive.zip');
174 HttpResponse::send();