![]() |
|
sfWebBrowserPlugin - 0.9.6Standalone HTTP client |
|
![]() |
188
users
Sign-in
to change your status |
The sfWebBrowserPlugin proposes an HTTP client capable of making web requests. The interface is similar to that of sfTestBrowser. |
| Name | Status | |
|---|---|---|
|
|
lead | moc.tcejorp-ynofmys <<ta>> ottoninaz.siocnarf |
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.
| Version | License | API | Released |
|---|---|---|---|
| 1.1.2stable | MIT license | 1.1.2stable | 12/05/2009 |
| 1.1.1stable | MIT license | 1.1.1stable | 22/04/2009 |
| 1.1.0stable | MIT license | 1.1.0stable | 23/09/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 1.1.2stable | MIT license | 1.1.2stable | 12/05/2009 |
| 1.1.1stable | MIT license | 1.1.1stable | 22/04/2009 |
| 1.1.0stable | MIT license | 1.1.0stable | 23/09/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 1.1.2stable | MIT license | 1.1.2stable | 12/05/2009 |
| 1.1.1stable | MIT license | 1.1.1stable | 22/04/2009 |
| 1.1.0stable | MIT license | 1.1.0stable | 23/09/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 1.1.2stable | MIT license | 1.1.2stable | 12/05/2009 |
| 1.1.1stable | MIT license | 1.1.1stable | 22/04/2009 |
| 1.1.0stable | MIT license | 1.1.0stable | 23/09/2008 |
| Version | License | API | Released |
|---|---|---|---|
| 1.1.2stable | MIT license | 1.1.2stable | 12/05/2009 |
| 1.1.1stable | MIT license | 1.1.1stable | 22/04/2009 |
| 1.1.0stable | MIT license | 1.1.0stable | 23/09/2008 |
| 1.0.1stable | MIT license | 1.0.1stable | 27/03/2007 |
| 1.0.0stable | MIT license | 1.0.0stable | 08/03/2007 |
| 0.9.6beta | MIT license | 0.9.6beta | 22/02/2007 |
| 0.9.5beta | MIT license | 0.9.5beta | 21/02/2007 |
| 0.9.4beta | MIT license | 0.9.4beta | 16/02/2007 |
| 0.9.3beta | MIT license | 0.9.3beta | 16/02/2007 |
| 0.9.2beta | MIT license | 0.9.2beta | 09/02/2007 |
| 0.9.1beta | MIT license | 0.9.1beta | 08/02/2007 |
| 0.9.0beta | MIT license | 0.9.0beta | 08/02/2007 |
sfCurlAdaptersfCurlAdaptersfCurlAdapter (based on a patch by adoanhuu)getResponseXMLsfCurlAdaptersfCurlAdapter: Added new options: 'cookies', 'cookies_dir', 'cookies_file', 'verbose', 'verbose_log'sfCurlAdapter: Increased speed by Moving some initialization from call() to the constructersfFopenAdapter error handlersfSocketsAdaptersfSocketsAdapterThe 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 browser can use various adapters to perform the requests:
sfFopenAdapter (default): Uses fopen() to fetch pages. fopen() can take an URL as a parameter provided that PHP is compiled with sockets support, and allow_url_fopen is defined to true in php.ini. This is the case in most PHP distributions, so the default adapter should work in almost every platform. On the other hand, the compatibility has a cost: this adapter is slow.
sfCurlAdapter: Uses Curl to fetch pages. This adapter is a lot faster than sfFopenAdapter, however PHP must be compiled with the with-curl option, and the curl extension must be enabled in php.ini (which is rarely the case by default) for it to work.
You can specify an adapter when you create a new browser object, as follows:
// use default adapter, i.e. sfFopenAdapter
$b = new sfWebBrowser(array());
// use sfCurlAdapter
$b = new sfWebBrowser(array(), 'sfCurlAdapter');
sfWebBrowser distiguishes to types of error: adapter errors and response errors. Thus, sfWebBrowser calls should be run this way :
$b = new sfWebBrowser();
try
{
if (!$b->get($url)->responseIsError())
{
// Successful response (eg. 200, 201, etc)
}
else
{
// Error response (eg. 404, 500, etc)
}
}
catch (Exception $e)
{
// Adapter error (eg. Host not found)
}
Besides, you should always remember that the response contents may contain incorrect code. Consider it as 'tainted', and therefore always use the escaping when outputting it to a template.
// In the action
$this->title = (string) $b->getResponseXml()->body->h1
// In the template
<?php echo $title // dangerous ?>
<?php echo $sf_data->get('title') // correct ?>
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
If allow_url_fopen is not defined to true and if the curl extension cannot be activated in the php.ini file, the browser can't fetch pages. Workaround (todo): use a sockets adapter.
Cookies are not handled yet.
Caching is not supported yet.
sfCurlAdaptersfWebBrowser::responseIsError()sfWebBrowser::getResponseMessage()sfFopenAdapternew sfWebBrowser(array $headers, string $adapter_class, array $adapter_options) initializeRequestHeaders()new sfWebBrowser(array $headers, array $adapter_options)getResponseBody()getResponseXML()