Location: PHPKode > projects > Guzzle > guzzle.github.com-master/http-client/http-redirects.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>HTTP redirects &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="Plugin system overview" href="../plugins/plugins-overview.html" />
    <link rel="prev" title="Request and response bodies" href="entity-bodies.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="../plugins/plugins-overview.html" title="Plugin system overview"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="entity-bodies.html" title="Request and response bodies"
             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="entity-bodies.html" title="previous chapter (use the left arrow)">&larr; Request and response bodies</a>
          <span class="divider">/</span>
          
        </li>
          <li><a href="../plugins/plugins-overview.html" title="next chapter (use the right arrow)">Plugin system overview &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="#">HTTP redirects</a><ul>
<li><a class="reference internal" href="#redirect-history">Redirect history</a></li>
<li><a class="reference internal" href="#disabling-redirects">Disabling redirects</a></li>
<li><a class="reference internal" href="#redirects-and-non-repeatable-streams">Redirects and non-repeatable streams</a></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="http-redirects">
<h1>HTTP redirects<a class="headerlink" href="#http-redirects" title="Permalink to this headline">¶</a></h1>
<p>By default, Guzzle will automatically follow redirects using the non-RFC compliant implementation used by most web
browsers. This means that redirects for POST requests are followed by a GET request. You can force RFC compliance by
enabling the strict mode on a request's parameter object:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="c1">// Set per request</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="nv">$request</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;redirect.strict&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>

<span class="c1">// You can set globally on a client so all requests use strict redirects</span>
<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">set</span><span class="p">(</span><span class="s1">&#39;request.params&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;redirect.strict&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span>
<span class="p">));</span>
</pre></div>
</div>
<p>By default, Guzzle will redirect up to 5 times before throwing a <tt class="docutils literal"><span class="pre">Guzzle\Http\Exception\TooManyRedirectsException</span></tt>.
You can raise or lower this value using the <tt class="docutils literal"><span class="pre">redirect.max</span></tt> parameter of a request object:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="nv">$request</span><span class="o">-&gt;</span><span class="na">getParams</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;redirect.max&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="redirect-history">
<h2>Redirect history<a class="headerlink" href="#redirect-history" title="Permalink to this headline">¶</a></h2>
<p>You can get the number of redirects of a request using the resulting response object's <tt class="docutils literal"><span class="pre">getRedirectCount()</span></tt> method.
Similar to cURL's <tt class="docutils literal"><span class="pre">effective_url</span></tt> property, Guzzle provides the effective URL, or the last redirect URL that returned
the request, in a response's <tt class="docutils literal"><span class="pre">getEffectiveUrl()</span></tt> method.</p>
<p>When testing or debugging, it is often useful to see a history of redirects for a particular request. This can be
acheived using the HistoryPlugin.</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;/&#39;</span><span class="p">);</span>
<span class="nv">$history</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Guzzle\Plugin\History\HistoryPlugin</span><span class="p">();</span>
<span class="nv">$request</span><span class="o">-&gt;</span><span class="na">addSubscriber</span><span class="p">(</span><span class="nv">$history</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">// Get the last redirect URL or the URL of the request that received</span>
<span class="c1">// this response</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getEffectiveUrl</span><span class="p">();</span>

<span class="c1">// Get the number of redirects</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getRedirectCount</span><span class="p">();</span>

<span class="c1">// Iterate over each sent request and response</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$history</span><span class="o">-&gt;</span><span class="na">getAll</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$transaction</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// Request object</span>
    <span class="k">echo</span> <span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;request&#39;</span><span class="p">]</span><span class="o">-&gt;</span><span class="na">getUrl</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="c1">// Response object</span>
    <span class="k">echo</span> <span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;response&#39;</span><span class="p">]</span><span class="o">-&gt;</span><span class="na">getEffectiveUrl</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="c1">// Or, simply cast the HistoryPlugin to a string to view each request and response</span>
<span class="k">echo</span> <span class="nv">$history</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="section" id="disabling-redirects">
<h2>Disabling redirects<a class="headerlink" href="#disabling-redirects" title="Permalink to this headline">¶</a></h2>
<p>You can disable redirects on a client by passing a configuration option in the client's constructor:</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">Client</span><span class="p">(</span><span class="k">null</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;redirect.disable&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">));</span>
</pre></div>
</div>
<p>You can also disable redirects per request:</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;allow_redirects&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="redirects-and-non-repeatable-streams">
<h2>Redirects and non-repeatable streams<a class="headerlink" href="#redirects-and-non-repeatable-streams" title="Permalink to this headline">¶</a></h2>
<p>If you are redirected when sending data from a non-repeatable stream and some of the data has been read off of the
stream, then you will get a <tt class="docutils literal"><span class="pre">Guzzle\Http\Exception\CouldNotRewindStreamException</span></tt>. You can get around this error by
adding a custom rewind method to the entity body object being sent in the request.</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">post</span><span class="p">(</span>
    <span class="s1">&#39;http://httpbin.com/redirect/2&#39;</span><span class="p">,</span>
    <span class="k">null</span><span class="p">,</span>
    <span class="nb">fopen</span><span class="p">(</span><span class="s1">&#39;http://httpbin.com/get&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="p">);</span>

<span class="c1">// Add a custom function that can be used to rewind the stream</span>
<span class="c1">// (reopen in this example)</span>
<span class="nv">$request</span><span class="o">-&gt;</span><span class="na">getBody</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">setRewindFunction</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="nv">$body</span><span class="p">)</span> <span class="p">{</span>
    <span class="nv">$body</span><span class="o">-&gt;</span><span class="na">setStream</span><span class="p">(</span><span class="nb">fopen</span><span class="p">(</span><span class="s1">&#39;http://httpbin.com/get&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">));</span>
    <span class="k">return</span> <span class="k">true</span><span class="p">;</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>
</pre></div>
</div>
</div>
</div>


            </div>
          <div class="clearfix"></div>
        </div>
        
    <div class="footer-links">
      <ul class="breadcrumb pull-right">
        <li>
          <a href="entity-bodies.html" title="previous chapter (use the left arrow)">&larr; Request and response bodies</a>
          <span class="divider">/</span>
          
        </li>
          <li><a href="../plugins/plugins-overview.html" title="next chapter (use the right arrow)">Plugin system overview &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/http-redirects';
    (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="../plugins/plugins-overview.html" title="Plugin system overview"
             >next</a> |</li>
        <li class="right" >
          <a href="entity-bodies.html" title="Request and response bodies"
             >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 = 'entity-bodies.html';
            return false;
            
          } else if (e.keyCode == 39) {
            
            window.location = '../plugins/plugins-overview.html';
            return false;
            
          }
        });
      </script>
    
  
  </body>
</html>
Return current item: Guzzle