Category → php

Using PHP with SQL Azure

SQL Azure is a wonderful service running the database in the cloud. This post summarizes the gotchas that you might wanna lookout for when trying to connect your PHP applications to talk to SQL Azure.

  1. The default driver php_mssql.dll doesn’t work with SQL Azure. The Internets say that support for php_mssql is dropped from PHP 5.3. I have PHP 5.2.10 and was a bit surprised when I can connect to any SQL server instance in the network (2005/2008) using php_mssql but not to SQL Azure. The all too generic Unable to connect to server message was also not helping. You have to use the new MSSQL driver for PHP.
  2. After you download and unzip the new driver you’ll see a set of DLLs with different versions. For PHP 5.2.10 you have to use php_sqlsrv_52_ts_vc6.dll. Rename it as php_sqlsrv.dll and copy it to the <PHP>\ext directory.
  3. Then add extension=php_sqlsrv.dll to your php.ini
  4. After that, you have to connect to the SQL Azure database with MultipleActiveResultSets option turned off.
    sqlsrv_connect( "server_name",
        array( "MultipleActiveResultSets" => 0, ... ) );
    

Voila! Now you’re all set to pull data from the clouds!

RESTful PHP Web Services

If you’ve read the architectural notes about RESTful applications and want to get your hands dirty writing one quickly, RESTful PHP Web Services by Samisa Abeysinghe is a book you should read. It starts with an introduction to what REST is and surrounding technologies. Since many web applications today expose their functions as RESTful APIs, you’ll be better prepared to play around with these after reading this book. Also a simple application that explains all the necessary functions for developing a RESTful application is discussed.

Introduction gives the reader a head start about what REST is all about and tools needed to play with RESTful web services. Then PHP libraries and function used for talking to a REST service is explained. How CURL is used to send requests with various HTTP verbs such as GET, POST, PUT, DELETE are explained with examples. After getting an XML response from a service, PHP libraries for manipulating XML and extracting parts of data you want is illustrated with samples. One could directly copy and paste these codes and try it out as they read along. Then the author goes on to explain how to use Flickr with PHP using CURL. If you’ve been using a high level library that insulate the lower level details, this example shows some of the basic API calls and how to use them.

After explaining a mashup of BBC news feed and Yahoo search API, author explains how to design RESTful services. This is carried out along with the simple but complete example of a library system. How to provide a service as well as consuming that using PHP and CURL is explained with complete code samples. Then he explains how to design the same thing using the Zend Framework.

The book concludes with a chapter dedicated to debugging REST web services. Using the TCPMon tool to capture messages and look for possible errors. The chapter ends with a set of best practices that everyone should be aware of if you do any programming with REST web services. The book also mentions WSF/PHP as an advanced framework providing many more functionality amidst of acting as a REST framework. I highly recommend this book if you’re a PHP programmer waiting to get your hands dirty designing RESTful applications. To whet your appetite here’s a sample chapter from the book about designing and implementing resource oriented clients. Enjoy!

Enterprise PHP

Samisa has written a nice post about using PHP in the enterprise. A commenter there argues that he don’t use PHP because it’s a dynamically typed language. It’s somewhat true that if you’re coming from a statically typed language background you’re stormed with so many questions and wonder possible nightmares of using one. Once you start learning a language and understand how programs are suppose to be written in that language most of these problems go away.

For all these years, it has only been Java/.Net programmers who has been enjoying the luxury of service oriented architectures. These folks understand the value of building a business functionality as a service or exposing an existing functionality as a service. PHP programmers have been building their web based programs happily without having to know what services mean. People in the PHP community has tried to bring Web services into the language with varying degrees of success. These are very courages and thoughtful efforts. But if you analyze them objectively you’ll see that many of these libraries doesn’t contain that much of features in order to build/integrate an application with an existing enterprise app.

What WSF/PHP has done is lower the bar of building PHP applications that can fit with existing applications in an enterprise written in Java/.Net or whatever the language that might be. Also, it has enabled ability to integrate existing PHP applications to your other enterprise applications.

One strong reason companies have embraced developing applications in Java is that so their data will not be isolated from rest of the enterprise data. In the past having a PHP application inside your company might have been frowned upon because it doesn’t make sense to have information which is isolated from all the other applications in the company. This is why most organizations invest in an ERP for example. But now, this has become a non issue. If you’re using open standards inside your enterprise to communicate/integrate applications, using WSF/PHP, you can integrate or extend services seamlessly with rest of your enterprise services.