|
PHPZendRestClientExample
Sample code for sending a message through Howlr using PHP's Zend_Rest_Client
Example IntroductionOne way to talk to Howlr in PHP is using the Zend_Rest_Client library. The client, part of the Zend_Framework, is an abstraction layer providing an object-oriented API for RESTful resources. The Zend_Framework is available for download from http://framework.zend.com/download. For general information on using the library, have a look at:
REST Client InitializationFirst, make sure that the Zend framework is installed somewhere in your include_path. Then at the top of your php script: <?php require 'Zend/Rest/Client.php'; $howlr_site_url = "http://localhost:7008"; ?> AuthenticationIf you have authentication enabled on your Howlr server, you will need to configure the Zend_Rest_Client client to authenticate: $username = 'howlr'; $password = 'howl!'; Zend_Rest_Client::getHttpClient()->setAuth(username, $password); Sending a Plain Text MessageOkay, now we're ready to send a message through Howlr: $rest = new Zend_Rest_Client($howlr_site_url);
$data = array(
'from' => "Test McTestski <tmctestski@example.foo>",
'recipients' => "John Doe <jdoe@example.foo>",
'subject' => "Testing Howlr",
'body' => "Did it work?"
);
$r = $rest->post('/messages.xml', $data);The post call returns a SimpleXml object with an XML-serialized copy of the message you just sent. When a message is sent out, a copy of it is temporarily stored on the server and can be later retrieved using a $rest->get('/messages/1.xml') call (replace the 1 with the id of the message you want to retrieve). You can examine the SimpleXml object returned by your post call to find out the id of the message you just sent. Sending an HTML MessageIt's also possible to send HTML-formatted messages. Just add a 'content_type' => 'text/html' parameter, and use HTML tags in your message's body: $rest = new Zend_Rest_Client($howlr_site_url);
$html = "
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>This is a test!</h1>
<p>Did it work?</p>
</body>
</html>
";
$data = array(
'from' => "Test McTestski <tmctestski@example.foo>",
'recipients' => "John Doe <jdoe@example.foo>",
'subject' => "Testing Howlr HTML",
'body' => $html,
'content_type' => 'text/html'
);
$r = $rest->post('/messages.xml', $data);
|