SPRZEDAŻ: 1-800-867-1389
EN
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

Using Service Bus from PHP with AMQP 1.0

Updated: August 18, 2014

Proton-PHP is a PHP language binding to Proton-C; that is, Proton-PHP is implemented as a wrapper around an engine implemented in C.

You can download Proton-C and its associated bindings (including PHP) from here. The download is in source code form. To build the code, follow the instructions contained in the downloaded package.

ImportantImportant
At the time of this writing, the SSL support in Proton-C is only available for Linux operating systems. Because Microsoft Azure Service Bus requires the use of SSL, Proton-C (and the language bindings) can only be used to access Microsoft Azure Service Bus from Linux at this time. Work to enable Proton-C with SSL on Windows is underway, so check back frequently for updates.

The following code shows how to send and receive messages from a Service Bus messaging entity.

The following code shows how to send a message to a Service Bus messaging entity.

$messenger = new Messenger();
$message = new Message();
$message->address = "amqps://[username]:[password]@[namespace].servicebus.windows.net/[entity]";

$message->body = "This is a text string";
$messenger->put($message);
$messenger->send();

The following code shows how to receive a message from a Service Bus messaging entity.

$messenger = new Messenger();
$address = "amqps://[username]:[password]@[namespace].servicebus.windows.net/[entity]";
$messenger->subscribe($address);

$messenger->start();
$messenger->recv(1);

if($messenger->incoming())
{
   $message = new Message();
   $messenger->get($message);      
}

$messenger->stop();

Proton-PHP messages support application properties of the following types: integer, double, Boolean, string, and object. The following PHP code shows how to set properties on a message by using each of these property types.

$message->properties["TestInt"] = 1;    
$message->properties["TestDouble"] = 1.5;      
$message->properties["TestBoolean"] = False;
$message->properties["TestString"] = "Service Bus";    
$message->properties["TestObject"] = new UUID("1234123412341234");   

In the Service Bus .NET API, message application properties are carried in the Properties collection of BrokeredMessage. The following code shows how to read the application properties of a message received from a PHP client.

if (message.Properties.Keys.Count > 0)
{
  foreach (string name in message.Properties.Keys)
  {
    Object value = message.Properties[name];
    Console.WriteLine(name + ": " + value + " (" + value.GetType() + ")" );
  }
  Console.WriteLine();
}if (message.Properties.Keys.Count > 0)
{
foreach (string name in message.Properties.Keys)
{
  Object value = message.Properties[name];
  Console.WriteLine(name + ": " + value + " (" + value.GetType() + ")" );
}
Console.WriteLine();
}

The following table maps the PHP property types to the .NET property types.

 

PHP Property Type .NET Property Type

integer

int

double

double

boolean

bool

string

string

object

Object

The BrokeredMessage type supports application properties of the following types: byte, sbyte, char, short, ushort, int, uint, long, ulong, float, double, decimal, bool, Guid, string, Uri, DateTime, DateTimeOffset, and TimeSpan. The following .NET code shows how to set properties on a BrokeredMessage object using each of these property types.

message.Properties["TestByte"] = (byte)128;
message.Properties["TestSbyte"] = (sbyte)-22;
message.Properties["TestChar"] = (char) 'X';
message.Properties["TestShort"] = (short)-12345;
message.Properties["TestUshort"] = (ushort)12345;
message.Properties["TestInt"] = (int)-100;
message.Properties["TestUint"] = (uint)100;
message.Properties["TestLong"] = (long)-12345;
message.Properties["TestUlong"] = (ulong)12345;
message.Properties["TestFloat"] = (float)3.14159;
message.Properties["TestDouble"] = (double)3.14159;
message.Properties["TestDecimal"] = (decimal)3.14159;
message.Properties["TestBoolean"] = true;
message.Properties["TestGuid"] = Guid.NewGuid();
message.Properties["TestString"] = "Service Bus";
message.Properties["TestUri"] = new Uri("http://www.bing.com");
message.Properties["TestDateTime"] = DateTime.Now;
message.Properties["TestDateTimeOffSet"] = DateTimeOffset.Now;
message.Properties["TestTimeSpan"] = TimeSpan.FromMinutes(60);

The following PHP code shows how to read the application properties of a message received from a Service Bus .NET client.

if ($message->properties != null)
{
  foreach($message->properties as $key => $value)
  {
    printf("-- %s : %s (%s) \n", $key, $value, gettype($value));                       
  }         
}

The following table maps the .NET property types to the PHP property types.

 

.NET Property Type PHP Property Type Notes

byte

integer

sbyte

integer

char

Char

Proton-PHP class

short

integer

ushort

integer

int

integer

uint

Integer

long

integer

ulong

integer

float

double

double

double

decimal

string

Decimal is not currently supported with Proton.

bool

boolean

Guid

UUID

Proton-PHP class

string

string

DateTime

integer

DateTimeOffset

DescribedType

DateTimeOffset.UtcTicks mapped to AMQP type:

<type name=”datetime-offset” class=restricted source=”long”>

<descriptor name=”com.microsoft:datetime-offset” />

</type>

TimeSpan

DescribedType

Timespan.Ticks mapped to AMQP type:

<type name=”timespan” class=restricted source=”long”>

<descriptor name=”com.microsoft:timespan” />

</type>

Uri

DescribedType

Uri.AbsoluteUri mapped to AMQP type:

<type name=”uri” class=restricted source=”string”>

<descriptor name=”com.microsoft:uri” />

</type>

The following tables show the mapping between the Proton-PHP standard message properties and the BrokeredMessage standard message properties.

 

Proton-PHP Service Bus .NET Notes

Durable

n/a

The Service Bus only supports durable messages.

Priority

n/a

The Service Bus only supports a single message priority.

Ttl

Message.TimeToLive

Conversion, Proton-PHP TTL is defined in milliseconds.

first_acquirer

delivery_count

Id

Message.Id

user_id

Address

Message.To

Subject

Message.Label

reply_to

Message.ReplyTo

correlation_id

Message.CorrelationId

content_type

Message.ContentType

content_encoding

n/a

expiry_time

Message.ExpiresAtUTC

creation_time

n/a

group_id

Message.SessionId

group_sequence

reply_to_group_id

Message.ReplyToSessionId

Format

n/a

 

Service Bus .NET Proton-PHP Notes

ContentType

Message->content_type

CorrelationId

Message->correlation_id

EnqueuedTimeUtc

Message->annotations[x-opt-enqueued-time]

Label

Message->subject

MessageId

Message->id

ReplyTo

Message->reply_to

ReplyToSessionId

Message->reply_to_group_id

ScheduledEnqueueTimeUtc

Message->annotations [“x-opt-scheduled-enqueue-time”]

SessionId

Message->group_id

TimeToLive

Message->ttl

Conversion, Proton-PHP TTL is defined in milliseconds.

To

Message->address

Czy oceniasz te materiały jako pomocne?
(Pozostało znaków: 1500)
Dziękujemy za opinię.
Pokaż:
© 2014 Microsoft