• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

dweidner/laravel-goutte: Laravel Facade for Goutte, a simple PHP Web Scraper

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

dweidner/laravel-goutte

开源软件地址(OpenSource Url):

https://github.com/dweidner/laravel-goutte

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Laravel Facade for Goutte

This repository implements a simple ServiceProvider that makes a singleton instance of the Goutte client easily accessible via a Facade in Laravel. See @FriendsOfPHP/Goutte for more information about the PHP web scraper and its interfaces.

Installation using Composer

In your terminal application move to the root directory of your laravel project using the cd command and require the project as a dependency using composer.

$ cd ~/Sites/laravel-example-project
$ composer require weidner/goutte

This will add the following lines to your composer.json and download the project and its dependencies to your projects ./vendor directory:

// ./composer.json
{
    "name": "weidner/laravel-goutte-test",
    "description": "A dummy project used to test the Laravel Goutte Facade.",

    // ...

    "require": {
        "php": "^7.2",
        "laravel/framework": "^8",
        "weidner/goutte": "^2",
        // ...
    },

    //...
}

Usage

In order to use the static interface we first have to customize the application configuration to tell the system where it can find the new service. Open the file config/app.php in the editor of your choice and add the following lines ([1], [2]):

// config/app.php

return [

    // ...

    'providers' => [

        // ...

        /*
         * Package Service Providers...
         */
        Weidner\Goutte\GoutteServiceProvider::class, // [1] This will register the Package in the laravel echo system

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

    // ...

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,

        // ...

        'Goutte' => Weidner\Goutte\GoutteFacade::class, // [2] It will register as an alias for the Goutte facade
        'Hash' => Illuminate\Support\Facades\Hash::class,

        // ...
    ],

];

Now you should be able to use the facade within your application. Laravel will autoload the corresponding classes once you use the registered alias.

// routes/web.php

Route::get('/', function() {
    $crawler = Goutte::request('GET', 'https://duckduckgo.com/html/?q=Laravel');
    $crawler->filter('.result__title .result__a')->each(function ($node) {
      dump($node->text());
    });
    return view('welcome');
});

TIP: If you retrieve a "Class 'Goutte' not found"-Exception try to update the autoloader by running composer dump-autoload in your project root.

TIP: You can use the package with Lumen as well. Register the GoutteServiceProvider in bootstrap/app.php and provide the missing path to your configuration directory in your AppServiceProvider (ref #34).

Configuration

You can customize the default request options to apply to each request of the client. Copy the default configuration to your application directory first:

php artisan vendor:publish --provider="Weidner\Goutte\GoutteServiceProvider"

Open the created file in the config/goutte.php and customize the configuration options to your liking.

<?php

return [
    'client' => [
        'max_redirects' => 0,
    ],
];

Have a look into the Symfony Http Client Documentation for a full list of available options.

Version Constraint

Release Supported Versions
Laravel Goutte 2.2 Laravel 8/9
Laravel Goutte 2.0 Laravel 8
Laravel Goutte 1.6 Laravel 5/6/7



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap