NATHAN242's Projects

Projects

Tools

Libraries

Web Tools


PHP ICAP Client

Project repo: LINK

Introduction

PHP client library for the Internet Content Adaptation Protocol (RFC 3507).

This library allows PHP to interact with ICAP servers (such as virus scanners and content filters). It supports building an ICAP request with a request body containing encapsulated parts of another request (such as HTTP) and can parse the body of an ICAP response according to the "Encapsulated" header into a PHP array.

I originally wrote this so a PHP microservice responsible for storing uploaded files could run them through a virus scanner and I was unable to find a PHP implementation of an ICAP client.

The library currently does not support message preview functionality or persistent connections (each request will close the connection at the end) but I may add support for those at a later time.

Installation

The library can be installed via composer:

composer require nathan242/php-icap-client

Usage

Instantiate the class with the ICAP server address and port:

$icap = new IcapClient('127.0.0.1', 13440);

Send an OPTIONS request to the "example" service:

$icap->options('example');

Send a REQMOD and RESPMOD request to the "example" service:

$icap->reqmod(
    'example',
    [
        'req-hdr' => "POST /test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n",
        'req-body' => 'This is another test.'
    ]
);

$icap->respmod(
    'example',
    [
        'res-hdr' => "HTTP/1.1 200 OK\r\nServer: Test/0.0.1\r\nContent-Type: text/html\r\n\r\n",
        'res-body' => 'This is a test.'
    ]
);

Successful requests will return an array describing the response:

Array
(
    [protocol] => Array
        (
            [icap] => ICAP/1.0
            [code] => 200
            [message] => OK
        )

    [headers] => Array
        (
            [Date] => Wed, 03 Jul 2019 22:11:33 GMT
            [ISTag] => X7K07GDZQW702ZVLSG6WWO0EPE2CTOMR
            [Encapsulated] => res-hdr=0, res-body=64
            [Server] => ICAP/1.3 Python/2.7.3
        )

    [body] => Array
        (
            [res-hdr] => HTTP/1.1 200 OK
content-type: text/html
server: Test/0.0.1
            [res-body] => This is a test.
        )

    [rawBody] => HTTP/1.1 200 OK
content-type: text/html
server: Test/0.0.1

f
This is a test.
0


)