Location: PHPKode > projects > Guzzle > guzzle.github.com-master/http-client/client.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
  
    <title>The Guzzle HTTP client &mdash; Guzzle 3.0.0 documentation</title>
  <link rel="stylesheet" type="text/css" href="../_static/bootstrap.min.css" />
  <script type="text/javascript" src="../_static/jquery-1.9.1.min.js.html"></script>
    
    <link rel="stylesheet" href="../_static/guzzle.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.0.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="Guzzle 3.0.0 documentation" href="../index.html" />
    <link rel="up" title="Guzzle Documentation" href="../docs.html" />
    <link rel="next" title="Using Request objects" href="request.html" />
    <link rel="prev" title="Installation" href="../getting-started/installation.html" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../_static/bootstrap-responsive.min.css" />
  
   

  </head>
  <body>
  <div class="navbar navbar-fixed-top ">
    <div class="navbar-inner">
      <div class="container">
        <a class="brand" href="../index.html">Guzzle</a>
        <ul class="nav">
<li><a href="../index.html">Home</a></li>
<li><a href="../docs.html">Docs</a></li>
<li><a href="http://guzzlephp.org/api/index.html">API</a></li>
<li><a href="https://github.com/guzzle/guzzle">GitHub</a></li>
<li><a href="https://groups.google.com/forum/?hl=en#!forum/guzzle">Forum</a></li>
<li><a href="https://packagist.org/packages/guzzle/guzzle">Packagist</a></li>
<li><a href="irc:irc.freenode.com/#guzzlephp">IRC</a></li>
        </ul>
        <div class="pull-right" id="github-stars">
          <iframe src="http://ghbtns.com/github-btn.html?user=guzzle&repo=guzzle&type=watch&count=true&size=small"
                  allowtransparency="true" frameborder="0" scrolling="0" width="110px" height="20px"></iframe>
        </div>
      </div>
    </div>
  </div>
  

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="request.html" title="Using Request objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../getting-started/installation.html" title="Installation"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">Guzzle</a> &raquo;</li>
          <li><a href="../docs.html" accesskey="U">Guzzle Documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="container">
        
    <div class="top-links">
      <ul class="breadcrumb pull-right">
        <li>
          <a href="../getting-started/installation.html" title="previous chapter (use the left arrow)">&larr; Installation</a>
          <span class="divider">/</span>
          
        </li>
          <li><a href="request.html" title="next chapter (use the right arrow)">Using Request objects &rarr;</a></li>
      </ul>
    </div>
  
        <div class="document clearer">
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The Guzzle HTTP client</a><ul>
<li><a class="reference internal" href="#creating-a-client">Creating a Client</a><ul>
<li><a class="reference internal" href="#base-urls">Base URLs</a></li>
<li><a class="reference internal" href="#configuration-options">Configuration options</a></li>
<li><a class="reference internal" href="#setting-a-custom-user-agent">Setting a custom User-Agent</a></li>
</ul>
</li>
<li><a class="reference internal" href="#creating-requests-with-a-client">Creating requests with a client</a></li>
<li><a class="reference internal" href="#static-clients">Static clients</a></li>
<li><a class="reference internal" href="#request-options">Request options</a><ul>
<li><a class="reference internal" href="#headers">headers</a></li>
<li><a class="reference internal" href="#query">query</a></li>
<li><a class="reference internal" href="#body">body</a></li>
<li><a class="reference internal" href="#auth">auth</a></li>
<li><a class="reference internal" href="#cookies">cookies</a></li>
<li><a class="reference internal" href="#allow-redirects">allow_redirects</a></li>
<li><a class="reference internal" href="#save-to">save_to</a></li>
<li><a class="reference internal" href="#events">events</a></li>
<li><a class="reference internal" href="#plugins">plugins</a></li>
<li><a class="reference internal" href="#exceptions">exceptions</a></li>
<li><a class="reference internal" href="#params">params</a></li>
<li><a class="reference internal" href="#timeout-connect-timeout">timeout / connect_timeout</a></li>
<li><a class="reference internal" href="#verify">verify</a></li>
<li><a class="reference internal" href="#proxy">proxy</a></li>
<li><a class="reference internal" href="#debug">debug</a></li>
<li><a class="reference internal" href="#stream">stream</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sending-requests">Sending requests</a><ul>
<li><a class="reference internal" href="#sending-requests-in-parallel">Sending requests in parallel</a></li>
</ul>
</li>
<li><a class="reference internal" href="#plugins-and-events">Plugins and events</a><ul>
<li><a class="reference internal" href="#using-the-same-cookie-session-for-each-request">Using the same cookie session for each request</a></li>
<li><a class="reference internal" href="#events-emitted-from-a-client">Events emitted from a client</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="hidden-phone">
  <h3>Need help?</h3>
  <p>Please post to our google group or #guzzlephp IRC channel on freenode if you need support or have a technical
  question.</p>
