<?php

include 'bootstrap.php';
$messaging = new SoftLayer\Messaging();
if ($messaging->authenticate(QUEUE_ACCOUNT, QUEUE_USERNAME, QUEUE_API_KEY)) {
    echo "Welcome to the SoftLayer Message Queue!" . PHP_EOL;
}
<?php

include 'bootstrap.php';
$messaging = new SoftLayer\Messaging();
$messaging->authenticate(QUEUE_ACCOUNT, QUEUE_USERNAME, QUEUE_API_KEY);
// Much like creating a queue, all I need to provide for a topic is a name.
$topic = $messaging->topic('myFirstTopic')->create();
// To actually do something, topics need subscribers. I can add either
// HTTP and/or queue subscriptions.
// A queue must exist before creating a subscription which targets it.
$queue1 = $messaging->queue('myTargetQueue1')->create();
$queue2 = $messaging->queue('myTargetQueue2')->create();
// First I'll create an endpoint targeting each queue.
$queueEndpoint1 = SoftLayer\Messaging\Endpoint::endpointByType('queue');
$queueEndpoint1->setQueueName($queue1->getName());
$queueEndpoint2 = SoftLayer\Messaging\Endpoint::endpointByType('queue');
$queueEndpoint2->setQueueName($queue2->getName());
// Then I add them to the topic as individual subscriptions.
$topic->subscription()->setEndpointType('queue')->setEndpoint($queueEndpoint1)->create();
$topic->subscription()->setEndpointType('queue')->setEndpoint($queueEndpoint2)->create();
// Now the topic should tell me I have 2 subscribers, each targeting a
// different queue.
$subscriptions = $topic->subscriptions();
foreach ($subscriptions as $s) {
    echo $s->getId() . ' : ' . $s->getEndpointType() . ' : ' . $s->getEndpoint()->getQueueName() . PHP_EOL;
}
// Finally, I can push a message to the topic. That message will subsequently
// end up in both target queues.
$topic->message('Example Message 1')->create();
// The message needs a moment to be dispatched and visible.
sleep(10);
<?php

include 'bootstrap.php';
$messaging = new SoftLayer\Messaging();
$messaging->authenticate(QUEUE_ACCOUNT, QUEUE_USERNAME, QUEUE_API_KEY);
// I first need a queue to push my messages to.
$queue = $messaging->queue('myQueue')->create();
// Now I can start creating messages.
for ($i = 0; $i < 10; $i++) {
    $queue->message("Example Message {$i}")->create();
}
// Should have all 10 available after a tick.
sleep(1);
// By default messages() fetches a single message. It has an optional
// $batch parameter which allows me to fetch up to 100.
$messages = $queue->messages(10);
foreach ($messages as $m) {
    echo $m->getId() . ' : ' . $m->getBody() . PHP_EOL;
}
// Each of the message objects can delete itself once I complete
// my work.
foreach ($messages as $m) {
    echo "Deleting: " . $m->getId() . PHP_EOL;
    $m->delete();
}
// If I need to, I can delete a message by its ID (say, if I lose the
// object reference to it but still have the ID in a database somewhere).
$id = $messages[0]->getId();
// Of course this will throw a 404 exception as I've already deleted it.
try {
    $messaging->queue('myQueue')->message()->delete($id);
<?php

include 'bootstrap.php';
$messaging = new SoftLayer\Messaging();
$messaging->authenticate(QUEUE_ACCOUNT, QUEUE_USERNAME, QUEUE_API_KEY);
// I can create a queue very simply by providing a name.
$messaging->queue('myFirstQueue')->create();
// Or, I can set various attributes and then create it.
$messaging->queue('mySecondQueue')->setVisibilityInterval(20)->addTag('tag1')->addTag('tag2')->create();
// The queue() method returns a valid queue object with all
// connectivity and auth persisted, so I can do this as well:
$queue = $messaging->queue();
// Then use that object, even calling create() on it directly.
$queue->setName('myThirdQueue');
$queue->addTag('tag1');
$queue->create();
// I can also use a generic save() if I'm uncertain if the
// queue already exists.
$queue->save();
// Now that I have a few queues created, I can get a list of
// them.
$queues = $messaging->queues();
foreach ($queues as $q) {
    // Each of these $q objects are also fully functional,
    // so I can add another tag and save it here.
    $q->addTag('tag3');
    $q->update();
    echo $q->getName() . '(' . implode(',', $q->getTags()) . ')' . PHP_EOL;
}
// Finally, I can remove them by name.
$messaging->queue('myFirstQueue')->delete();