To do some automated billing I needed to grab an email attachment off the exchange server. The Zend framework made it very easy to connect to the mail server and navigate files, and that part of the code came directly from the Zend Framework documentation. The issue that took the longest was saving the attachment. I could not figure out how to decode the attachment using the Zend framework. Let me know if you have a better way or if it helps you out.
NOTE: If you getting the mail from the INBOX you do not have to use the getFolders() function.
// Connecting with Imap
$mail = new Zend_Mail_Storage_Imap(
array('host' => 'SERVER',
'user' => 'USERNAME',
'password' => 'PASSWORD'));
// Navigate to desired folder
$folder = $mail->getFolders()->INBOX->Info;
// Change to folder
$mail->selectFolder($folder);
// Loop through messages
foreach ($mail as $message)
{
// Find desired message subject
if($message->subject == 'SUBJECT')
{
// Check for attachment
if($message->isMultipart())
{
$part = $message->getPart(2);
}
// Get the attacment file name
$fileName = $part->getHeader('content-description');
// Get the attachement and decode
$attachment = base64_decode($part->getContent());
// Save the attachment
$fh = fopen($fileName, 'w');
fwrite($fh, $attachment);
fclose($fh);
}
}
Categories: PHP · Zend Framework
PHPs SimpleXML is great for parsing xml files. The downside is that it does not return the root node name. I wrote this function that gets the name.
/**
* function getXMLRootNode
* @param string An xml string
* @return string Return XML root node name
*/
function getXMLRootNode($xmlstr)
{
// Create DOM model
$doc = new DOMDocument();
// Load the XML string
if(!$doc->loadXML($xmlstr))
{
throw new Exception('Unable to parse XML string');
}
// Find the root tag name
$root = $doc->documentElement;
if(!isset($root))
{
throw new Exception('Unable to find XML root node');
}
if(!isset($root->nodeName))
{
throw new Exception('Unable to find XML root node name');
}
return $root->nodeName;
}
Categories: PHP · Programming
I love the fact that you can autoload classes using the ZF. I was a little shocked at the depreciation errors when I updated to ZF 1.8. Apparently, there is a new way to use the autoload class. It took a little reading but I came up with this solution. Let me know what you think!
/*
* Set up Zend loader
*/
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
/*
* Register Cisc Classes
*/
$autoloader->registerNamespace('Cisc_');
The registerNamespace function registers my own classes.
Categories: PHP
Tagged: Zend Framework
I decide to try my hand at creating a soap server using the Zend Framework. This was an exersice in getting me more familiar with ZF and web services. Hopefully this will help others. I think the code is pretty self explanitory but feel free to ask questions or provide comments.
Basically the WSDL is auto generated by php if the URL ends in ?wsdl, if not then the soap server is created and the request handled.
/*
* Check to see if soap call is to be handled
* or if the WSDL should be displayed
*/
if(isset($_GET['wsdl']))
{
createWSDL();
}
else
{
/*
* Soap Server WSDL document
*/
$wsdl = 'http://' . $_SERVER["SERVER_NAME"] .
$_SERVER['PHP_SELF'] . '?wsdl';
/*
* Create a new soap server
*/
$server = new Zend_Soap_Server($wsdl,
array('soap_version' => SOAP_1_2)
);
/*
* Set the server to cache the wsdl file
*/
$server->setWsdlCache(1);
/*
* Set the class for the web service
*/
$server->setClass('Cisc_Ws_Cs3webservice');
/*
* Set soap server persistance (For use with login feature)
*/
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
/*
* Register exceptions
*/
$server->registerFaultException('Exception');
/*
* Handle the soap call
*/
$server->handle();
}
/**
* Create the WSDL file for display
*
* @param none
* @return string The wsdl xml
*/
function createWSDL()
{
$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setClass('Cisc_Ws_Cs3webservice');
$wsdl->handle();
}
The WSDL is created from the class file, so make sure you use proper documentation blocks for you code. The only issue I have seen so far is that if a function input in the class is a integer,
no matter what is inputted, an integer will be pass to the function. This makes it difficult to verify data, say that your getting an integer and not a float. The workaround is to pass an array
and check the values of the array.
Categories: PHP
Tagged: Zend Framework
I had the problem of when I added new events on my iPod Touch they would not show up in iCal after syncing. The quick fix for me was to open iSync, go to Preferences, then click the Reset Sync History… button. This resolved the issue for me. Hope this helps somebody else.
Categories: Hardware · Mac · Software
So I decided I would try out Zend Server CE. I have tried the installation on a server running Windows 2003 and XP Professional with PHP already installed using ISAPI. The install goes fine, but when I try to access the web page I get an FastCGI Access Denied error.
I have tried everything I can think of, including the suggestions on the web for changing permissions on several directories, but I still cannot resolve this issue. I am going to try and get it working but if anybody has any suggestions please let me know.
UPDATE: I was finally able to get Zend Sever installed. I had to remove IIS using add/remove programs, re-install it, then install Zend Server. I like it a lot.
Categories: PHP · Programming
I found a nice simple way to do cross domain ajax calls reliably. I found this Yahoo page that gives a nice little tutorial on how to use php and curl to access other domains using a simple proxy. Simple and effective. The code that I tweeked for my own use is found here. Let me know if you have any comments or suggestions.
Categories: PHP · Programming
Although I love my Mac, I have to use Windows at work. On my Mac I love Adium it lets you connect to several IM services at one time and has some other nice features. I really did not like the latest release of AIM for windows so I went on a search for a new client. I tried Trillian but I really did not like that either. Then I found Digsby. This program rocks! Not only does it let you connect to several IM services, you can check your AOL or Yahoo mail, check your Twitter feed and Facebook feed. There working on a Mac version which I will definately be checking out. Check it out and let me know what you think.
Categories: Fun Stuff
I was looking through the iTunes music store to see if there were any podcasts I might be interested in. I came across the Apple Quick Tips video podcast section. They are short videos focused on making you life with your Mac a little easier. Some you may already know, some maybe not. I subscribed to the podcast because I’m into making my life easier. You can also view them on Apples site.
Let me know if they help you or not and if you have any additional suggestions.
Categories: Uncategorized
Found a nice tip today. If you watch or listen to podcasts, sometimes you want to jump ahead to say skip commercials or parts you don’t want to listen to or watch try this. Hold down the ctrl+option+command (Apple Key) and then press the right arrow. This will skip ahead 5 seconds.
Let me know of you have any other iTunes tips or tricks.
Categories: Mac