HL7

其他类别 2025-08-19

重要的

  • 最低支持的PHP版本已更新为8.0
    最后支持版本:
    • PHP 7.0或7.1 => 1.5.4
    • PHP 7.2 => 2.0.2
    • PHP 7.4 => 2.1.7
  • 全局设置SEGMENT_ENDING_BAR被弃用,并将在以后的版本中删除。代替使用WITH_SEGMENT_ENDING_FIELD_SEPARATOR

介绍

基于PHP的HL7 V2.X解析,生成和发送库,灵感来自著名的Perl Net- HL7软件包。

安装

composer require aranyasen/ HL7 

用法

导入库

HL7 factory class use Aranyasen HL7 Message; // If Message is used use Aranyasen HL7 Segment; // If Segment is used use Aranyasen HL7 SegmentsMSH; // If MSH is used // ... and so on">
 // First, import classes from the library as needed...
use Aranyasen  HL7 ; // HL7 factory class
use Aranyasen  HL7  Message ; // If Message is used
use Aranyasen  HL7  Segment ; // If Segment is used
use Aranyasen  HL7  Segments  MSH ; // If MSH is used
// ... and so on

解析

HL7 string $message = HL7 ::from("MSH|^~\&|1|")->createMessage(); // Returns Message object // Or, using Message class... $message = new Message("MSH|^~\&|1|rPID|||abcd|r"); // Either n or r can be used as segment endings // Get string form of the message echo $message->toString(true); // Extracting segments and fields from a Message object... $message->getSegmentByIndex(1); // Get the first segment $message->getSegmentsByName('ABC'); // Get an array of all 'ABC' segments $message->getFirstSegmentInstance('ABC'); // Returns the first ABC segment. Same as $message->getSegmentsByName('ABC')[0]; // Check if a segment is present in the message object $message->hasSegment('ABC'); // return true or false based on whether PID is present in the $message object // Check if a message is empty $message = new Message(); $message->isempty(); // Returns true">
 // Create a Message object from a HL7 string
$ message = HL7 :: from ( " MSH|^~ \ &|1| " )-> createMessage (); // Returns Message object

// Or, using Message class...
$ message = new Message ( " MSH|^~ \ &|1| r PID|||abcd| r" ); // Either n or r can be used as segment endings

// Get string form of the message
echo $ message -> toString ( true );

// Extracting segments and fields from a Message object...
$ message -> getSegmentByIndex ( 1 ); // Get the first segment
$ message -> getSegmentsByName ( ' ABC ' ); // Get an array of all 'ABC' segments
$ message -> getFirstSegmentInstance ( ' ABC ' ); // Returns the first ABC segment. Same as $message->getSegmentsByName('ABC')[0];

// Check if a segment is present in the message object
$ message -> hasSegment ( ' ABC ' ); // return true or false based on whether PID is present in the $message object

// Check if a message is empty
$ message = new Message ();
$ message -> isempty (); // Returns true

撰写新消息

HL7 object. It is a factory class with various helper methods to help build a HL7 . $message = HL7 ::build()->createMessage(); // Creates an empty message // The HL7 factory class provides methods that can be chained together in a fluent fashion $message = HL7 ::build() ->withComponentSeparator('#') ->withFieldSeparator('-') ->createMessage(); // Or, using Message class... $message = new Message();">
 // The class ` HL7 ` can be used to build HL7 object. It is a factory class with various helper methods to help build a HL7 .
$ message = HL7 :: build ()-> createMessage (); // Creates an empty message

// The HL7 factory class provides methods that can be chained together in a fluent fashion
$ message = HL7 :: build ()
    -> withComponentSeparator ( ' # ' )
    -> withFieldSeparator ( ' - ' )
    -> createMessage ();

// Or, using Message class...
$ message = new Message ();

消息构造函数参数

HL7. // Note: All of these properties are available as fluent methods in HL7 factory class (shown above). So it's recommended to use that for readability // Creating multiple message objects may have an unexpected side effect: segments start with wrong index values (Check tests/MessageTest for explanation)... // Use 4th argument as true, or call resetSegmentIndices() on $message object to reset segment indices to 1 $message = new Message("MSH|^~&|||||||ORM^O01||P|2.3.1|", null, true, true); // ... any segments added here will now start index from 1, as expected.">
 // When a message is composed using Message class, there are multiple parameters available to define the properties of the HL7 .
