File: Zend_Http_Client-Adapters.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (778 lines) | stat: -rw-r--r-- 31,555 bytes parent folder | download | duplicates (2)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.http.client.adapters">
    <title>Zend_Http_Client - Connection Adapters</title>

    <sect2 id="zend.http.client.adapters.overview">
        <title>Overview</title>

        <para>
            <classname>Zend_Http_Client</classname> is based on a connection adapter design. The
            connection adapter is the object in charge of performing the
            actual connection to the server, as well as writing requests
            and reading responses.
            This connection adapter can be replaced, and you can create and
            extend the default connection adapters to suite your special needs,
            without the need to extend or replace the entire <acronym>HTTP</acronym> client
            class, and with the same interface.
        </para>

        <para>
            Currently, the <classname>Zend_Http_Client</classname> class provides four built-in
            connection adapters:

            <itemizedlist>
                <listitem>
                    <para>
                        <classname>Zend_Http_Client_Adapter_Socket</classname> (default)
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <classname>Zend_Http_Client_Adapter_Proxy</classname>
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <classname>Zend_Http_Client_Adapter_Curl</classname>
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <classname>Zend_Http_Client_Adapter_Test</classname>
                    </para>
                </listitem>
            </itemizedlist>
        </para>

        <para>
            The <classname>Zend_Http_Client</classname> object's adapter connection adapter is set
            using the 'adapter' configuration option. When instantiating the
            client object, you can set the 'adapter' configuration option to
            a string containing the adapter's name (eg. 'Zend_Http_Client_Adapter_Socket')
            or to a variable holding an adapter object (eg.
            <command>new Zend_Http_Client_Adapter_Test</command>). You can also set the
            adapter later, using the <classname>Zend_Http_Client->setConfig()</classname> method.
        </para>
    </sect2>

    <sect2 id="zend.http.client.adapters.socket">
        <title>The Socket Adapter</title>

        <para>
            The default connection adapter is the
            <classname>Zend_Http_Client_Adapter_Socket</classname> adapter - this adapter will be
            used unless you explicitly set the connection adapter. The Socket adapter is based on
            <acronym>PHP</acronym>'s built-in fsockopen() function, and does not require any special
            extensions or compilation flags.
        </para>

        <para>
            The Socket adapter allows several extra configuration options that
            can be set using <classname>Zend_Http_Client->setConfig()</classname> or
            passed to the client constructor.

            <table id="zend.http.client.adapter.socket.configuration.table">
                <title>Zend_Http_Client_Adapter_Socket configuration parameters</title>

                <tgroup cols="4">
                    <thead>
                        <row>
                            <entry>Parameter</entry>
                            <entry>Description</entry>
                            <entry>Expected Type</entry>
                            <entry>Default Value</entry>
                        </row>
                    </thead>

                    <tbody>
                        <row>
                            <entry>persistent</entry>

                            <entry>
                                Whether to use persistent <acronym>TCP</acronym> connections
                            </entry>

                            <entry>boolean</entry>
                            <entry><constant>FALSE</constant></entry>
                        </row>

                        <row>
                            <entry>ssltransport</entry>
                            <entry>SSL transport layer (eg. 'sslv2', 'tls')</entry>
                            <entry>string</entry>
                            <entry>ssl</entry>
                        </row>

                        <row>
                            <entry>sslcert</entry>

                            <entry>
                                Path to a <acronym>PEM</acronym> encoded <acronym>SSL</acronym>
                                certificate
                            </entry>

                            <entry>string</entry>
                            <entry><constant>NULL</constant></entry>
                        </row>

                        <row>
                            <entry>sslpassphrase</entry>

                            <entry>
                                Passphrase for the <acronym>SSL</acronym> certificate file
                            </entry>

                            <entry>string</entry>
                            <entry><constant>NULL</constant></entry>
                        </row>

                        <row>
                            <entry>sslusecontext</entry>

                            <entry>
                                Enables proxied connections to use SSL even if
                                the proxy connection itself does not.
                            </entry>

                            <entry>boolean</entry>
                            <entry><constant>FALSE</constant></entry>
                        </row>
                    </tbody>
                </tgroup>
            </table>

            <note>
                <title>Persistent TCP Connections</title>

                <para>
                    Using persistent <acronym>TCP</acronym> connections can potentially speed up
                    <acronym>HTTP</acronym> requests - but in most use cases, will have little
                    positive effect and might overload the <acronym>HTTP</acronym> server you are
                    connecting to.
                </para>

                <para>
                    It is recommended to use persistent <acronym>TCP</acronym> connections only if
                    you connect to the same server very frequently, and are
                    sure that the server is capable of handling a large number
                    of concurrent connections. In any case you are encouraged
                    to benchmark the effect of persistent connections on both
                    the client speed and server load before using this option.
                </para>

                <para>
                    Additionally, when using persistent connections it is recommended to enable
                    Keep-Alive <acronym>HTTP</acronym> requests as described in <link
                        linkend="zend.http.client.configuration">the configuration section</link> -
                    otherwise persistent connections might have little or no effect.
                </para>
            </note>

            <note>
                <title>HTTPS SSL Stream Parameters</title>

                <para>
                    <property>ssltransport</property>, <property>sslcert</property> and
                    <property>sslpassphrase</property> are only relevant when connecting using
                    <acronym>HTTPS</acronym>.
                </para>

                <para>
                    While the default <acronym>SSL</acronym> settings should work for most
                    applications, you might need to change them if the server
                    you are connecting to requires special client setup. If so,
                    you should read the sections about <acronym>SSL</acronym> transport layers and
                    options <ulink
                        url="http://www.php.net/manual/en/transports.php#transports.inet">here</ulink>.
                </para>
            </note>
        </para>

        <example id="zend.http.client.adapters.socket.example-1">
            <title>Changing the HTTPS transport layer</title>

            <programlisting language="php"><![CDATA[
// Set the configuration parameters
$config = array(
    'adapter'      => 'Zend_Http_Client_Adapter_Socket',
    'ssltransport' => 'tls'
);

// Instantiate a client object
$client = new Zend_Http_Client('https://www.example.com', $config);

// The following request will be sent over a TLS secure connection.
$response = $client->request();
]]></programlisting>
        </example>

        <para>
            The result of the example above will be similar to opening a <acronym>TCP</acronym>
            connection using the following <acronym>PHP</acronym> command:
        </para>

        <para>
            <methodname>fsockopen('tls://www.example.com', 443)</methodname>
        </para>

        <sect3 id="zend.http.client.adapters.socket.streamcontext">
            <title>Customizing and accessing the Socket adapter stream context</title>

            <para>
                Starting from Zend Framework 1.9,
                <classname>Zend_Http_Client_Adapter_Socket</classname> provides direct access to the
                underlying <ulink
                    url="http://php.net/manual/en/stream.contexts.php">stream context</ulink> used
                to connect to the remote server. This allows the user to pass specific options and
                parameters to the <acronym>TCP</acronym> stream, and to the <acronym>SSL</acronym>
                wrapper in case of <acronym>HTTPS</acronym> connections.
            </para>

            <para>
                You can access the stream context using the following methods of
                <classname>Zend_Http_Client_Adapter_Socket</classname>:

                <itemizedlist>
                    <listitem>
                        <para>
                            <firstterm><methodname>setStreamContext($context)</methodname></firstterm>
                            Sets the stream context to be used by the adapter. Can accept either
                            a stream context resource created using the <ulink
                                url="http://php.net/manual/en/function.stream-context-create.php"><methodname>stream_context_create()</methodname></ulink>
                            <acronym>PHP</acronym> function, or an array of stream context options,
                            in the same format provided to this function. Providing an array will
                            create a new stream context using these options, and set it.
                        </para>
                    </listitem>

                    <listitem>
                        <para>
                            <firstterm><methodname>getStreamContext()</methodname></firstterm>
                            Get the stream context of the adapter. If no stream context was set,
                            will create a default stream context and return it. You can then set
                            or get the value of different context options using regular
                            <acronym>PHP</acronym> stream context functions.
                        </para>
                    </listitem>
                </itemizedlist>
            </para>

            <example id="zend.http.client.adapters.socket.streamcontext.example-1">
                <title>Setting stream context options for the Socket adapter</title>

                <programlisting language="php"><![CDATA[
// Array of options
$options = array(
    'socket' => array(
        // Bind local socket side to a specific interface
        'bindto' => '10.1.2.3:50505'
    ),
    'ssl' => array(
        // Verify server side certificate,
        // do not accept invalid or self-signed SSL certificates
        'verify_peer' => true,
        'allow_self_signed' => false,

        // Capture the peer's certificate
        'capture_peer_cert' => true
    )
);

// Create an adapter object and attach it to the HTTP client
$adapter = new Zend_Http_Client_Adapter_Socket();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);

// Method 1: pass the options array to setStreamContext()
$adapter->setStreamContext($options);

// Method 2: create a stream context and pass it to setStreamContext()
$context = stream_context_create($options);
$adapter->setStreamContext($context);

// Method 3: get the default stream context and set the options on it
$context = $adapter->getStreamContext();
stream_context_set_option($context, $options);

// Now, preform the request
$response = $client->request();

// If everything went well, you can now access the context again
$opts = stream_context_get_options($adapter->getStreamContext());
echo $opts['ssl']['peer_certificate'];
]]></programlisting>
            </example>

            <note>
                <para>
                    Note that you must set any stream context options before using the adapter
                    to preform actual requests. If no context is set before preforming
                    <acronym>HTTP</acronym> requests with the Socket adapter, a default stream
                    context will be created. This context resource could be accessed after
                    preforming any requests using the <methodname>getStreamContext()</methodname>
                    method.
                </para>
            </note>
        </sect3>
    </sect2>

    <sect2 id="zend.http.client.adapters.proxy">
        <title>The Proxy Adapter</title>

        <para>
            The <classname>Zend_Http_Client_Adapter_Proxy</classname> adapter is similar to the
            default Socket adapter - only the connection is made through an <acronym>HTTP</acronym>
            proxy server instead of a direct connection to the target server. This
            allows usage of <classname>Zend_Http_Client</classname> behind proxy servers - which is
            sometimes needed for security or performance reasons.
        </para>

        <para>
            Using the Proxy adapter requires several additional configuration
            parameters to be set, in addition to the default 'adapter' option:

            <table id="zend.http.client.adapters.proxy.table">
                <title>Zend_Http_Client configuration parameters</title>

                <tgroup cols="4">
                    <thead>
                        <row>
                            <entry>Parameter</entry>
                            <entry>Description</entry>
                            <entry>Expected Type</entry>
                            <entry>Example Value</entry>
                        </row>
                    </thead>

                    <tbody>
                        <row>
                            <entry>proxy_host</entry>
                            <entry>Proxy server address</entry>
                            <entry>string</entry>
                            <entry>'proxy.myhost.com' or '10.1.2.3'</entry>
                        </row>

                        <row>
                            <entry>proxy_port</entry>
                            <entry>Proxy server <acronym>TCP</acronym> port</entry>
                            <entry>integer</entry>
                            <entry>8080 (default) or 81</entry>
                        </row>

                        <row>
                            <entry>proxy_user</entry>
                            <entry>Proxy user name, if required</entry>
                            <entry>string</entry>
                            <entry>'shahar' or '' for none (default)</entry>
                        </row>

                        <row>
                            <entry>proxy_pass</entry>
                            <entry>Proxy password, if required</entry>
                            <entry>string</entry>
                            <entry>'secret' or '' for none (default)</entry>
                        </row>

                        <row>
                            <entry>proxy_auth</entry>
                            <entry>Proxy <acronym>HTTP</acronym> authentication type</entry>
                            <entry>string</entry>
                            <entry>Zend_Http_Client::AUTH_BASIC (default)</entry>
                        </row>
                    </tbody>
                </tgroup>
            </table>
        </para>

        <para>
            proxy_host should always be set - if it is not set, the client will
            fall back to a direct connection using
            <classname>Zend_Http_Client_Adapter_Socket</classname>. proxy_port defaults to '8080' -
            if your proxy listens on a different port you must set this one as well.
        </para>

        <para>
            proxy_user and proxy_pass are only required if your proxy server
            requires you to authenticate. Providing these will add a 'Proxy-Authentication'
            header to the request. If your proxy does not require authentication,
            you can leave these two options out.
        </para>

        <para>
            proxy_auth sets the proxy authentication type, if your proxy server
            requires authentication. Possibly values are similar to the ones
            accepted by the Zend_Http_Client::setAuth() method. Currently, only
            basic authentication (Zend_Http_Client::AUTH_BASIC) is supported.
        </para>

        <example id="zend.http.client.adapters.proxy.example-1">
            <title>Using Zend_Http_Client behind a proxy server</title>

            <programlisting language="php"><![CDATA[
// Set the configuration parameters
$config = array(
    'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
    'proxy_host' => 'proxy.int.zend.com',
    'proxy_port' => 8000,
    'proxy_user' => 'shahar.e',
    'proxy_pass' => 'bananashaped'
);

// Instantiate a client object
$client = new Zend_Http_Client('http://www.example.com', $config);

// Continue working...
]]></programlisting>
        </example>

        <para>
            As mentioned, if proxy_host is not set or is set to a blank string,
            the connection will fall back to a regular direct connection. This
            allows you to easily write your application in a way that allows a
            proxy to be used optionally, according to a configuration parameter.
        </para>

        <note>
            <para>
                Since the proxy adapter inherits from
                <classname>Zend_Http_Client_Adapter_Socket</classname>, you can use the stream
                context access method (see <link
                    linkend="zend.http.client.adapters.socket.streamcontext">this section</link>)
                to set stream context options on Proxy connections as demonstrated above.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.http.client.adapters.curl">
        <title>The cURL Adapter</title>

        <para>
            cURL is a standard <acronym>HTTP</acronym> client library that is distributed with many
            operating systems and can be used in <acronym>PHP</acronym> via the cURL extension. It
            offers functionality for many special cases which can occur for a
            <acronym>HTTP</acronym> client and make it a perfect choice for a
            <acronym>HTTP</acronym> adapter. It supports secure connections, proxy, all sorts of
            authentication mechanisms and shines in applications that move large files around
            between servers.
        </para>

        <example id="zend.http.client.adapters.curl.example-1">
            <title>Setting cURL options</title>

            <programlisting language="php"><![CDATA[
$config = array(
    'adapter'   => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
);
$client = new Zend_Http_Client($uri, $config);
]]></programlisting>
        </example>

        <para>
            By default the cURL adapter is configured to behave exactly like
            the Socket Adapter and it also accepts the same configuration parameters
            as the Socket and Proxy adapters. You can also change the cURL options by either
            specifying the 'curloptions' key in the constructor of the adapter or by calling
            <methodname>setCurlOption($name, $value)</methodname>. The <varname>$name</varname> key
            corresponds to the CURL_* constants of the cURL extension. You can
            get access to the Curl handle by calling <command>$adapter->getHandle();</command>
        </para>

        <example id="zend.http.client.adapters.curl.example-2">
            <title>Transfering Files by Handle</title>

            <para>
                You can use cURL to transfer very large files over <acronym>HTTP</acronym> by
                filehandle.
            </para>

            <programlisting language="php"><![CDATA[
$putFileSize   = filesize("filepath");
$putFileHandle = fopen("filepath", "r");

$adapter = new Zend_Http_Client_Adapter_Curl();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);
$adapter->setConfig(array(
    'curloptions' => array(
        CURLOPT_INFILE => $putFileHandle,
        CURLOPT_INFILESIZE => $putFileSize
    )
));
$client->request("PUT");
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.http.client.adapters.test">
        <title>The Test Adapter</title>

        <para>
            Sometimes, it is very hard to test code that relies on <acronym>HTTP</acronym>
            connections. For example, testing an application that pulls an <acronym>RSS</acronym>
            feed from a remote server will require a network connection, which is not always
            available.
        </para>

        <para>
            For this reason, the <classname>Zend_Http_Client_Adapter_Test</classname> adapter is
            provided. You can write your application to use <classname>Zend_Http_Client</classname>,
            and just for testing purposes, for example in your unit testing
            suite, you can replace the default adapter with a Test adapter (a
            mock object), allowing you to run tests without actually
            performing server connections.
        </para>

        <para>
            The <classname>Zend_Http_Client_Adapter_Test</classname> adapter provides an additional
            method, setResponse() method. This method takes one parameter,
            which represents an <acronym>HTTP</acronym> response as either text or a
            <classname>Zend_Http_Response</classname> object. Once set, your Test adapter will
            always return this response, without even performing an actual <acronym>HTTP</acronym>
            request.
        </para>

        <example id="zend.http.client.adapters.test.example-1">
            <title>Testing Against a Single HTTP Response Stub</title>

            <programlisting language="php"><![CDATA[
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    'adapter' => $adapter
));

// Set the expected response
$adapter->setResponse(
    "HTTP/1.1 200 OK"        . "\r\n" .
    "Content-type: text/xml" . "\r\n" .
                               "\r\n" .
    '<?xml version="1.0" encoding="UTF-8"?>' .
    '<rss version="2.0" ' .
    '     xmlns:content="http://purl.org/rss/1.0/modules/content/"' .
    '     xmlns:wfw="http://wellformedweb.org/CommentAPI/"' .
    '     xmlns:dc="http://purl.org/dc/elements/1.1/">' .
    '  <channel>' .
    '    <title>Premature Optimization</title>' .
    // and so on...
    '</rss>');

$response = $client->request('GET');
// .. continue parsing $response..
]]></programlisting>
        </example>

        <para>
            The above example shows how you can preset your <acronym>HTTP</acronym> client to
            return the response you need. Then, you can continue testing your
            own code, without being dependent on a network connection, the server's
            response, etc. In this case, the test would continue to check how
            the application parses the <acronym>XML</acronym> in the response body.
        </para>

        <para>
            Sometimes, a single method call to an object can result in that
            object performing multiple <acronym>HTTP</acronym> transactions. In this case, it's
            not possible to use setResponse() alone because there's no
            opportunity to set the next response(s) your program might need
            before returning to the caller.
        </para>

        <example id="zend.http.client.adapters.test.example-2">
            <title>Testing Against Multiple HTTP Response Stubs</title>

            <programlisting language="php"><![CDATA[
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    'adapter' => $adapter
));

