PHP SIP Parsing/Rendering Library

Overview
php-sip

PHP SIP Parsing/Rendering Library

RFC 3261 compliant SIP parsing and rendering library for PHP 7.4.

Build Status Latest Stable Version Test Coverage Maintainability License

Quickstart

SIP Message Parsing

Once installed, you can parse SIP messages right away as follows:

version); printf("Request method: %s" . PHP_EOL, $message->method); printf("Request URI: %s" . PHP_EOL, $message->uri); printf("Via: %s" . PHP_EOL, $message->via->values[0]->host); printf("Via branch: %s" . PHP_EOL, $message->via->values[0]->branch); printf("From scheme: %s" . PHP_EOL, $request->from->uri->scheme); printf("From user: %s" . PHP_EOL, $request->from->uri->user); printf("From host: %s" . PHP_EOL, $request->from->uri->host); printf("From tag: %s" . PHP_EOL, $request->from->tag); printf("To scheme: %s" . PHP_EOL, $request->to->uri->scheme); printf("To user: %s" . PHP_EOL, $request->to->uri->user); printf("To host: %s" . PHP_EOL, $request->to->uri->host); printf("Sequence number: %s" . PHP_EOL, $message->cSeq->sequence); printf("Call ID: %s" . PHP_EOL, $message->callId->value);">
/*
 * $text holds your SIP message as a string, for example
 * $text = 'REGISTER sip:192.168.0.1 SIP/2.0 /.../';
 */
$message = \RTCKit\SIP\Message::parse($text);

/* Outputs "RTCKit\SIP\Request" */
echo get_class($message) . PHP_EOL;

/* Outputs something similar to:
 * Protocol version:   SIP/2.0
 * Request method:     REGISTER
 * Request URI:        sip:192.168.0.1
 * Via:                192.168.0.2:5050
 * Via branch:         z9hG4bK.eAV4o0nXr
 * From scheme:        sip
 * From user:          buzz
 * From host:          192.168.0.1
 * From tag:           SFJbQ2oWh
 * To scheme:          sip
 * To user:            buzz
 * To host:            192.168.0.1
 * Sequence number:    20
 * Call ID:            ob0EYyuyC0
 */
printf("Protocol version:   %s" . PHP_EOL, $message->version);
printf("Request method:     %s" . PHP_EOL, $message->method);
printf("Request URI:        %s" . PHP_EOL, $message->uri);
printf("Via:                %s" . PHP_EOL, $message->via->values[0]->host);
printf("Via branch:         %s" . PHP_EOL, $message->via->values[0]->branch);
printf("From scheme:        %s" . PHP_EOL, $request->from->uri->scheme);
printf("From user:          %s" . PHP_EOL, $request->from->uri->user);
printf("From host:          %s" . PHP_EOL, $request->from->uri->host);
printf("From tag:           %s" . PHP_EOL, $request->from->tag);
printf("To scheme:          %s" . PHP_EOL, $request->to->uri->scheme);
printf("To user:            %s" . PHP_EOL, $request->to->uri->user);
printf("To host:            %s" . PHP_EOL, $request->to->uri->host);
printf("Sequence number:    %s" . PHP_EOL, $message->cSeq->sequence);
printf("Call ID:            %s" . PHP_EOL, $message->callId->value);

SIP Message Rendering

Rendering is the opposite action of parsing; for example, let's prepare a 200 OK response for a REGISTER request:

$response = new \RTCKit\SIP\Response;
$response->version = 'SIP/2.0';
$response->code = 200;

$response->via = new \RTCKit\SIP\Header\ViaHeader;
$response->via->values[0] = new \RTCKit\SIP\Header\ViaValue;
$response->via->values[0]->protocol = 'SIP';
$response->via->values[0]->version = '2.0';
$response->via->values[0]->transport = 'UDP';
$response->via->values[0]->host = '192.168.0.2:5050';
$response->via->values[0]->branch = 'z9hG4bK.eAV4o0nXr';

$response->from = new \RTCKit\SIP\Header\NameAddrHeader;
$response->from->uri = new \RTCKit\SIP\URI;
$response->from->uri->scheme = 'sip';
$response->from->uri->user = 'buzz';
$response->from->uri->host = '192.168.0.1';
$response->from->tag = 'SFJbQ2oWh';

$response->to = new \RTCKit\SIP\Header\NameAddrHeader;
$response->to->uri = new \RTCKit\SIP\URI;
$response->to->uri->scheme = 'sip';
$response->to->uri->user = 'buzz';
$response->to->uri->host = '192.168.0.1';
$response->to->tag = '8cQtUyH6N5N9K';

$response->cSeq = new \RTCKit\SIP\Header\CSeqHeader;
$response->cSeq->sequence = 20;
$response->cSeq->method = 'REGISTER';

$response->callId = new \RTCKit\SIP\Header\CallIdHeader;
$response->callId->value = 'ob0EYyuyC0';

$response->maxForwards = new \RTCKit\SIP\Header\ScalarHeader;
$response->maxForwards->value = 70;

