Reader small image

You're reading from  Mastering PHP 7

Product typeBook
Published inJun 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781785882814
Edition1st Edition
Languages
Right arrow
Author (1)
Branko Ajzele
Branko Ajzele
author image
Branko Ajzele

Branko Ajzele is a respected and highly accomplished software developer, book author, solution specialist, consultant, and team leader. He currently works for Interactive Web Solutions Ltd (iWeb), where he holds the role of senior developer and is the director of iWeb's Croatia office. Branko holds several respected IT certifications, including Zend Certified PHP Engineer, Magento Certified Developer, Magento Certified Developer Plus, Magento Certified Solution Specialist, Magento 2 Certified Solution Specialist, Magento 2 Certified Professional Developer, to mention just a few. He was crowned the e-commerce Developer of the Year by the Digital Entrepreneur Awards in October 2014 for his excellent knowledge and expertise in e-commerce development.
Read more about Branko Ajzele

Right arrow

Chapter 11. Building Services

A great deal of modern applications use HTTP (Hypertext Transfer Protocol) nowadays. This stateless, application-layer protocol allows us to exchange messages between distributed systems. The message exchange process can be observed through a client-server computing model as it happens in the form of the request-response type of messages. This allows us to easily write a service, or web service to be more specific, that triggers various operations on server and feedback data back to the client.

In this chapter, we will take a closer look at this client-server relationship through the following sections:

  • Understanding the client-server relationship
  • Working with SOAP:
    • XML extensions
    • Creating server
    • Creating WSDL file
    • Creating client
  • Working with REST:
    • JSON extensions
    • Creating server
    • Creating client
  • Working with Apache Thrift (RPC):
    • Installing Apache Thrift
    • Defining service
    • Creating server
    • Creating client
  • Understanding microservices

Understanding the client-server relationship


To easily visualize the client-server relationship and the request-response type of messaging, we can think of a mobile currency application acting as a client and some remote website, such as http://api.fixer.io/, being the server. The server exposes one or more URL endpoints, allowing communication exchange, such as http://api.fixer.io/latest?symbols=USD,GBP. The mobile application can easily issue a HTTP GET http://api.fixer.io/latest?symbols=GBP,HRK,USD request, which then results in a response like this:

{
  "base": "EUR",
  "date": "2017-03-10",
  "rates": {
    "GBP": 0.8725,
    "HRK": 7.419,
    "USD": 1.0606
  }
}

The HTTP GET keyword is used to denote the type of operation we want to perform on the receiver located on the remote (server) system that we contact via URL. The response contains JSON-formatted data, which our mobile currency application can easily digest and make use of. This specific message exchange example is what we flag...

Working with SOAP


SOAP (Simple Object Access Protocol) is an XML-based message exchange protocol that relies on application layer protocols such as HTTP for message negotiation and transmission. The World Wide Web Consortium (W3C) maintains SOAP specification.

Note

The SOAP specifications document is available at https://www.w3.org/TR/soap/.

The SOAP message is an XML document comprised of Envelope, Header, Body, and Fault elements:

<?xml version="1.0" ?>
<env:Envelope>
<env:Header>
<!-- ... -->
    </env:Header>
<env:Body>
<!-- ... -->
        <env:Fault>
<!-- ... -->
        </env:Fault>
</env:Body>
</env:Envelope>

Envelope is a required element of each SOAP request, as it envelops an entire SOAP message. Similarly, the Body element is also required as it contains request and response information.  Header and Fault, on the other hand, are optional elements. Using merely XML-based request-response messages, we can establish...

Working with REST


Unlike SOAP, REST is an architectural style. It has no protocols or standards of its own. It relies on URLs and HTTP verbs, such as POST, GET, PUT, and DELETE, in order to establish a message exchange process. The lack of standard makes it somewhat challenging to talk about, as various REST service implementations may present a client with different ways to consume services. When it comes to juggling data back and forth, we are free to choose over JSON, XML, or any other format we prefer. The simplicity and lightweightness of JSON made it a popular choice among many users and frameworks.

Loosely speaking, the very act of opening a web page in the browser can be interpreted as a REST call, where the browser acts as a client and server acts as a REST service. Unlike browser pages that may involve cookies and sessions, REST relies on stateless operations.

Moving forward, we will assume that our web server is configured to serve content of the rest-service/server directory for...

Working with Apache Thrift (RPC)


Apache Thrift is an open source framework to build scalable cross-language services. It was originally developed by Facebook, then entered the Apache Incubator around May 2008. Simplicity, transparency, consistency, and performance are the four key values behind the framework.

Unlike the REST and SOAP type of services, Thrift services use a binary form of communication. Luckily for us, Thrift provides a code generation engine to get us started. The code generation engine can pick up any interface definition language (IDL) file and generate PHP or other language bindings from it.

Before we start writing our first service definition, we need to install Apache Thrift.

Installing Apache Thrift

Apache Thrift can be installed from source files. Assuming that we have a fresh Ubuntu 16.10 installation, we can kick off the Apache Thrift installation steps using the following set of commands:

sudo apt-get update
sudo apt-get -y install php automake bison flex g++ git libboost...

Understanding microservices


The term microservices denotes an architectural style of building applications taking the form of loosely coupled services. These independently deployable services are tiny applications most often built via a web service technology. While one service can communicate via SOAP, the other can be do so via REST, Apache Thrift, or something else. There is no standard to specify the firm requirements here. The general idea is to take a large monolithic application and slice it down into several smaller applications, that is, services, but doing so in a manner that serves a business goal.

The following diagram tries to visualize this concept:

Popularized by the likes of Netflix and Amazon, the microservices style sets out to solve a few key challenges of modern application development, some of which include the following:

  • Development team size: This is a single microservice that can be developed by a relatively small team
  • Diversity of development skills: These are different...

Summary


Throughout this chapter, we took a look at two of the most common and well-established web services: SOAP and REST. We also looked into a rising new star called Apache Thrift. Once we pass the barrier of initial Apache Thrift installation and setup, features such as simplicity, scalability, speed, and portability certainly come into focus. As we saw in our client example, the RPC calls can easily be implemented with a central code repository--the thrift-service/gen-php/ directory in our case.

While Apache Thrift has yet to catch up in terms of popularity, the fact that it is being used by the likes of Facebook, Evernote, Pinterest, Quora, Uber, and other well-known names, certainly speaks for itself. This is not to say that future-wise SOAP or REST are less important. Choosing the right type of service is a matter of careful planning and forward thinking.

Finally, we glossed over some of the key points of an emerging a new architectural style called microservices.

Moving forward, we...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering PHP 7
Published in: Jun 2017Publisher: PacktISBN-13: 9781785882814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Branko Ajzele

Branko Ajzele is a respected and highly accomplished software developer, book author, solution specialist, consultant, and team leader. He currently works for Interactive Web Solutions Ltd (iWeb), where he holds the role of senior developer and is the director of iWeb's Croatia office. Branko holds several respected IT certifications, including Zend Certified PHP Engineer, Magento Certified Developer, Magento Certified Developer Plus, Magento Certified Solution Specialist, Magento 2 Certified Solution Specialist, Magento 2 Certified Professional Developer, to mention just a few. He was crowned the e-commerce Developer of the Year by the Digital Entrepreneur Awards in October 2014 for his excellent knowledge and expertise in e-commerce development.
Read more about Branko Ajzele