Symfony之components--第二章--第二讲:BrowserKit组件的使用
宋凌龙
2023-12-01
BrowserKit Component
simulates(模拟)一个网络浏览器的习惯,允许你发送请求,点击链接和表单提交.
Basic Usage
创建一个客户
该组建只提供哦你一个抽象的客户段,并且不提供任何后台http层的任何操作.
创建自己的client,你必须继承一个抽象的Client类并且实现doRequest()方法.
这个方法接受一个请求并且应该返回一个响应.
namespace Acme;
use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Response;
class Client extends BaseClient
{
protected function doRequest($request)
{
// ... convert request into a response
return new Response($content, $status, $headers);
}
}
发送请求
用request()方法去发送一个http请求.前两个参数是http方法和请求的url:
use Acme\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');
共过request()方法返回的值是Crawler类的一个实例,允许以编程的方式访问和遍历html元素.
Clicking Links
Crawler对象可以模拟链接点击.首先,通过给selectLink()方法设置链接的文本内容,返回一个Link对象.
然后通过给click方法设置这个Link对象,执行需要的http get请求,从而模拟链接点击.
use Acme\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');
$link = $crawler->selectLink('Go elsewhere...')->link();
$client->click($link);
Submitting Forms
Crawler对象之能够选择表单.首相,通过selectButton()方法选择任何一个表单按钮.
然后,通过form()方法去选择这个按钮所属表单.
在选择了表单之后,填写他的数据并且通过submit()方法提交它.
use Acme\Client;
// make a real request to an external site
$client = new Client();
$crawler = $client->request('GET', 'https://github.com/login');
// select the form and fill in some values
$form = $crawler->selectButton('Log in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';
// submit that form
$crawler = $client->submit($form);
Cookies
retrieving(检索) cookies
Client通过一个CookieJar实现了暴露的cookies,允许你去存储和检索任何cookie当你在客户端
发送请求的时候.
use Acme\Client;
// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');
// Get the cookie Jar
$cookieJar = $client->getCookieJar();
// Get a cookie by name
$cookie = $cookieJar->get('name_of_the_cookie');
// Get cookie data
$name = $cookie->getName();
$value = $cookie->getValue();
$raw = $cookie->getRawValue();
$secure = $cookie->isSecure();
$isHttpOnly = $cookie->isHttpOnly();
$isExpired = $cookie->isExpired();
$expires = $cookie->getExpiresTime();
$path = $cookie->getPath();
$domain = $cookie->getDomain();
Looping Through Cookies
use Acme\Client;
// Make a request
$client = new Client();
$crawler = $client->request('GET', 'http://symfony.com');
// Get the cookie Jar
$cookieJar = $client->getCookieJar();
// Get array with all cookies
$cookies = $cookieJar->all();
foreach ($cookies as $cookie) {
// ...
}
// Get all values
$values = $cookieJar->allValues('http://symfony.com');
foreach ($values as $value) {
// ...
}
// Get all raw values
$rawValues = $cookieJar->allRawValues('http://symfony.com');
foreach ($rawValues as $rawValue) {
// ...
}
Setting Cookies
你可以之创建cookies并且添加他们到cookiejar,可以注入到客户端的构造函数:
use Acme\Client;
// create cookies and add to cookie jar
$cookieJar = new Cookie('flavor', 'chocolate', strtotime('+1 day'));
// create a client and set the cookies
$client = new Client(array(), array(), $cookieJar);
// ...
History
客户端存储所有你的请求,允许在你的历史浏览记录中返回或者前进
use Acme\Client;
// make a real request to an external site
$client = new Client();
$client->request('GET', 'http://symfony.com');
// select and click on a link
$link = $crawler->selectLink('Documentation')->link();
$client->click($link);
// go back to home page
$crawler = $client->back();
// go forward to documentation page
$crawler = $client->forward();
你可以删除客户端的历史,通过restart()方法.这也将删除所有的cookies:
use Acme\Client;
// make a real request to an external site
$client = new Client();
$client->request('GET', 'http://symfony.com');
// delete history
$client->restart();