php browserkit,BrowserKit组件

BrowserKit组件模拟浏览器行为,让你能够程序化地制造请求、对链接的点击以及表单提交。

安装 ¶

你可以通过下述两种方式安装:

然后,包容vendor/autoload.php文件,以开启Composer提供的自动加载机制。否则,你的程序将无法找到这个Symfony组件的类。

基本用法 ¶

创建Client ¶

本组件只提供抽象的client,并不提供任何可以用于HTTP层的后端。

要创建你自己的客户端,必须继承 Client 抽象类,然后实现其

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15namespace 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);

}

}

基于HTTP layer的一个简单的client实现,可以看看 Goutte。基于 HttpKernelInterface 的实现,看一下由 HttpKernel组件 提供的

制造请求 ¶

使用

1

2

3

4use Acme\Client;

$client = new Client();

$crawler = $client->request('GET', 'http://symfony.com');

由 request() 方法返回的值,是一个 DomCrawler组件,该组件能够程式化地访问和遍历HTML元素。

点击链接 ¶

Crawler 对象有能力模拟对链接的点击。首先,把链接的文本传入 selectLink() 方法,该方法返回一个 Link 对象。然后再把这个对象传入 click() 方法, 该方法负责把所需的HTTP GET请求模拟成链接点击:

1

2

3

4

5

6use Acme\Client;

$client = new Client();

$crawler = $client->request('GET', 'http://symfony.com');

$link = $crawler->selectLink('Go elsewhere...')->link();

$client->click($link);

提交表单 ¶

Crawler 对象有能力选择表单。首先,用 selectButton() 方法来选择任意表单的按钮。然后,使用 form() 来选择该按钮的归属表单。

选择了表单之后,填充表单数据,并使用 submit() 方法(利用所需的HTTP POST请求来提交表单内容) 来发送表单:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16use 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 ¶

取出Cookie ¶

The Client implementation exposes cookies (if any) through a

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26use 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名称取得cookie

$cookie = $cookieJar->get('name_of_the_cookie');

// Get cookie data

// 取得cookie数据

$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();

这些方法只返回尚未过期的cookie。

循环Cookie ¶

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31use 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

// 取得全部cookie的数组

$cookies = $cookieJar->all();

foreach ($cookies as $cookie) {

// ...

}

// Get all values

// 获取全部cookie的值

$values = $cookieJar->allValues('http://symfony.com');

foreach ($values as $value) {

// ...

}

// Get all raw values

// 获取全部原生的值

$rawValues = $cookieJar->allRawValues('http://symfony.com');

foreach ($rawValues as $rawValue) {

// ...

}

设置Cookie ¶

你也可以创建cookie并把它们装到cookie Jar中,以便将饼干罐注入到客户端的构造器中:

1

2

3

4

5

6

7

8

9use Acme\Client;

// create cookies and add to cookie jar

// 创建cookie并添加到cookie Jar

$cookieJar = new Cookie('flavor', 'chocolate', strtotime('+1 day'));

// create a client and set the cookies

// 创建client并设置cookie

$client = new Client(array(), array(), $cookieJar);

历史 ¶

客户端存有你的全部请求,允许你在历史记录中回退和前进:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19use 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:

1

2

3

4

5

6

7

8

9

10use Acme\Client;

// make a real request to an external site

// 制造一个对外部网站的真实请求

$client = new Client();

$client->request('GET', 'http://symfony.com');

// delete history

// 删除历史记录

$client->restart();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值