cookie-plugin.rst 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. =============
  2. Cookie plugin
  3. =============
  4. Some web services require a Cookie in order to maintain a session. The ``Guzzle\Plugin\Cookie\CookiePlugin`` will add
  5. cookies to requests and parse cookies from responses using a CookieJar object:
  6. .. code-block:: php
  7. use Guzzle\Http\Client;
  8. use Guzzle\Plugin\Cookie\CookiePlugin;
  9. use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
  10. $cookiePlugin = new CookiePlugin(new ArrayCookieJar());
  11. // Add the cookie plugin to a client
  12. $client = new Client('http://www.test.com/');
  13. $client->addSubscriber($cookiePlugin);
  14. // Send the request with no cookies and parse the returned cookies
  15. $client->get('http://www.yahoo.com/')->send();
  16. // Send the request again, noticing that cookies are being sent
  17. $request = $client->get('http://www.yahoo.com/');
  18. $request->send();
  19. echo $request;
  20. You can disable cookies per-request by setting the ``cookies.disable`` value to true on a request's params object.
  21. .. code-block:: php
  22. $request->getParams()->set('cookies.disable', true);