PHPackages                             msonowal/simple-hl7 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. msonowal/simple-hl7

ActiveLibrary

msonowal/simple-hl7
===================

Simple HL7 Message generator and sender.

v0.1.4(6y ago)21.4k3MITPHPPHP &gt;=7.1

Since May 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/msonowal/simple-hl7)[ Packagist](https://packagist.org/packages/msonowal/simple-hl7)[ RSS](/packages/msonowal-simple-hl7/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (0)

Introduction
------------

[](#introduction)

A PHP-based HL7 v2.x Parsing, Generation and Sending library, inspired from the famous Perl Net-HL7 package.

Installation
------------

[](#installation)

```
composer require msonowal/simple-hl7
```

Usage
-----

[](#usage)

### Parsing

[](#parsing)

```
// Create a Message object from a HL7 string
$msg = new Message("MSH|^~\\&|1|\rPID|||abcd|\r"); // Either \n or \r can be used as segment endings
$pid = $msg->getSegmentByIndex(1);
echo $pid->getField(3); // prints 'abcd'
echo $msg->toString(true); // Prints entire HL7 string
```

### Creating new messages

[](#creating-new-messages)

```
// Create an empty Message object, and populate MSH and PID segments...
$msg = new Message();
$msh = new MSH();
$msg->addSegment($msh); // Message is: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\n"

// Create any custom segment
$abc = new Segment('ABC');
$abc->setField(1, 'xyz');
$abc->setField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty
$msg->setSegment($abc, 1); // Message is now: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\nABC|xyz|\n"

// Create a defined segment (To know which segments are defined in this package, look into Segments/ directory)
// Advantages of defined segments over custom ones (shown above) are 1) Helpful setter methods, 2) Auto-incrementing segment index
$pid = new PID(); // Automatically creates PID segment, and adds segment index at PID.1
$pid->setPatientName([$lastname, $firstname, $middlename, $suffix]); // Use a setter method to add patient's name at standard position (PID.5)
$pid->setField('abcd', 5); // Apart from standard setter methods, you can manually set a value at any position too
unset $pid; // Destroy the segment and decrement the id number. Useful when you want to discard a segment.

// Create a segment with empty sub-fields retained
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true); // Third argument 'true' forces to keep all sub fields
$pv1 = $msg->getSegmentByIndex(1);
$fields = $pv1->getField(3); // $fields is ['', 'AAAA1', '', '', 'BB']

// Create/send message with segment-ending bar character (|) removed
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx\n", ['SEGMENT_ENDING_BAR' => false]);
$msg->toString(true); // Returns "MSH|^~\&|1\nABC|||xxx\n"
(new Connection($ip, $port))->send($msg); // Sends the message without ending bar-characters (details on Connection below)

// Specify custom values for separators, HL7 version etc.
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.3']);
```

### Send messages to remote listeners

[](#send-messages-to-remote-listeners)

```
$ip = '127.0.0.1'; // An IP
$port = '12001'; // And Port where a HL7 listener is listening
$message = new Message($hl7String); // Create a Message object from your HL7 string

// Create a Socket and get ready to send message. Optionally add timeout in seconds as 3rd argument (default: 10 sec)
$connection = new Connection($ip, $port);
$response = $connection->send($message); // Send to the listener, and get a response back
echo $reponse->toString(true); // Prints ACK from the listener
```

### ACK

[](#ack)

Handle ACK message returned from a remote HL7 listener...

```
$ack = (new Connection($ip, $port))->send($message); // Send a HL7 to remote listener
$returnString = $ack->toString(true);
if (strpos($returnString, 'MSH') === false) {
    echo "Failed to send HL7 to 'IP' => $ip, 'Port' => $port";
}
$msa = $ack->getSegmentsByName('MSA')[0];
$ackCode = $msa->getAcknowledgementCode();
if ($ackCode[1] === 'A') {
    echo "Recieved ACK from remote\n";
}
else {
    echo "Recieved NACK from remote\n";
    echo "Error text: " . $msa->getTextMessage()
}
```

APIs
----

[](#apis)

This package exposes a number of public methods for convenient HL7 handling. Some examples are:

1. Considering you have a Message object (say, `$msg = new Message(file_get_contents('somefile.hl7'));`)

```
    $msg->toFile('/path/to/some.hl7'); // Write to a file
    $msg->isOru(); // Check if it's an ORU
    $msg->isOrm(); // Check if it's an ORM
```

Visit [docs\\README](docs/README.md) for details on available APIs

All segment level getter/setter APIs can be used in two ways -

- If a position index isn't provided as argument (1st argument for getters, 2nd for setters), a standard index is used.
    `$pid->setPatientName('John Doe')` -&gt; Set patient name at position 5 as per HL7 v2.3 [standard](https://corepointhealth.com/resource-center/hl7-resources/hl7-pid-segment)
    `$pid->getPatientAddress()` -&gt; Get patient address from standard 11th position
- To use a custom position index, provide it in the argument:
    `$pid->setPatientName('John Doe', 6)` -&gt; Set patient name at 6th position in PID segment
    `$pid->getPatientAddress(12)` -&gt; Get patient address from 12th position

### TODOs

[](#todos)

- Data Validation
- Search by regex and return segment/field/index
- Define more segments

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~35 days

Recently: every ~21 days

Total

6

Last Release

2376d ago

Major Versions

v0.1.4 → v1.0.0-beta2019-11-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/ab0ec80e4e9d7eaeed5b9010e62cf7ce3121f4779870663a253218cfa43f8f7c?d=identicon)[msonowal](/maintainers/msonowal)

---

Top Contributors

[![msonowal](https://avatars.githubusercontent.com/u/6334484?v=4)](https://github.com/msonowal "msonowal (1 commits)")

---

Tags

sendparsegeneratehl7

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/msonowal-simple-hl7/health.svg)

```
[![Health](https://phpackages.com/badges/msonowal-simple-hl7/health.svg)](https://phpackages.com/packages/msonowal-simple-hl7)
```

###  Alternatives

[aranyasen/hl7

HL7 parser, generator and sender.

1951.3M](/packages/aranyasen-hl7)[smalot/pdfparser

Pdf parser library. Can read and extract information from pdf file.

2.7k34.5M216](/packages/smalot-pdfparser)[sendgrid/sendgrid

This library allows you to quickly and easily send emails through Twilio SendGrid using PHP.

1.5k47.5M164](/packages/sendgrid-sendgrid)[fightbulc/moment

Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js

9693.2M10](/packages/fightbulc-moment)[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[sqids/sqids

Generate short YouTube-looking IDs from numbers

5781.4M33](/packages/sqids-sqids)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