$response->contact = new \RTCKit\SIP\Header\ContactHeader;
$response->contact->values[0] = new \RTCKit\SIP\Header\ContactValue;
$response->contact->values[0]->uri = new \RTCKit\SIP\URI;
$response->contact->values[0]->uri->scheme = 'sip';
$response->contact->values[0]->uri->user = 'buzz';
$response->contact->values[0]->uri->host = '192.168.0.2';
$response->contact->values[0]->uri->port = 5050;
$response->contact->values[0]->uri->transport = 'udp';
$response->contact->values[0]->expires = 3600;

$response->userAgent = new \RTCKit\SIP\Header\Header;
$response->userAgent->values[0] = 'MyDeskPhone/1.0.0';

/* Outputs:
 * SIP/2.0 200 OK
 * Via: SIP/2.0/UDP 192.168.0.2:5050;branch=z9hG4bK.eAV4o0nXr
 * From: 
   
    ;tag=SFJbQ2oWh
   
 * To: 
   
    ;tag=8cQtUyH6N5N9K
   
 * Contact: 
   
    ;expires=3600
   
 * Call-ID: ob0EYyuyC0
 * CSeq: 20 REGISTER
 * Max-Forwards: 70
 * User-Agent: MyDeskPhone/1.0.0
 */
echo $response->render();

SIP Message Stream Parsing

If your use case involves a continuous data stream rather than individual messages, the StreamParser class can help; this is particularly useful for analyzing SIP trace files or packet captures, parsing SIP traffic over TCP etc.

/* Instantiate the Stream Parser */
$parser = new \RTCKit\SIP\StreamParser;

$fp = fopen(/.../);

while (!feof($fp)) {
    $bytes = fread($fp, 256);

    /* The actual input string ($bytes) can be retrieved from any stream-like source */
    if ($parser->process($bytes, $messages) === \RTCKit\SIP\StreamParser::SUCCESS) {
        foreach ($messages as $message) {
            /*
             * $message is either a Request or a Response object, using
             * the same structure as messages returned by Message::parse()
             */
        }
    }
}

Lastly, the provided examples are a good starting point.

Requirements

RTCKit\SIP is compatible with PHP 7.4+ and has no external library and extension dependencies.

Installation

You can add the library as project dependency using Composer:

composer require rtckit/sip

If you only need the library during development, for instance when used in your test suite, then you should add it as a development-only dependency:

composer require --dev rtckit/sip

Tests

To run the test suite, clone this repository and then install dependencies via Composer:

composer install

Then, go to the project root and run:

php -d memory_limit=-1 ./vendor/bin/phpunit -c ./etc/phpunit.xml.dist

Static Analysis

In order to ensure high code quality, RTCKit\SIP uses PHPStan and Psalm:

php -d memory_limit=-1 ./vendor/bin/phpstan analyse -c ./etc/phpstan.neon -n -vvv --ansi --level=max src
php -d memory_limit=-1 ./vendor/bin/psalm --config=./etc/psalm.xml --show-info=true

License

MIT, see LICENSE file.

Acknowledgments

Contributing

Bug reports (and small patches) can be submitted via the issue tracker. Forking the repository and submitting a Pull Request is preferred for substantial patches.

Comments
  • Question

    Question

    Hello,

    I m newbie with sip and have a question. I read the readme and think the anwser to my question is yes but i have to be sure.

    • Can i make a phone ringing and display (on the phone) a custom message.

    For what i understand the documentation part for my use case is "SIP Message Rendering" and i guess "SIP Message Parsing" to be sure everything run smothly (aka : following protocol) ?

    Thx for your time

    opened by HumanG33k 1
  • v0.7.0

    v0.7.0

    :zap: Authenticate/Authorization Header Overhaul

    • Separate header classes for requests (challenges) and responses;
    • Proper qop parameter parsing and rendering;
    • Digest validation and verification functionality;
    • Support for modern RFC 8760 digest algorithms.

    :art: Docblock fixes

    opened by cdosoftei 0
  • v0.6.0

    v0.6.0

    • :star: SIP URI Parser/Renderer
    • :zap: Request-URI and From, To and Contact headers use SIP URI objects
    • :earth_africa: IPv6 SIP URI support
    • :zap: PSR compliant exception class naming
    opened by cdosoftei 0
  • v0.5.1

    v0.5.1

    :zap: Auth header value parameter rendering improvements:

    • nc is being rendered verbatim (i.e. no implicit zero padding);
    • algorithm is no longer rendered as quoted-string.
    opened by cdosoftei 0
  • v0.5.0

    v0.5.0

    Breaking changes:

    • :lady_beetle: Fixed nc parameter rendering for Auth Header field values;
    • :lady_beetle: Changed nc parameter to string; it must hold the verbatim value for digest hash verification.
    opened by cdosoftei 0
  • v0.3.1

    v0.3.1

    • :heavy_check_mark: Reorganized stream tests (dedicated directory)
    • :heavy_check_mark: Added lioneagle/sipparser test material
    • :star: Recognize RFC 3262 Headers
    opened by cdosoftei 0
  • Improvements for PHPUnit assertion and fixtues

    Improvements for PHPUnit assertion and fixtues

    Changed log

    • According to the official PHPUnit doc, it should be the protected function setUp and protected function tearDown methods.
    • Using the assertCount to assert expected count is same as result.
    opened by peter279k 0
Releases(v0.7.0)
Owner
rtckit
:rocket: Real-Time Communications Projects
rtckit
A simple PHP library for handling Emoji

Emoji Emoji images from unicode characters and names (i.e. :sunrise:). Built to work with Twemoji images. use HeyUpdate\Emoji\Emoji; use HeyUpdate\Emo

null 54 May 23, 2022
A simple PHP library for handling Emoji

Emoji Emoji images from unicode characters and names (i.e. :sunrise:). Built to work with Twemoji images. use HeyUpdate\Emoji\Emoji; use HeyUpdate\Emo

null 51 Jan 15, 2021
Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Emanuil Rusev 14.3k Jan 8, 2023
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

The League of Extraordinary Packages 2.4k Jan 1, 2023
Convert HTML to Markdown with PHP

HTML To Markdown for PHP Library which converts HTML to Markdown for your sanity and convenience. Requires: PHP 7.2+ Lead Developer: @colinodell Origi

The League of Extraordinary Packages 1.5k Dec 28, 2022
A super fast, highly extensible markdown parser for PHP

A super fast, highly extensible markdown parser for PHP What is this? A set of PHP classes, each representing a Markdown flavor, and a command line to

Carsten Brandt 989 Dec 16, 2022
An HTML5 parser and serializer for PHP.

HTML5-PHP HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has w

null 1.2k Dec 31, 2022
📜 Modern Simple HTML DOM Parser for PHP

?? Simple Html Dom Parser for PHP A HTML DOM parser written in PHP - let you manipulate HTML in a very easy way! This is a fork of PHP Simple HTML DOM

Lars Moelleken 665 Jan 4, 2023
Advanced shortcode (BBCode) parser and engine for PHP

Shortcode Shortcode is a framework agnostic PHP library allowing to find, extract and process text fragments called "shortcodes" or "BBCodes". Example

Tomasz Kowalczyk 358 Nov 26, 2022
Parsica - PHP Parser Combinators - The easiest way to build robust parsers.

Parsica The easiest way to build robust parsers in PHP.

null 0 Feb 22, 2022
UpToDocs scans a Markdown file for PHP code blocks, and executes each one in a separate process.

UpToDocs UpToDocs scans a Markdown file for PHP code blocks, and executes each one in a separate process. Include this in your CI workflows, to make s

Mathias Verraes 56 Nov 26, 2022
HTML sanitizer, written in PHP, aiming to provide XSS-safe markup based on explicitly allowed tags, attributes and values.

TYPO3 HTML Sanitizer ℹ️ Common safe HTML tags & attributes as given in \TYPO3\HtmlSanitizer\Builder\CommonBuilder still might be adjusted, extended or

TYPO3 GitHub Department 22 Dec 14, 2022
This is a simple php project to help a friend how parse a xml file.

xml-parser-with-laravie Requirements PHP 7.4+ Composer 2+ How to to setup to test? This is very simple, just follow this commands git clone https://gi

Lucas Saraiva 2 Dec 3, 2021
This is a php parser for plantuml source file.

PlantUML parser for PHP Overview This package builds AST of class definitions from plantuml files. This package works only with php. Installation Via

Tasuku Yamashita 5 May 29, 2022
Efficient, easy-to-use, and fast PHP JSON stream parser

JSON Machine Very easy to use and memory efficient drop-in replacement for inefficient iteration of big JSON files or streams for PHP 5.6+. See TL;DR.

Filip Halaxa 801 Dec 28, 2022
php html parser,类似与PHP Simple HTML DOM Parser,但是比它快好几倍

HtmlParser php html解析工具,类似与PHP Simple HTML DOM Parser。 由于基于php模块dom,所以在解析html时的效率比 PHP Simple HTML DOM Parser 快好几倍。 注意:html代码必须是utf-8编码字符,如果不是请转成utf-8

俊杰jerry 522 Dec 29, 2022
A simple PHP scripting application which fetch emails from your Gmail account according to a filter and parses them for information.

A simple PHP scripting application which fetch emails from your Gmail account according to a filter and parses them for information.

Haitham Sweilem 1 Jan 18, 2022
A PHP hold'em range parser

mattjmattj/holdem-range-parser A PHP hold'em range parser Installation No published package yet, so you'll have to clone the project manually, or add

Matthias Jouan 1 Feb 2, 2022
EmailReplyParser is a PHP library for parsing plain text email content, based on GitHub's email_reply_parser library written in Ruby

EmailReplyParser EmailReplyParser is a PHP library for parsing plain text email content, based on GitHub's email_reply_parser library written in Ruby.

William Durand 606 Dec 8, 2022
Library that provides collection, processing, and rendering functionality for PHP code coverage information.

phpunit/php-code-coverage Provides collection, processing, and rendering functionality for PHP code coverage information. Installation You can add thi

Sebastian Bergmann 8.5k Jan 5, 2023