Copyright (c) 2004-2006 Francois Zaninotto
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sfWebBrowser plugin
The sfWebBrowserPlugin proposes an HTTP client capable of making web requests. The interface is similar to that of sfTestBrowser.
Possible uses
- Querying a Web service
- Monitoring a Website
- Mashup of content from several websites
- Aggregation of RSS feeds
- Proxy to another server
- Cross-domain AJAX interactions
- API to foreign websites
- ...
Contents
This plugin contains one single class: sfWebBrowser. Unit tests are available in the SVN repository, to be placed in a symfony application's test/ directory.
Features
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'));
Requests in POST mode are also supported.
$b->post('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.
Installation
Known limitations
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.
Changelog
2007-02-16 | 0.9.4
- francois: refactored the browser to make it multi-adapter
- francois: BC break constructor signature changed :
new sfWebBrowser(array $headers, string $adapter_class, array $adapter_options)
- francois: fixed notice when trying to retrieve inexistent header
- francois: fixed header case normalization
- francois: transformed setResponseXXX() methods to public
- francois: fixed caps in
initializeRequestHeaders()
- francois: fixed unit test #40
2007-02-16 | 0.9.3
- tristan: Added support for gzip and deflate.
- tristan: Possibility to pass default request headers to sfWebBrowser's constructor
- tristan: "Accept-Encoding" header is automatically set depending on PHP capabilities
- tristan: Fixed problems with request and response headers case
- tristan: Renamed "browser options" to "adapter options" (http://www.symfony-project.com/forum/index.php/m/21635/)
- tristan: BC break constructor signature changed :
new sfWebBrowser(array $headers, array $adapter_options)
- tristan: Unit tested POST requests
- tristan: Changed way httpd headers are stored internally
- tristan: Fixed small bug in
getResponseBody()
- francois: Fixed unit test for malformed headers
2007-02-09 | 0.9.2
- francois: Fixed notice with
getResponseXML()
2007-02-08 | 0.9.1
- francois: Fixed notice with some headers
- francois: Added license and copyright
2007-02-08 | 0.9.0
- francois: Initial release