</div>
<div class="hidden-phone">
  <h3>Found an error?</h3>
  <p>If you find an error in the documentation, please file a ticket or submit a pull request on
  <a href="https://github.com/guzzle/guzzle">GitHub</a></p>
</div>
<div id="searchbox" style="display: none">
    <h3>Quick search</h3>
    <form class="form-search margin-top-1em" action="../search.html" method="get">
        <input type="text" name="q" style="width: 105px" class="input-small search-query" />
        <button type="submit" class="btn btn-small">Search</button>
        <input type="hidden" name="check_keywords" value="yes" />
        <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
            <div class="body">
              
  <div class="section" id="the-guzzle-http-client">
<h1>The Guzzle HTTP client<a class="headerlink" href="#the-guzzle-http-client" title="Permalink to this headline">¶</a></h1>
<p>Guzzle gives PHP developers complete control over HTTP requests while utilizing HTTP/1.1 best practices. Guzzle's HTTP
functionality is a robust framework built on top of the <a class="reference external" href="http://www.php.net/curl">PHP libcurl bindings</a>.</p>
<p>The three main parts of the Guzzle HTTP client are:</p>
<table class="table table-bordered">
<colgroup>
<col width="12%" />
<col width="88%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Clients</td>
<td><tt class="docutils literal"><span class="pre">Guzzle\Http\Client</span></tt> (creates and sends requests, associates a response with a request)</td>
</tr>
<tr class="row-even"><td>Requests</td>
<td><tt class="docutils literal"><span class="pre">Guzzle\Http\Message\Request</span></tt> (requests with no body),
<tt class="docutils literal"><span class="pre">Guzzle\Http\Message\EntityEnclosingRequest</span></tt> (requests with a body)</td>
</tr>
<tr class="row-odd"><td>Responses</td>
<td><tt class="docutils literal"><span class="pre">Guzzle\Http\Message\Response</span></tt></td>
</tr>
</tbody>
</table>
<div class="section" id="creating-a-client">
<h2>Creating a Client<a class="headerlink" href="#creating-a-client" title="Permalink to this headline">¶</a></h2>
<p>Clients create requests, send requests, and set responses on a request object. When instantiating a client object,
you can pass an optional &quot;base URL&quot; and optional array of configuration options. A base URL is a
<a class="reference internal" href="uri-templates.html"><em>URI template</em></a> that contains the URL of a remote server. When creating requests with a relative
URL, the base URL of a client will be merged into the request's URL.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Http\Client</span><span class="p">;</span>