// Note: All of these properties are available as fluent methods in HL7 factory class (shown above). So it's recommended to use that for readability

// Creating multiple message objects may have an unexpected side effect: segments start with wrong index values (Check tests/MessageTest for explanation)...
// Use 4th argument as true, or call resetSegmentIndices() on $message object to reset segment indices to 1
$ message = new Message ( " MSH|^~&|||||||ORM^O01||P|2.3.1| " , null , true , true );
// ... any segments added here will now start index from 1, as expected. 
HL7String = "MSH|^~&|||||||ORU^R01|00001|P|2.3.1|n" . "OBX|1||11^AA|n" . "OBX|1||22^BB|n"; $message = new Message($ HL7 String, null, true, true, false); $// $message contains both OBXs with given indexes in the string">
 // Sometimes you may want to have exact index values, rather than auto-incrementing for each instance of a segment
// Use 5th argument as false...
$ HL7 String = " MSH|^~&|||||||ORU^R01|00001|P|2.3.1| n" . " OBX|1||11^AA| n" . " OBX|1||22^BB| n" ;
$ message = new Message ( $ HL7 String , null , true , true , false ); $ // $message contains both OBXs with given indexes in the string 
getSegmentByIndex(1); $fields = $pv1->getField(3); // $fields is ['', 'AAAA1', '', '', 'BB'] // Create/send message with segment-ending field-separator character (default "|") removed $message = new Message("MSH|^~\&|1|nABC|||xxxn", ['WITH_SEGMENT_ENDING_FIELD_SEPARATOR' => false]); $message->toString(true); // Returns "MSH|^~&|1nABC|||xxxn" (new Connection($ip, $port))->send($message); // Sends the message without ending field-separator character (details on Connection below) // Specify custom values for separators, 'rn', ' HL7 _VERSION' => '2.3']); // Segment with separator character (~) creates sub-arrays containing each sub-segment $message = new Message("MSH|^~&|||||||ADT^A01||P|2.3.1|nPID|||3^0~4^1"); // Creates [[3,0], [4,1]] // To create a single array instead, pass 'true' as 6th argument. This may be used to retain behavior from previous releases // Notice: Since this leads to a non-standard behavior, it may be removed in future $message = new Message("MSH|^~&|||||||ADT^A01||P|2.3.1|nPID|||3^0~4^1", null, false, false, true, true); // Creates ['3', '0~4', '1'] // or $message = new Message("MSH|^~&|||||||ADT^A01||P|2.3.1|nPID|||3^0~4^1", doNotSplitRepetition: true); // Creates ['3', '0~4', '1']">
 // Create a segment with empty sub-fields retained
$ message = new Message ( " MSH|^~ \ &|1| r PV1|1|O|^AAAA1^^^BB| " , null , true ); // Third argument 'true' forces to keep all sub-fields
$ pv1 = $ message -> getSegmentByIndex ( 1 );
$ fields = $ pv1 -> getField ( 3 ); // $fields is ['', 'AAAA1', '', '', 'BB']

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

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

// Segment with separator character (~) creates sub-arrays containing each sub-segment
$ message = new Message ( " MSH|^~&|||||||ADT^A01||P|2.3.1| n PID|||3^0~4^1 " ); // Creates [[3,0], [4,1]]

// To create a single array instead, pass 'true' as 6th argument. This may be used to retain behavior from previous releases
// Notice: Since this leads to a non-standard behavior, it may be removed in future
$ message = new Message ( " MSH|^~&|||||||ADT^A01||P|2.3.1| n PID|||3^0~4^1 " , null , false , false , true , true ); // Creates ['3', '0~4', '1']
// or
$ message = new Message ( " MSH|^~&|||||||ADT^A01||P|2.3.1| n PID|||3^0~4^1 " , doNotSplitRepetition: true ); // Creates ['3', '0~4', '1'] 

处理细分市场

