Archive

Archive for the ‘PHP’ Category

Save an email attachment using Zend Mail

June 10, 2009 Wes Leave a comment

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

Getting the root node from an XML string

June 3, 2009 Wes Leave a comment

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

New autoloading with Zend Framework 1.8

May 15, 2009 Wes 2 comments

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 Tags:

Creating a Soap Server using the Zend Framework

May 15, 2009 Wes Leave a comment

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 Tags:

Zend Server Community Edition Install

March 4, 2009 Wes 3 comments

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

Cross Domain Ajax

September 29, 2008 Wes Leave a comment

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