Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[BrowserKit] Add support for HttpClient#30602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 2 commits intosymfony:masterfromfabpot:http-with-browserkit
Mar 23, 2019

Conversation

@fabpot
Copy link
Member

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketspart of#30502
LicenseMIT
Doc PRnot yet

When combining the power of the new HttpClient component with the BrowserKit and Mime components, we can makes something really powerful... a full/better/awesome replacement forhttps://github.com/FriendsOfPHP/Goutte.

So, this PR is about integrating the HttpClient component with BrowserKit to give users a high-level interface to ease usages in the most common use cases.

Scraping websites can be done like this:

useSymfony\Component\BrowserKit\HttpBrowser;useSymfony\Component\HttpClient\HttpClient;$client = HttpClient::create();$browser =newHttpBrowser($client);$browser->request('GET','https://example.com/');$browser->clickLink('Log In');$browser->submitForm('Sign In', ['username' =>'me','password' =>'pass']);$browser->clickLink('Subscriptions')->filter('table tr:nth-child(2) td:nth-child(2)')->each(function ($node) {echotrim($node->text())."\n";});

And voilà! Nice, isn't?

Want to add HTTP cache? Sure:

useSymfony\Component\HttpKernel\HttpCache\Store;$client = HttpClient::create();$store =newStore(sys_get_temp_dir().'/http-cache-store');$browser =newHttpBrowser($client,$store);// ...

Want logging and debugging of HTTP Cache? Yep:

usePsr\Log\AbstractLogger;class EchoLoggerextends AbstractLogger{publicfunctionlog($level,$message,array$context = [])    {echo$message."\n";    }}$browser =newHttpBrowser($client,$store,newEchoLogger());

The first time you run your code, you will get an output similar to:

Request: GET https://twig.symfony.com/Response: 200 https://twig.symfony.com/Cache: GET /: miss, storeRequest: GET https://twig.symfony.com/doc/2.x/Response: 200 https://twig.symfony.com/doc/2.x/Cache: GET /doc/2.x/: miss, store

But then:

Cache: GET /: freshCache: GET /doc/2.x/: fresh

Limit is the sky here as you get the full power of all the Symfony ecosystem.

Under the hood, these examples leverage HttpFoundation, HttpKernel (with HttpCache),
DomCrawler, BrowserKit, CssSelector, HttpClient, Mime, ...

Excited?

P.S. : Tests need to wait for the HttpClient Mock class to land into master.

EmmanuelVella, ro0NL, yhammououali, Pierstoval, sdaoudi, and1truong, ianrodrigues, pcabreus, and ternel reacted with thumbs up emojinicolas-grekas, HeahDude, OskarStark, nicholasruunu, stloyd, yceruto, yhammououali, and Pierstoval reacted with hooray emojiOskarStark, julienbourdeau, yceruto, Pierstoval, jkbmaj, and1truong, and PReimers reacted with rocket emoji
@fabpotfabpotforce-pushed thehttp-with-browserkit branch 2 times, most recently from8bde4ee toa15b707CompareMarch 19, 2019 12:58
Copy link
Contributor

@ro0NLro0NL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Limit is the sky

You mean "Sky's the limit" i suppose :D

PReimers reacted with thumbs up emoji
@nicolas-grekasnicolas-grekas changed the title[HttpClient] Add support for BrowserKit[BrowserKit] Add support for HttpClientMar 21, 2019
fabpot added a commit that referenced this pull requestMar 21, 2019
…HttpClientInterface (fabpot)This PR was merged into the 4.3-dev branch.Discussion----------[HttpKernel] add RealHttpKernel: handle requests with HttpClientInterface| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -This commit is directly extracted from#30602 by@fabpotCommits-------b579b02 [HttpKernel] add RealHttpKernel: handle requests with HttpClientInterface
@nicolas-grekasnicolas-grekasforce-pushed thehttp-with-browserkit branch 3 times, most recently from380a517 toa25fe3fCompareMarch 22, 2019 10:48
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Green in a minute, with tests provided by@ktherage. The caching part is now separated into#30629, and logging has been removed. It should be reintroduced inHttpClient directly.