// Set the first expected response
$adapter->setResponse(
    "HTTP/1.1 302 Found"      . "\r\n" .
    "Location: /"             . "\r\n" .
    "Content-Type: text/html" . "\r\n" .
                                "\r\n" .
    '<html>' .
    '  <head><title>Moved</title></head>' .
    '  <body><p>This page has moved.</p></body>' .
    '</html>');

// Set the next successive response
$adapter->addResponse(
    "HTTP/1.1 200 OK"         . "\r\n" .
    "Content-Type: text/html" . "\r\n" .
                                "\r\n" .
    '<html>' .
    '  <head><title>My Pet Store Home Page</title></head>' .
    '  <body><p>...</p></body>' .
    '</html>');

// inject the http client object ($client) into your object
// being tested and then test your object's behavior below
]]></programlisting>
        </example>

        <para>
            The setResponse() method clears any responses in the
            <classname>Zend_Http_Client_Adapter_Test</classname>'s buffer and sets the
            first response that will be returned. The addResponse()
            method will add successive responses.
        </para>

        <para>
            The responses will be replayed in the order that they
            were added. If more requests are made than the number
            of responses stored, the responses will cycle again
            in order.
        </para>

        <para>
            In the example above, the adapter is configured to test your
            object's behavior when it encounters a 302 redirect. Depending on
            your application, following a redirect may or may not be desired
            behavior. In our example, we expect that the redirect will be
            followed and we configure the test adapter to help us test this.
            The initial 302 response is set up with the setResponse() method
            and the 200 response to be returned next is added with the
            addResponse() method. After configuring the test adapter, inject
            the <acronym>HTTP</acronym> client containing the adapter into your object under test
            and test its behavior.
        </para>

        <para>
            If you need the adapter to fail on demand you can use
            <methodname>setNextRequestWillFail($flag)</methodname>. The method will cause the next
            call to <methodname>connect()</methodname> to throw an
            <classname>Zend_Http_Client_Adapter_Exception</classname> exception. This can be useful
            when your application caches content from an external site (in case the site goes down)
            and you want to test this feature.
        </para>

        <example id="zend.http.client.adapters.test.example-3">
            <title>Forcing the adapter to fail</title>

            <programlisting language="php"><![CDATA[
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    'adapter' => $adapter
));