<span class="c1">// Create a client and provide a base URL</span>
<span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Client</span><span class="p">(</span><span class="s1">&#39;https://api.github.com&#39;</span><span class="p">);</span>

<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;/user&#39;</span><span class="p">);</span>
<span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setAuth</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">,</span> <span class="s1">&#39;pass&#39;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">getUrl</span><span class="p">();</span>
<span class="c1">// &gt;&gt;&gt; https://api.github.com/user</span>

<span class="c1">// You must send a request in order for the transfer to occur</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getBody</span><span class="p">();</span>
<span class="c1">// &gt;&gt;&gt; {&quot;type&quot;:&quot;User&quot;, ...</span>

<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeader</span><span class="p">(</span><span class="s1">&#39;Content-Length&#39;</span><span class="p">);</span>
<span class="c1">// &gt;&gt;&gt; 792</span>

<span class="nv">$data</span> <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">json</span><span class="p">();</span>
<span class="k">echo</span> <span class="nv">$data</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">];</span>
<span class="c1">// &gt;&gt;&gt; User</span>
</pre></div>
</div>
<div class="section" id="base-urls">
<h3>Base URLs<a class="headerlink" href="#base-urls" title="Permalink to this headline">¶</a></h3>
<p>Notice that the URL provided to the client's <tt class="docutils literal"><span class="pre">get()</span></tt> method is relative. Relative URLs will always merge into the
base URL of the client. There are a few rules that control how the URLs are merged.</p>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">Guzzle follows <a class="reference external" href="http://tools.ietf.org/html/rfc3986#section-5.2">RFC 3986</a> when merging base URLs and
relative URLs.</p>
</div>
<p>In the above example, we passed <tt class="docutils literal"><span class="pre">/user</span></tt> to the <tt class="docutils literal"><span class="pre">get()</span></tt> method of the client. This is a relative URL, so it will
merge into the base URL of the client-- resulting in the derived URL of <tt class="docutils literal"><span class="pre">https://api.github.com/users</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">/user</span></tt> is a relative URL but uses an absolute path because it contains the leading slash. Absolute paths will
overwrite any existing path of the base URL. If an absolute path is provided (e.g. <tt class="docutils literal"><span class="pre">/path/to/something</span></tt>), then the
path specified in the base URL of the client will be replaced with the absolute path, and the query string provided
by the relative URL will replace the query string of the base URL.</p>
<p>Omitting the leading slash and using relative paths will add to the path of the base URL of the client. So using a
client base URL of <tt class="docutils literal"><span class="pre">https://api.twitter.com/v1.1</span></tt> and creating a GET request with <tt class="docutils literal"><span class="pre">statuses/user_timeline.json</span></tt>
will result in a URL of <tt class="docutils literal"><span class="pre">https://api.twitter.com/v1.1/statuses/user_timeline.json</span></tt>. If a relative path and a query
string are provided, then the relative path will be appended to the base URL path, and the query string provided will
be merged into the query string of the base URL.</p>
<p>If an absolute URL is provided (e.g. <tt class="docutils literal"><span class="pre">http://httpbin.org/ip</span></tt>), then the request will completely use the absolute URL
as-is without merging in any of the URL parts specified in the base URL.</p>
</div>
<div class="section" id="configuration-options">
<h3>Configuration options<a class="headerlink" href="#configuration-options" title="Permalink to this headline">¶</a></h3>
<p>The second argument of the client's constructor is an array of configuration data. This can include URI template data
or special options that alter the client's behavior:</p>
<table class="table table-bordered">
<colgroup>
<col width="27%" />
<col width="73%" />
</colgroup>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">request.options</span></tt></td>
<td>Associative array of <a class="reference internal" href="#request-options"><em>Request options</em></a> to apply to every
request created by the client.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">redirect.disable</span></tt></td>
<td>Disable HTTP redirects for every request created by the client.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">curl.options</span></tt></td>
<td>Associative array of cURL options to apply to every request created by the client.
if either the key or value of an entry in the array is a string, Guzzle will
attempt to find a matching defined cURL constant automatically (e.g.
&quot;CURLOPT_PROXY&quot; will be converted to the constant <tt class="docutils literal"><span class="pre">CURLOPT_PROXY</span></tt>).</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">ssl.certificate_authority</span></tt></td>
<td><p class="first">Set to true to use the Guzzle bundled SSL certificate bundle (this is used by
default, or null to use the bundle on your system, a string pointing to a file to
use a specific certificate file, a string pointing to a directory to use multiple
certificates, or <tt class="docutils literal"><span class="pre">false</span></tt> to disable SSL validation (not recommended).</p>
<p class="last">When using  Guzzle inside of a phar file, the bundled SSL certificate will be
extracted to your system's temp folder, and each time a client is created an MD5
check will be performed to ensure the integrity of the certificate.</p>
</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">command.params</span></tt></td>
<td>When using a <tt class="docutils literal"><span class="pre">Guzzle\Service\Client</span></tt> object, this is an associative array of
default options to set on each command created by the client.</td>
</tr>
</tbody>
</table>
<p>Here's an example showing how to set various configuration options, including default headers to send with each request,
defauly query string parameters to add to each request, a default auth scheme for each request, and a proxy to use for
each request. Values can be injected into the client's base URL using variables from the configuration array.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Http\Client</span><span class="p">;</span>

<span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Client</span><span class="p">(</span><span class="s1">&#39;https://api.twitter.com/{version}&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;version&#39;</span>        <span class="o">=&gt;</span> <span class="s1">&#39;v1.1&#39;</span><span class="p">,</span>
    <span class="s1">&#39;request.options&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;headers&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Bar&#39;</span><span class="p">),</span>
        <span class="s1">&#39;query&#39;</span>   <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;testing&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;123&#39;</span><span class="p">),</span>
        <span class="s1">&#39;auth&#39;</span>    <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">,</span> <span class="s1">&#39;Basic|Digest|NTLM|Any&#39;</span><span class="p">),</span>
        <span class="s1">&#39;proxy&#39;</span>   <span class="o">=&gt;</span> <span class="s1">&#39;tcp://localhost:80&#39;</span>
    <span class="p">)</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="setting-a-custom-user-agent">
<h3>Setting a custom User-Agent<a class="headerlink" href="#setting-a-custom-user-agent" title="Permalink to this headline">¶</a></h3>
<p>The default Guzzle User-Agent header is <tt class="docutils literal"><span class="pre">Guzzle/&lt;Guzzle_Version&gt;</span> <span class="pre">curl/&lt;curl_version&gt;</span> <span class="pre">PHP/&lt;PHP_VERSION&gt;</span></tt>. You can
customize the User-Agent header of a client by calling the <tt class="docutils literal"><span class="pre">setUserAgent()</span></tt> method of a Client object.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="c1">// Completely override the default User-Agent</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setUserAgent</span><span class="p">(</span><span class="s1">&#39;Test/123&#39;</span><span class="p">);</span>

<span class="c1">// Prepend a string to the default User-Agent</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setUserAgent</span><span class="p">(</span><span class="s1">&#39;Test/123&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="creating-requests-with-a-client">
<h2>Creating requests with a client<a class="headerlink" href="#creating-requests-with-a-client" title="Permalink to this headline">¶</a></h2>
<p>A Client object exposes several methods used to create Request objects:</p>
<ul class="simple">
<li>Create a custom HTTP request: <tt class="docutils literal"><span class="pre">$client-&gt;createRequest($method,</span> <span class="pre">$uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$body,</span> <span class="pre">$options)</span></tt></li>
<li>Create a GET request: <tt class="docutils literal"><span class="pre">$client-&gt;get($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$options)</span></tt></li>
<li>Create a HEAD request: <tt class="docutils literal"><span class="pre">$client-&gt;head($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$options)</span></tt></li>
<li>Create a DELETE request: <tt class="docutils literal"><span class="pre">$client-&gt;delete($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$body,</span> <span class="pre">$options)</span></tt></li>
<li>Create a POST request: <tt class="docutils literal"><span class="pre">$client-&gt;post($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$postBody,</span> <span class="pre">$options)</span></tt></li>
<li>Create a PUT request: <tt class="docutils literal"><span class="pre">$client-&gt;put($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$body,</span> <span class="pre">$options)</span></tt></li>
<li>Create a PATCH request: <tt class="docutils literal"><span class="pre">$client-&gt;patch($uri,</span> <span class="pre">array</span> <span class="pre">$headers,</span> <span class="pre">$body,</span> <span class="pre">$options)</span></tt></li>
</ul>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Http\Client</span><span class="p">;</span>

<span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Client</span><span class="p">(</span><span class="s1">&#39;http://baseurl.com/api/v1&#39;</span><span class="p">);</span>

<span class="c1">// Create a GET request using Relative to base URL</span>
<span class="c1">// URL of the request: http://baseurl.com/api/v1/path?query=123&amp;value=abc)</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;path?query=123&amp;value=abc&#39;</span><span class="p">);</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="c1">// Create HEAD request using a relative URL with an absolute path</span>
<span class="c1">// URL of the request: http://baseurl.com/path?query=123&amp;value=abc</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">head</span><span class="p">(</span><span class="s1">&#39;/path?query=123&amp;value=abc&#39;</span><span class="p">);</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="c1">// Create a DELETE request using an absolute URL</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/path?query=123&amp;value=abc&#39;</span><span class="p">);</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="c1">// Create a PUT request using the contents of a PHP stream as the body</span>
<span class="c1">// Specify custom HTTP headers</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">put</span><span class="p">(</span><span class="s1">&#39;http://www.example.com/upload&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;X-Header&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Header&#39;</span>
<span class="p">),</span> <span class="nb">fopen</span><span class="p">(</span><span class="s1">&#39;http://www.test.com/&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">));</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="c1">// Create a POST request and add the POST files manually</span>
<span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">(</span><span class="s1">&#39;http://localhost:8983/solr/update&#39;</span><span class="p">)</span>
    <span class="o">-&gt;</span><span class="na">addPostFiles</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;file&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;/path/to/documents.xml&#39;</span><span class="p">));</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>

<span class="c1">// Check if a resource supports the DELETE method</span>
<span class="nv">$supportsDelete</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">options</span><span class="p">(</span><span class="s1">&#39;/path&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">isMethodAllowed</span><span class="p">(</span><span class="s1">&#39;DELETE&#39;</span><span class="p">);</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>
</pre></div>
</div>
<p>Client objects create Request objects using a request factory (<tt class="docutils literal"><span class="pre">Guzzle\Http\Message\RequestFactoryInterface</span></tt>).
You can inject a custom request factory into the Client using <tt class="docutils literal"><span class="pre">$client-&gt;setRequestFactory()</span></tt>, but you can typically
rely on a Client's default request factory.</p>
</div>
<div class="section" id="static-clients">
<h2>Static clients<a class="headerlink" href="#static-clients" title="Permalink to this headline">¶</a></h2>
<p>You can use Guzzle's static client facade to more easily send simple HTTP requests.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="c1">// Mount the client so that you can access it at \Guzzle</span>
<span class="nx">Guzzle\Http\StaticClient</span><span class="o">::</span><span class="na">mount</span><span class="p">();</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nx">Guzzle</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://guzzlephp.org&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>Each request method of the static client (e.g. <tt class="docutils literal"><span class="pre">get()</span></tt>, <tt class="docutils literal"><span class="pre">post()`,</span> <span class="pre">``put()</span></tt>, etc) accepts an associative array of request
options to apply to the request.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$response</span> <span class="o">=</span> <span class="nx">Guzzle</span><span class="o">::</span><span class="na">post</span><span class="p">(</span><span class="s1">&#39;http://test.com&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;headers&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;X-Foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Bar&#39;</span><span class="p">),</span>
    <span class="s1">&#39;body&#39;</span>    <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Test&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;123&#39;</span><span class="p">),</span>
    <span class="s1">&#39;timeout&#39;</span> <span class="o">=&gt;</span> <span class="mi">10</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="request-options">
<span id="id1"></span><h2>Request options<a class="headerlink" href="#request-options" title="Permalink to this headline">¶</a></h2>
<p>Request options can be specified when creating a request or in the <tt class="docutils literal"><span class="pre">request.options</span></tt> parameter of a client. These
options can control various aspects of a request including: headers to send, query string data, where the response
should be downloaded, proxies, auth, etc.</p>
<div class="section" id="headers">
<h3>headers<a class="headerlink" href="#headers" title="Permalink to this headline">¶</a></h3>
<p>Associative array of headers to apply to the request. When specified in the <tt class="docutils literal"><span class="pre">$options`</span> <span class="pre">argument</span> <span class="pre">of</span> <span class="pre">a</span> <span class="pre">client</span> <span class="pre">creational</span>
<span class="pre">method</span> <span class="pre">(e.g.</span> <span class="pre">``get()</span></tt>, <tt class="docutils literal"><span class="pre">post()</span></tt>, etc), the headers in the <tt class="docutils literal"><span class="pre">$options</span></tt> array will overwrite headers specified in the
<cite>$headers</cite> array.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;headers&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;X-Foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Bar&#39;</span><span class="p">)</span>
<span class="p">));</span>
</pre></div>
</div>
<p>Headers can be specified on a client to add default headers to every request sent by a client.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Guzzle\Http\Client</span><span class="p">();</span>

<span class="c1">// Set a single header using path syntax</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setDefaultOption</span><span class="p">(</span><span class="s1">&#39;headers/X-Foo&#39;</span><span class="p">,</span> <span class="s1">&#39;Bar&#39;</span><span class="p">);</span>

<span class="c1">// Set all headers</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setDefaultOption</span><span class="p">(</span><span class="s1">&#39;headers&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;X-Foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Bar&#39;</span><span class="p">));</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>In addition to setting request options when creating requests or using the <tt class="docutils literal"><span class="pre">setDefaultOption()</span></tt> method, any
default client request option can be set using a client's config object:</p>
<div class="last highlight-php"><div class="highlight"><pre><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getConfig</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">setPath</span><span class="p">(</span><span class="s1">&#39;request.options/header/X-Foo&#39;</span><span class="p">,</span> <span class="s1">&#39;Bar&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="query">
<h3>query<a class="headerlink" href="#query" title="Permalink to this headline">¶</a></h3>
<p>Associative array of query string parameters to the request. When specified in the <tt class="docutils literal"><span class="pre">$options</span></tt> argument of a client
creational method, the query string parameters in the <tt class="docutils literal"><span class="pre">$options</span></tt> array will overwrite query string parameters
specified in the <cite>$url</cite>.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;query&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;123&#39;</span><span class="p">)</span>
<span class="p">));</span>
</pre></div>
</div>
<p>Query string parameters can be specified on a client to add default query string parameters to every request sent by a
client.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Guzzle\Http\Client</span><span class="p">();</span>

<span class="c1">// Set a single query string parameter using path syntax</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setDefaultOption</span><span class="p">(</span><span class="s1">&#39;query/abc&#39;</span><span class="p">,</span> <span class="s1">&#39;123&#39;</span><span class="p">);</span>

<span class="c1">// Set an array of default query string parameters</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setDefaultOption</span><span class="p">(</span><span class="s1">&#39;query&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;123&#39;</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="body">
<h3>body<a class="headerlink" href="#body" title="Permalink to this headline">¶</a></h3>
<p>Sets the body of a request. The value supplied to the body option can be a <tt class="docutils literal"><span class="pre">Guzzle\Http\EntitBodyInterface</span></tt>, string,
fopen resource, or array when sending POST requests. When a <tt class="docutils literal"><span class="pre">body</span></tt> request option is supplied, the option value will
ovewrite the <tt class="docutils literal"><span class="pre">$body</span></tt> argument of a client creational method.</p>
</div>
<div class="section" id="auth">
<h3>auth<a class="headerlink" href="#auth" title="Permalink to this headline">¶</a></h3>
<p>Specifies and array of HTTP authorization parameters parameters to use with the request. The array must contain the
username in index [0], the password in index [2], and can optionally contain the authentication type in index [3].
The available authentication types are: &quot;Basic&quot; (default), &quot;Digest&quot;, &quot;NTLM&quot;, or &quot;Any&quot;.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;auth&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">,</span> <span class="s1">&#39;Digest&#39;</span><span class="p">)</span>
<span class="p">));</span>

<span class="c1">// You can add auth headers to every request of a client</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setDefaultOption</span><span class="p">(</span><span class="s1">&#39;auth&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">,</span> <span class="s1">&#39;Digest&#39;</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="cookies">
<h3>cookies<a class="headerlink" href="#cookies" title="Permalink to this headline">¶</a></h3>
<p>Specifies an associative array of cookies to add to the request.</p>
</div>
<div class="section" id="allow-redirects">
<h3>allow_redirects<a class="headerlink" href="#allow-redirects" title="Permalink to this headline">¶</a></h3>
<p>Specifies whether or not the request should follow redirects. Requests will follow redirects by default. Set
<tt class="docutils literal"><span class="pre">allow_redirects</span></tt> to <tt class="docutils literal"><span class="pre">false</span></tt> to disable redirects.</p>
</div>
<div class="section" id="save-to">
<h3>save_to<a class="headerlink" href="#save-to" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">save_to</span></tt> option specifies where the body of a response is downloaded. You can pass the path to a file, an fopen
resource, or a <tt class="docutils literal"><span class="pre">Guzzle\Http\EntityBodyInterface</span></tt> object.</p>
<p>See <a class="reference internal" href="request.html#request-set-response-body"><em>Changing where a response is downloaded</em></a> for more information on setting the
<cite>save_to</cite> option.</p>
</div>
<div class="section" id="events">
<h3>events<a class="headerlink" href="#events" title="Permalink to this headline">¶</a></h3>
<p>The <cite>events</cite> option makes it easy to attach listeners to the various events emitted by a request object. The <cite>events</cite>
options must be an associative array mapping an event name to a Closure or array the contains a Closure and the
priority of the event.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;events&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;request.before_send&#39;</span> <span class="o">=&gt;</span> <span class="k">function</span> <span class="p">(</span><span class="nx">\Guzzle\Common\Event</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">echo</span> <span class="s1">&#39;About to send &#39;</span> <span class="o">.</span> <span class="nv">$e</span><span class="p">[</span><span class="s1">&#39;request&#39;</span><span class="p">];</span>
        <span class="p">}</span>
    <span class="p">)</span>
<span class="p">));</span>

<span class="c1">// Using the static client:</span>
<span class="nx">Guzzle</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;events&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;request.before_send&#39;</span> <span class="o">=&gt;</span> <span class="k">function</span> <span class="p">(</span><span class="nx">\Guzzle\Common\Event</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">echo</span> <span class="s1">&#39;About to send &#39;</span> <span class="o">.</span> <span class="nv">$e</span><span class="p">[</span><span class="s1">&#39;request&#39;</span><span class="p">];</span>
        <span class="p">}</span>
    <span class="p">)</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="plugins">
<h3>plugins<a class="headerlink" href="#plugins" title="Permalink to this headline">¶</a></h3>
<p>The <cite>plugins</cite> options makes it easy to attach an array of plugins to a request.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="c1">// Using the static client:</span>
<span class="nx">Guzzle</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$url</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;plugins&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
        <span class="k">new</span> <span class="nx">Guzzle\Plugin\Cache\CachePlugin</span><span class="p">(),</span>
        <span class="k">new</span> <span class="nx">Guzzle\Plugin\Cookie\CookiePlugin</span><span class="p">()</span>
    <span class="p">)</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="exceptions">
<h3>exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<p>The <cite>exceptions</cite> option can be used to disable throwing exceptions for unsuccessful HTTP response codes
(e.g. 404, 500, etc). Set <cite>exceptions</cite> to false to not throw exceptions.</p>
</div>
<div class="section" id="params">
<h3>params<a class="headerlink" href="#params" title="Permalink to this headline">¶</a></h3>
<p>The <cite>params</cite> options can be used to specify an associative array of data parameters to add to a request.</p>
</div>
<div class="section" id="timeout-connect-timeout">
<h3>timeout / connect_timeout<a class="headerlink" href="#timeout-connect-timeout" title="Permalink to this headline">¶</a></h3>
<p>You can specify the maximum number of seconds to allow for an entire transfer to take place before timing out using
the <cite>timeout</cite> request option. You can specify the maximum number of seconds to wait while trying to connect using the
<cite>connect_timeout</cite> request option. Set either of these options to 0 to wait indefinitely.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://www.example.com&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;timeout&#39;</span>         <span class="o">=&gt;</span> <span class="mi">20</span><span class="p">,</span>
    <span class="s1">&#39;connect_timeout&#39;</span> <span class="o">=&gt;</span> <span class="mf">1.5</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="verify">
<h3>verify<a class="headerlink" href="#verify" title="Permalink to this headline">¶</a></h3>
<p>Set to true to enable SSL certificate validation (the default), false to disable SSL certificate validation, or supply
the path to a CA bundle to enable verification using a custom certificate.</p>
</div>
<div class="section" id="proxy">
<h3>proxy<a class="headerlink" href="#proxy" title="Permalink to this headline">¶</a></h3>
<p>The <cite>proxy</cite> option is used to specify an HTTP proxy (e.g. <cite>http://username:password&#64;192.168.16.1:10</cite>).</p>
</div>
<div class="section" id="debug">
<h3>debug<a class="headerlink" href="#debug" title="Permalink to this headline">¶</a></h3>
<p>The <cite>debug</cite> option is used to output all data sent over the wire using the <cite>LogPlugin</cite> if it's available, or cURL
verbose output otherwise.</p>
</div>
<div class="section" id="stream">
<h3>stream<a class="headerlink" href="#stream" title="Permalink to this headline">¶</a></h3>
<p>When using a static client, you can set the <cite>stream</cite> option to true to return a <cite>GuzzleStreamStream</cite> object that can
be used to pull data from a stream as needed (rather than have cURL download the entire contents of a response to a
stream all at once).</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$stream</span> <span class="o">=</span> <span class="nx">Guzzle</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://guzzlephp.org&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;stream&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">));</span>
<span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">feof</span><span class="p">())</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">readLine</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="sending-requests">
<h2>Sending requests<a class="headerlink" href="#sending-requests" title="Permalink to this headline">¶</a></h2>
<p>Requests can be sent by calling the <tt class="docutils literal"><span class="pre">send()</span></tt> method of a Request object, but you can also send requests using the
<tt class="docutils literal"><span class="pre">send()</span></tt> method of a Client.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://www.amazon.com&#39;</span><span class="p">);</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="sending-requests-in-parallel">
<h3>Sending requests in parallel<a class="headerlink" href="#sending-requests-in-parallel" title="Permalink to this headline">¶</a></h3>
<p>The Client's <tt class="docutils literal"><span class="pre">send()</span></tt> method accept a single <tt class="docutils literal"><span class="pre">Guzzle\Http\Message\RequestInterface</span></tt> object or an array of
RequestInterface objects. When an array is specified, the requests will be sent in parallel.</p>
<p>Sending many HTTP requests serially (one at a time) can cause an unnecessary delay in a script's execution. Each
request must complete before a subsequent request can be sent. By sending requests in parallel, a pool of HTTP
requests can complete at the speed of the slowest request in the pool, significantly reducing the amount of time
needed to execute multiple HTTP requests. Guzzle provides a wrapper for the curl_multi functions in PHP.</p>
<p>Here's an example of sending three requests in parallel using a client object:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Common\Exception\MultiTransferException</span><span class="p">;</span>

<span class="k">try</span> <span class="p">{</span>
    <span class="nv">$responses</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
        <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://www.google.com/&#39;</span><span class="p">),</span>
        <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">head</span><span class="p">(</span><span class="s1">&#39;http://www.google.com/&#39;</span><span class="p">),</span>
        <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;https://www.github.com/&#39;</span><span class="p">)</span>
    <span class="p">));</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">MultiTransferException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>

    <span class="k">echo</span> <span class="s2">&quot;The following exceptions were encountered:</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="k">foreach</span> <span class="p">(</span><span class="nv">$e</span> <span class="k">as</span> <span class="nv">$exception</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">echo</span> <span class="nv">$exception</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">()</span> <span class="o">.</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">echo</span> <span class="s2">&quot;The following requests failed:</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="k">foreach</span> <span class="p">(</span><span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getFailedRequests</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$request</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">echo</span> <span class="nv">$request</span> <span class="o">.</span> <span class="s2">&quot;</span><span class="se">\n\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">echo</span> <span class="s2">&quot;The following requests succeeded:</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="k">foreach</span> <span class="p">(</span><span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getSuccessfulRequests</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$request</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">echo</span> <span class="nv">$request</span> <span class="o">.</span> <span class="s2">&quot;</span><span class="se">\n\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If the requests succeed, an array of <tt class="docutils literal"><span class="pre">Guzzle\Http\Message\Response</span></tt> objects are returned. A single request failure
will not cause the entire pool of requests to fail. Any exceptions thrown while transferring a pool of requests will
be aggregated into a <tt class="docutils literal"><span class="pre">Guzzle\Common\Exception\MultiTransferException</span></tt> exception.</p>
</div>
</div>
<div class="section" id="plugins-and-events">
<h2>Plugins and events<a class="headerlink" href="#plugins-and-events" title="Permalink to this headline">¶</a></h2>
<p>Guzzle provides easy to use request plugins that add behavior to requests based on signal slot event notifications
powered by the
<a class="reference external" href="http://symfony.com/doc/2.0/components/event_dispatcher/introduction.html">Symfony2 Event Dispatcher component</a>. Any
event listener or subscriber attached to a Client object will automatically be attached to each request created by the
client.</p>
<div class="section" id="using-the-same-cookie-session-for-each-request">
<h3>Using the same cookie session for each request<a class="headerlink" href="#using-the-same-cookie-session-for-each-request" title="Permalink to this headline">¶</a></h3>
<p>Attach a <tt class="docutils literal"><span class="pre">Guzzle\Plugin\Cookie\CookiePlugin</span></tt> to a client which will in turn add support for cookies to every request
created by a client, and each request will use the same cookie session:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Plugin\Cookie\CookiePlugin</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar</span><span class="p">;</span>

<span class="c1">// Create a new cookie plugin</span>
<span class="nv">$cookiePlugin</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">CookiePlugin</span><span class="p">(</span><span class="k">new</span> <span class="nx">ArrayCookieJar</span><span class="p">());</span>

<span class="c1">// Add the cookie plugin to the client</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">addSubscriber</span><span class="p">(</span><span class="nv">$cookiePlugin</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="events-emitted-from-a-client">
<span id="client-events"></span><h3>Events emitted from a client<a class="headerlink" href="#events-emitted-from-a-client" title="Permalink to this headline">¶</a></h3>
<p>A <tt class="docutils literal"><span class="pre">Guzzle\Http\Client</span></tt> object emits the following events:</p>
<table class="table table-bordered">
<colgroup>
<col width="26%" />
<col width="38%" />
<col width="36%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Event name</th>
<th class="head">Description</th>
<th class="head">Event data</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>client.create_request</td>
<td>Called when a client creates a request</td>
<td><ul class="first last simple">
<li>client: The client</li>
<li>request: The created request</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="highlight-php"><div class="highlight"><pre><span class="k">use</span> <span class="nx">Guzzle\Common\Event</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Guzzle\Http\Client</span><span class="p">;</span>

<span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Client</span><span class="p">();</span>

<span class="c1">// Add a listener that will echo out requests as they are created</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getEventDispatcher</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">addListener</span><span class="p">(</span><span class="s1">&#39;client.create_request&#39;</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="nx">Event</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="s1">&#39;Client object: &#39;</span> <span class="o">.</span> <span class="nb">spl_object_hash</span><span class="p">(</span><span class="nv">$e</span><span class="p">[</span><span class="s1">&#39;client&#39;</span><span class="p">])</span> <span class="o">.</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
    <span class="k">echo</span> <span class="s2">&quot;Request object: </span><span class="si">{</span><span class="nv">$e</span><span class="p">[</span><span class="s1">&#39;request&#39;</span><span class="p">]</span><span class="si">}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
<span class="p">});</span>
</pre></div>
</div>
</div>
</div>
</div>


            </div>
          <div class="clearfix"></div>
        </div>
        
    <div class="footer-links">
      <ul class="breadcrumb pull-right">
        <li>
          <a href="../getting-started/installation.html" title="previous chapter (use the left arrow)">&larr; Installation</a>
          <span class="divider">/</span>
          
        </li>
          <li><a href="request.html" title="next chapter (use the right arrow)">Using Request objects &rarr;</a></li>
      </ul>
    </div>
  
        <div class="container comment-container">
          
<div id="comments">
  <div id="disqus_thread"></div>
  <script type="text/javascript">
    var disqus_shortname = 'guzzle';
    var disqus_identifier = 'http-client/client';
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
  </script>
  <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>

        </div>
        

    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="request.html" title="Using Request objects"
             >next</a> |</li>
        <li class="right" >
          <a href="../getting-started/installation.html" title="Installation"
             >previous</a> |</li>
        <li><a href="../index.html">Guzzle</a> &raquo;</li>
          <li><a href="../docs.html" >Guzzle Documentation</a> &raquo;</li> 
      </ul>
    </div>
  <div class="footer container">
    &copy; Copyright 2012, Michael Dowling. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
  </div>
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-22752917-1']);
      _gaq.push(['_trackPageview']);
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>

  
    
      <script type="text/javascript">
        $(document).keydown(function(e){
          if (e.keyCode == 37) {
            
            window.location = '../getting-started/installation.html';
            return false;
            
          } else if (e.keyCode == 39) {
            
            window.location = 'request.html';
            return false;
            
          }
        });
      </script>
    
  
  </body>
</html>
Return current item: Guzzle