class HttpBrowserextends AbstractBrowser
{
private$client;
private$httpKernelBrowser;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

unused

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

removed thanks

@fabpot
Copy link
MemberAuthor

Thank you THERAGE Kévin.

ktherage reacted with thumbs up emoji

@fabpotfabpot merged commitb5b2a25 intosymfony:masterMar 23, 2019
fabpot added a commit that referenced this pull requestMar 23, 2019
…GE Kévin)This PR was merged into the 4.3-dev branch.Discussion----------[BrowserKit] Add support for HttpClient| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | part of#30502| License       | MIT| Doc PR        | not yetWhen combining the power of the new HttpClient component with the BrowserKit and Mime components, we can makes something really powerful... a full/better/awesome replacement forhttps://github.com/FriendsOfPHP/Goutte.So, this PR is about integrating the HttpClient component with BrowserKit to give users a high-level interface to ease usages in the most common use cases.Scraping websites can be done like this:```phpuse Symfony\Component\BrowserKit\HttpBrowser;use Symfony\Component\HttpClient\HttpClient;$client = HttpClient::create();$browser = new HttpBrowser($client);$browser->request('GET', 'https://example.com/');$browser->clickLink('Log In');$browser->submitForm('Sign In', ['username' => 'me', 'password' => 'pass']);$browser->clickLink('Subscriptions')->filter('table tr:nth-child(2) td:nth-child(2)')->each(function ($node) {    echo trim($node->text())."\n";});```And voilà! Nice, isn't?Want to add HTTP cache? Sure:```phpuse Symfony\Component\HttpKernel\HttpCache\Store;$client = HttpClient::create();$store = new Store(sys_get_temp_dir().'/http-cache-store');$browser = new HttpBrowser($client, $store);// ...```Want logging and debugging of HTTP Cache? Yep:```phpuse Psr\Log\AbstractLogger;class EchoLogger extends AbstractLogger{    public function log($level, $message, array $context = [])    {        echo $message."\n";    }}$browser = new HttpBrowser($client, $store, new EchoLogger());```The first time you run your code, you will get an output similar to:```Request: GEThttps://twig.symfony.com/Response: 200https://twig.symfony.com/Cache: GET /: miss, storeRequest: GEThttps://twig.symfony.com/doc/2.x/Response: 200https://twig.symfony.com/doc/2.x/Cache: GET /doc/2.x/: miss, store```But then:```Cache: GET /: freshCache: GET /doc/2.x/: fresh```Limit is the sky here as you get the full power of all the Symfony ecosystem.Under the hood, these examples leverage HttpFoundation, HttpKernel (with HttpCache),DomCrawler, BrowserKit, CssSelector, HttpClient, Mime, ...Excited?P.S. : Tests need to wait for the HttpClient Mock class to land into master.Commits-------b5b2a25 Add tests for HttpBrowserdd55845 [BrowserKit] added support for HttpClient
fabpot added a commit that referenced this pull requestMar 23, 2019
This PR was merged into the 4.3-dev branch.Discussion----------[HttpClient] added CachingHttpClient| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -The proposed `CachingHttpClient` uses `HttpCache` from the HttpKernel component to provide an HTTP-compliant cache.If this is accepted, it could replace the corresponding part in#30602Commits-------dae5686 [HttpClient] added CachingHttpClient
@nicolas-grekasnicolas-grekas modified the milestones:next,4.3Apr 30, 2019
@fabpotfabpot mentioned this pull requestMay 9, 2019
@fabpotfabpot deleted the http-with-browserkit branchSeptember 12, 2019 12:56
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestJan 2, 2020
This PR was merged into the 4.3 branch.Discussion----------Replace references to Goutte with HttpBrowserWith the introduction of [HttpBrowser](symfony/symfony#30602) there's no need for Goutte any more, but the docs currently mention both, which is a little bit confusing. Also, the current version of Goutte actuality [extends HttpBrowser](https://github.com/FriendsOfPHP/Goutte/blob/v4.0.0/Goutte/Client.php), but doesn't add any functionality to it. It seems to me that Goutte is being completely replaced with HttpBrowser so there's no point in mentioning it in the docs any more.Commits-------50898ae Replace references to Goutte with HttpBrowser
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof left review comments

@chalasrchalasrchalasr approved these changes

+3 more reviewers

@ro0NLro0NLro0NL left review comments

@thewilkybarkidthewilkybarkidthewilkybarkid left review comments

@nlubischnlubischnlubisch requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

9 participants

@fabpot@nicolas-grekas@stof@ro0NL@thewilkybarkid@nlubisch@chalasr@carsonbot@ktherage

[8]ページ先頭

©2009-2025 Movatter.jp