Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 1.74 KB

29_Custom_Request_Header.md

File metadata and controls

64 lines (50 loc) · 1.74 KB

Custom Request Header

Add some header information to the crawler request.

The Members Bundle adds a auth header element by default.

Event

Name Class Setter
lucene_search.task.crawler.request_header Event\CrawlerRequestHeaderEvent addHeader

Example: Auth

parameters:
    lucene_search_user_name: 'Crawler'
    lucene_search_password: 'crawler@universe.org'


AppBundle\EventListener\CrawlerHeader:
    arguments:
        $userName: '%lucene_search_user_name%'
        $password: '%lucene_search_password%'
    tags:
        - { name: kernel.event_subscriber }
<?php

namespace AppBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use LuceneSearchBundle\Event\CrawlerRequestHeaderEvent;
use LuceneSearchBundle\LuceneSearchEvents;

class CrawlerHeader implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            LuceneSearchEvents::LUCENE_SEARCH_CRAWLER_REQUEST_HEADER => 'addHeaderToLuceneCrawler'
        ];
    }

    public function addHeaderToLuceneCrawler(CrawlerRequestHeaderEvent $event)
    {
        //example 1: token auth.
        $event->addHeader([
            'name'          => 'x-auth-token',
            'value'         => 'your-special-token',
            'identifier'    => 'lucene-search-token-auth'
        ]);
        
        //example 2: basic auth.
        $event->addHeader([
            'name'          => 'Authorization',
            'value'         => 'Basic ' . base64_encode('USERNAME:PASSWORD'),
            'identifier'    => 'lucene-search-basic-auth'
        ]);
    }
}