Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation about automatic handling of a redirect response. #239

Merged
merged 3 commits into from
Mar 30, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions en/core-utility-libraries/httpsocket.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ HTTP methods.
'User-Agent' => 'CakePHP' 'User-Agent' => 'CakePHP'
), ),
'raw' => null, 'raw' => null,
'redirect' => false,
'cookies' => array() 'cookies' => array()
); );


Expand Down Expand Up @@ -145,8 +146,10 @@ The ``HttpResponse`` also exposes the following methods:


* ``body()`` returns the body * ``body()`` returns the body
* ``isOk()`` returns if code is 200; * ``isOk()`` returns if code is 200;
* ``isRedirect()`` returns if code is 301, 302, 303 or 307 and the *Location* header is set.
* ``getHeader()`` allows you to fetch headers, see the next section. * ``getHeader()`` allows you to fetch headers, see the next section.



Getting headers from a response Getting headers from a response
------------------------------- -------------------------------


Expand Down Expand Up @@ -179,6 +182,28 @@ You could fetch the above headers by calling::


Headers can be fetched case-insensitively. Headers can be fetched case-insensitively.


Automaticaly handling a redirect response
----------------------------------------

When the response has a valid redirect status code (see ``HttpResponse::isRedirect``),
an extra request can be automaticaly done according to the received *Location* header::

<?php
App::uses('HttpSocket', 'Network/Http');

$HttpSocket = new HttpSocket();
$response = $HttpSocket->get('http://example.com/redirecting_url', array(), array('redirect' => true));


The *redirect* option can take the following values

* **true** : all redirecting responses will fire a consequent new request
* **integer** : the setted value is the maximum number of redirections allowed (after reaching it, the *redirect* value is condidered as **false**)
* **false** (default) : no consequent request will be fired

The returned ``$response`` will be the final one, according to the settings.


Creating a custom response class Creating a custom response class
-------------------------------- --------------------------------


Expand Down