Cookies

Zend_Http_Client will accept manually specified cookie name-value pairs via its setCookie method, but by default will not automatically retain response cookies and resend them in subsequent requests. To have it do so, simply call setCookieJar with no parameters. This will cause an instance of the default cookie handling class, Zend_Http_CookieJar, to be implicitly instantiated.

If you need access to cookie data for something other than propagating it to subsequent requests, there are a few ways to do so. Cookies can be accessed individually via the cookie jar’s getCookie method, the required parameters for which are a URI and a cookie name.

<?php
$cookie = $client->getCookieJar()->getCookie(
'http://localhost.example/',
'cookiename'
);
?>

Note that the URI includes a scheme (http: //), a domain (localhost. example), and a path (/). A single cookie jar instance can store cookie data for multiple domains and multiple paths on the same domain. In cases where the latter capability is not used, the path / can be specified so that all cookies set on the specified domain are available to all paths under that domain. The getMatchingCookies method of Zend_Http_CookieJar allows cookies to be accessed collectively based on these criteria and returns an array of Zend_Http_Cookie objects by default. See below for examples.

<?php
//All cookies for the domain localhost.example
$cookies = $cookiejar->getMatchingCookies(
'http://localhost.example/'
);
// All cookies for the domain localhost.example with a path or
// subpath of /some/path
$cookies = $cookiejar->getMatchingCookies(
'http://localhost.example/some/path'
);

// All non-session cookies for the domain localhost.example $cookies = $cookiejar->getMatchingCookies(
'http://localhost.example/', false
);
?>

Alternatively, getAUCookies can be used to access all cookies contained in the cookie jar instance. When a cookie jar is only used to store cookies for a single domain, getAUCookies offers a more concise method than getMatchingCookies to retrieve all cookies for that domain. Like getMatchingCookies, getAUCookies also returns an array of Zend_Http_Cookie objects by default.


© Zend_Http_Client — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 512 | Rating: 0.0/0
Total comments: 0
avatar