8d8998d0a05a5df8c588e85b38a60507d31ec3c9
[m6w6/ext-http] / docs / examples / tutorial.txt
1
2 A Beginners Tutorial
3 --------------------
4 $Revision$
5
6
7 - GET Queries
8
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.
13
14 <?php
15 $r = new HttpRequest('http://www.google.com/search');
16
17 // store Googles cookies in a dedicated file
18 touch('google.txt');
19 $r->setOptions(
20 array( 'cookiestore' => 'google.txt',
21 )
22 );
23
24 $r->setQueryData(
25 array( 'q' => '+"pecl_http" -msg -cvs -list',
26 'hl' => 'de'
27 )
28 );
29
30 // HttpRequest::send() returns an HttpMessage object
31 // of type HttpMessage::TYPE_RESPONSE or throws an exception
32 try {
33 print $r->send()->getBody();
34 } catch (HttpException $e) {
35 print $e;
36 }
37 ?>
38
39 - Multipart Posts
40
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
45 redirect option.
46
47 <?php
48 $r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
49
50 // if redirects is set to true, a single redirect is allowed;
51 // one can set any reasonable count of allowed redirects
52 $r->setOptions(
53 array( 'cookies' => array('MyCookie' => 'has a value'),
54 'redirect' => true,
55 )
56 );
57
58 // common form data
59 $r->setPostFields(
60 array( 'name' => 'Mike',
61 'mail' => 'mike@php.net',
62 )
63 );
64 // add the file to post (form name, file name, file type)
65 touch('profile.jpg');
66 $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
67
68 try {
69 print $r->send()->getBody();
70 } catch (HttpException $e) {
71 print $e;
72 }
73 ?>
74
75 - Parallel Requests
76
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
80 at the same time.
81
82 <?php
83 try {
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) {
90 print $e;
91 exit;
92 }
93
94 try {
95 $p->send();
96 // HttpRequestPool implements an iterator over attached HttpRequest objects
97 foreach ($p as $r) {
98 echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
99 }
100 } catch (HttpException $e) {
101 print $e;
102 }
103 ?>
104
105 - Parallel Requests?
106
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.
110
111 <?php
112 class Pool extends HttpRequestPool
113 {
114 public function __construct()
115 {
116 parent::__construct(
117 new HttpRequest('http://pear.php.net', HTTP_METH_HEAD),
118 new HttpRequest('http://pecl.php.net', HTTP_METH_HEAD)
119 );
120
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!");
129 }
130 }
131 print "\nDone!\n";
132 }
133 }
134
135 try {
136 foreach (new Pool as $r) {
137 echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
138 }
139 } catch (HttpException $ex) {
140 print $e;
141 }
142 ?>
143
144 - Cached Responses
145
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.
151
152 <?php
153 HttpResponse::setCacheControl('public');
154 HttpResponse::setCache(true);
155 HttpResponse::capture();
156
157 print "This will be cached until content changes!\n";
158 print "Note that this approach will only save the clients download time.\n";
159 ?>
160
161 - Bandwidth Throttling
162
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.
166
167 <?php
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();
175 ?>