![]() |
|
sfWebBrowserPlugin - 0.9.5Standalone HTTP client |
|
The sfWebBrowserPlugin proposes an HTTP client capable of making web requests. The interface is similar to that of sfTestBrowser.
This plugin contains three classes: sfWebBrowser, sfFopenAdapter, andsfCurlAdapter. Unit tests are available in the SVN repository, to be placed in a symfony application's test/ directory.
The sfWebBrowser class makes web requests based on a URI:
$b = new sfWebBrowser();
$b->get('http://www.example.com/');
$res = $b->getResponseText();
The usual methods of the sfTestBrowser also work there, with the fluid interface.
// Inline
$b->get('http://www.example.com/')->get('http://www.google.com/')->back()->reload();
// More readable
$b->get('http://www.example.com/')
*>get('http://www.google.com/')
*>back()
*>reload();
The browser accepts absolute and relative URIs
$b->get('http://www.example.com/test.html');
$b->get('test.html');
The get() method accepts parameters either as a query string, or as an array.
$b->get('http://www.example.com/test.php?foo=bar');
$b->get('http://www.example.com/test.php', array('foo', 'bar'));
POST, PUT and DELETE requests are also supported.
$b->post('http://www.example.com/test.php', array('foo', 'bar'));
$b->put('http://www.example.com/test.php', array('foo', 'bar'));
$b->delete('http://www.example.com/test.php', array('foo', 'bar'));
You can access the response in various formats, at your convenience:
$myString = $b->getResponseText();
$myString = $b->getResponseBody(); // drop the <head> part
$myDomDocument = $b->getResponseDom();
$myDomCssSelector = $b->getResponseDomCssSelector();
$mySimpleXml = $b->getResponseXml();
You can also interact with the response with the setFields() and click() methods.
$b->get('http://www.example.com/login')
*>setField('user', 'foobar')
*>setField('password', 'barbaz')
*>click('submit');
The browser supports HTTP and HTTPS requests, proxies, redirects, and timeouts.
Gzip and deflate content-encoded response bodies are also supported, provided that you have the zlib extention enabled.
The default page-fetching method uses [fopen()]. However, you can use http://php.net/curl Curl if you prefer:
$b = new sfWebBrowser(array(), 'sfCurlAdapter');
// use $b in the same way as the examples above
To use Curl, make sure it is enabled in your php.ini file.
Install the plugin
$ symfony plugin-install http://plugins.symfony-project.com/sfWebBrowserPlugin
Clear the cache to enable the autoloading to find the new class
$ symfony cc
The web request is made via fopen(), so this will only work if PHP is compiled with sockets support and if allow_url_fopen is defined to true in the php.ini.
Cookies are not handled yet.
Caching is not supported yet.
new sfWebBrowser(array $headers, string $adapter_class, array $adapter_options) initializeRequestHeaders()new sfWebBrowser(array $headers, array $adapter_options)getResponseBody()getResponseXML()