setField(1, 'xyz'); $abc->setField(2, 0); $abc->setField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty $abc->clearField(2); // Clear the value from field 2 $message->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. // It is possible that segments get added in a way that the Set IDs/Sequence IDs within the message are not in order or leave gaps. To reset all Set/Sequence IDs in the message: $message->reindexSegments();">
 // Once a message object is created, we can now add, insert, set segments and fields.

// Create a MSH segment and add to message object
$ msh = new MSH ();
$ message -> addSegment ( $ msh ); // Message is: "MSH|^~&|||||20171116140058|||2017111614005840157||2.3|n"

// Create a custom segment
$ abc = new Segment ( ' ABC ' );
$ abc -> setField ( 1 , ' xyz ' );
$ abc -> setField ( 2 , 0 );
$ abc -> setField ( 4 , [ '' ]); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty
$ abc -> clearField ( 2 ); // Clear the value from field 2
$ message -> 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.

// It is possible that segments get added in a way that the Set IDs/Sequence IDs within the message are not in order or leave gaps. To reset all Set/Sequence IDs in the message:

$ message -> reindexSegments ();

将消息发送给远程侦听器

旁注:要运行连接,您需要安装php ext-sockets https://www.**ph*p.net/manual/enual/en/sockets.installation.php

HL7 listener is listening $message = new Message($ HL7 String); // 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 $response->toString(true); // Prints ACK from the listener">
 $ ip = ' 127.0.0.1 ' ; // An IP
$ port = ' 12001 ' ; // And Port where a HL7 listener is listening
$ message = new Message ( $ HL7 String ); // 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 $ response -> toString ( true ); // Prints ACK from the listener

ACK

处理ACK消息从远程HL7侦听器返回...

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->getFirstSegmentInstance('MSA'); $ackCode = $msa->getAcknowledgementCode(); if ($ackCode[1] === 'A') { echo "Received ACK from remoten"; } else { echo "Received NACK from remoten"; echo "Error text: " . $msa->getTextMessage(); }">
 $ 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 -> getFirstSegmentInstance ( ' MSA ' );
$ ackCode = $ msa -> getAcknowledgementCode ();
if ( $ ackCode [ 1 ] === ' A ' ) {
    echo " Received ACK from remote n" ;
}
else {
    echo " Received NACK from remote n" ;
    echo " Error text: " . $ msa -> getTextMessage ();
}

从给定的HL7消息中创建ACK响应:

 $ msg = new Message ( " MSH|^~ \ &|1| r ABC|1||^AAAA1^^^BB| " , null , true );
$ ackResponse = new ACK ( $ msg );

创建ACK对象时可以通过选项:

'rn', 'HL7_VERSION' => '2.5']);">
 $ msg = new Message ( " MSH|^~ \ &|1| r ABC|1||^AAAA1^^^BB| " , null , true );
$ ackResponse = new ACK ( $ msg , null , [ ' SEGMENT_SEPARATOR ' => ' rn ' , ' HL7 _VERSION ' => ' 2.5 ' ]);

蜜蜂

该软件包公开了许多公共方法,以方便HL7处理。一些例子是:

  1. 考虑到您有一个消息对象(例如, $msg = new Message(file_get_contents('somefile. HL7 ')); );
HL7'); // Write to a file $msg->isOru(); // Check if it's an ORU $msg->isOrm(); // Check if it's an ORM">
    $ 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

有关可用API的详细信息,请访问Docs Readme

所有段级别的Getter/Setter API都可以通过两种方式使用 -

  • 如果未提供位置索引作为参数(Getters的第一个参数,seters的第二个参数),则使用标准索引。 $pid->setPatientName('John Doe') - >按照HL7 v2.3标准$pid->getPatientAddress() - >从标准的第11位获得患者地址

  • 要使用自定义位置索引,请在参数中提供: $pid->setPatientName('John Doe', 6) - >在PID段中的第6个位置设置患者名称$pid->getPatientAddress(12) - >从12th位置获得患者地址

问题

错误报告和功能请求可以在GitHub Disears Tracker上提交。

贡献

有关信息,请参见贡献。

下载源码

通过命令行克隆项目:

git clone https://github.com/senaranya/HL7.git