Developing SOAP and RPC Web Services with PHP 5
In the constantly changing world of web development, PHP technology has proven to be versatile and practical. It had evolved from the simple scripting language originally created in 19951 to the full fledged enterprise grade web technology platform with vast amount of third party modules, extensive API library, support for object oriented development, namespaces, robust database access, and mush more. In this article I fill focus on development of web services with PHP. I will explore various toolkits that are available and discuss the best ways to create web services in your PHP applications.
1PHP was originally created by Rasmus Lerdorf in 1995
Brief History of PHP
Since the late nineties, PHP (hypertext processor) gained popularity because of its simplicity and specifics geared toward web development. Currently PHP is powering more then 20 million websites2 and web products including some of the most popular sites, such as Facebook, as well as, content management tools such as, Joomla or Wordpress. PHP is free software and is released under the PHP License (different from GNU license).
Due to its open source structure, PHP technology is a natural fit with other open source or free technologies used by many startups or small to medium businesses to develop and maintain their web presence and online businesses. Many sites run on what is known as LAMP — Linux, Apache, MySQL and PHP. Which means that the operation system is some flavor of Linux, web server is Apache, database is MySQL, and application server logic or dynamic portion of the site is handled by the PHP. The combination of these products represents the best free source solution to develop and host the web business with minimum upfront costs. PHP technology is scalable and able to handle millions of transactions and requests. However, because of its open source nature and lack of formal support, it is still not very popular among the large enterprises.
PHP has evolved dramatically from its roots, to becoming a fully object oriented language in its 5th edition. It is supported, managed and maintained by PHP group, has a very active developer community and is a de facto standard for most of the online hosting providers (many of whom in the past primarily used CGI to handle dynamic rendering of html).
In addition to object oriented aspects, the language includes plentitude of prebuilt functions, modules and libraries to help with any imaginable web development task. PHP includes build in database support, error and exception handling, advanced string manipulation library and powerful regular expression engine. Other standard features incude networking and email support, session handling, web services toolkits, and much more.
Web services standards SOAP, XML RPC and REST
There are many books and articles written about web services, and I also covered this technology in the article Enterprise Java Contract-First vs. Contract-Last Web Services. There are also several books specifically devoted to development and consumption of web services with PHP. For the purposes of this article I will briefly mention several standards for web services development and then dive into some actual implementation in PHP.
The three main methodologies for developing web services in PHP (as well as other languages) are SOAP, XML RPC and REST. SOAP or Simple Object Access Protocol is a w3c standard and very popular web service development methodology in the modern enterprise world. With SOAP xml documents and XML schema for communication between client and server are established and services are based on complex XML documents marshaling and un-marshaling (usually into native object structures on the service consumer and provider sides). SOAP is newer web service (WS) methodology and development with SOAP usually presumes "contract first" approach, where schema and communication xml documents are done first and any native code and logic development is done last.
XML RPC is relatively simpler way to develop web services than with SOAP, even though it is also based on the XML. With RPC or Remote Procedure Call, specific methods are invoked on the server from the client and results are usually function return variables. RPC WS are usually a quick and easy way to add web services to legacy applications (because logic to be executed is already developed), and a way to do contract last services.
The third methodology to develop web services is REST (Representational state transfer), which like SOAP involves HTTP requests to the server and processing XML document returned in response. However, unlike SOAP, there is no concrete standard and no definition on how data should be used with REST. REST and RPC are older methodologies compared with SOAP. In PHP, support for SOAP was added in version 5, but support for REST and RPC existed prior with third-party toolkits.