// Force the next request to fail with an exception
$adapter->setNextRequestWillFail(true);

try {
    // This call will result in a Zend_Http_Client_Adapter_Exception
    $client->request();
} catch (Zend_Http_Client_Adapter_Exception $e) {
    // ...
}

// Further requests will work as expected until
// you call setNextRequestWillFail(true) again
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.http.client.adapters.extending">
        <title>Creating your own connection adapters</title>

        <para>
            You can create your own connection adapters and use them. You could, for
            example, create a connection adapter that uses persistent sockets,
            or a connection adapter with caching abilities, and use them as
            needed in your application.
        </para>

        <para>
            In order to do so, you must create your own adapter class that implements
            the <classname>Zend_Http_Client_Adapter_Interface</classname> interface. The following
            example shows the skeleton of a user-implemented adapter class. All the public
            functions defined in this example must be defined in your adapter as well:
        </para>

        <example id="zend.http.client.adapters.extending.example-1">
            <title>Creating your own connection adapter</title>

            <programlisting language="php"><![CDATA[
class MyApp_Http_Client_Adapter_BananaProtocol
    implements Zend_Http_Client_Adapter_Interface
{
    /**
     * Set the configuration array for the adapter
     *
     * @param array $config
     */
    public function setConfig($config = array())
    {
        // This rarely changes - you should usually copy the
        // implementation in Zend_Http_Client_Adapter_Socket.
    }

    /**
     * Connect to the remote server
     *
     * @param string  $host
     * @param int     $port
     * @param boolean $secure
     */
    public function connect($host, $port = 80, $secure = false)
    {
        // Set up the connection to the remote server
    }

    /**
     * Send request to the remote server
     *
     * @param string        $method
     * @param Zend_Uri_Http $url
     * @param string        $http_ver
     * @param array         $headers
     * @param string        $body
     * @return string Request as text
     */
    public function write($method,
                          $url,
                          $http_ver = '1.1',
                          $headers = array(),
                          $body = '')
    {
        // Send request to the remote server.
        // This function is expected to return the full request
        // (headers and body) as a string
    }

    /**
     * Read response from server
     *
     * @return string
     */
    public function read()
    {
        // Read response from remote server and return it as a string
    }

    /**
     * Close the connection to the server
     *
     */
    public function close()
    {
        // Close the connection to the remote server - called last.
    }
}

// Then, you could use this adapter:
$client = new Zend_Http_Client(array(
    'adapter' => 'MyApp_Http_Client_Adapter_BananaProtocol'
));
]]></programlisting>
        </example>
    </sect2>
</sect1>