Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

PHP and Web Services

PHP is a great language for developing web services, and this chapter shows you just what a web service is and how it is made up.
This chapter is from the book

This chapter is from the book

Web services are one of the most talked-about technologies of the day. They are set to change how data is exchanged on the Internet as the Internet itself evolves to deliver content not only to web browsers on PCs but also to PDAs and other devices. Further still, the evolution of "Internet-ready" software and hardware will see web services being used in applications such as MP3 players, personal stereos, and game consoles. PHP is a great language for developing web services, and this chapter shows you just what a web service is and how it is made up. Then we will look at how you can use PHP to develop web services.

What Makes Up a Web Service?

A web service is made up of four parts, as shown in Figure 9.1. The first part is the component that wants to act as a web service. It can be any part of an application: the executable, a COM component, a JavaBean, and so on. The web service component exposes public methods and functions that other applications can query. Your component could be one that you have created for this purpose, but normally you can expose any component that has public methods as a web service.

Figure 9.1Figure 9.1 The makeup of web services.

You must allow your components to be accessed by other applications as a web service. To do this, you must allow other applications to see what public functions and methods your components have. You don't allow applications to do this directly. To accomplish this, you create a file based on an XML-based metalanguage called Web Services Description Language (WSDL).

Next you must allow applications to query the WSDL file and exchange data with the web service.

For this purpose, you use another XML-based metalanguage called Simple Object Access Protocol (SOAP).

Using SOAP, you look up the web service component's public methods and functions using the WSDL file and then query those methods and functions. However, you don't always know where to find a web service's WSDL file. In that case, you can look at an XML-based database (also called a registry) of WSDL addresses. This database is called a Uniform Description, Discovery, and Integration (UDDI) registry.

The easiest way to remember the component parts of a web service is to think in terms of these three concepts:

  • Discovery: UDDI lets you discover web services.

  • Query: WSDL lets you query web services.

  • Transport: SOAP lets you transmit those queries back and forth from the web service.

Don't worry if some of these terms are new to you. They are covered in further detail later in this chapter. Now that we have identified all the component parts of a web service, let's look at each in detail.

SOAP (Simple Object Access Protocol)

In Chapter 5, "PHP and Sessions," SOAP was mentioned briefly when we looked at how WDDX developed. SOAP, like WDDX, is an XML-based language. Unlike WDDX, however, and like another XML-based language, XML-RPC, it is used for RPC (Remote Procedure Call) via XML. XML-RPC started life as an idea of Dave Winer of UserLand software. He discussed his ideas with Microsoft, and from his ideas, SOAP was born. (XML-RPC continues to develop as a protocol separate from SOAP.)

Other companies (such as IBM) joined Microsoft in developing SOAP. Soon after that, implementations of SOAP for languages such as Java (via IBM) and Visual C++ and Visual Basic (via Microsoft) were released.

Using SOAP

So how is SOAP used? SOAP is an XML-based language, so in effect, all SOAP implementations do is create XML files or strings that facilitate passing data and calling methods between (normally remote) applications. SOAP does not have to be strictly about web services; in other words, it does not require WSDL or UDDI to work and can be used in a standard RPC manner. SOAP is often described as passing objects between applications; this can be both misleading and confusing. All SOAP does is allow a public function or method to be queried via an XML interface. It does not pass physical objects in the same way that Java serialization does, for example.

When an application queries a public function or another application's methods, it passes some data to that function or method and might get results in return. A public function or method does not always do this, but it is good practice that such functions or methods at least return some handshake data (a simple code that allows the calling application to see that the public function or method has received the data it sent).

When dealing with remote applications, you might face data type problems. That is, different languages have different ways of representing data. You might have some success on this front. For example, Java and PHP have very similar data types, but when dealing with calling applications that might be made up of languages such Perl, Python, and C++, you will face a nightmare.

If you imagine that your remote application is developed in Java and your calling clients are made up of PHP and Visual Basic, you might face few problems with the PHP application calling the remote Java application. But you will face a lot of problems when you do the same with a Visual Basic application.

Luckily, SOAP deals with this by presenting a standard way of representing data. Imagine that your remote Java application function accepts a string. Using SOAP, you translate the calling application data into the SOAP equivalent and pass that to the remote Java application. The Java application then translates the SOAP string data into the Java equivalent.

This can work in reverse too so that if your Java application returns a result, it does so as SOAP data for your client applications to translate back into native data types. So, in effect, SOAP does not really let you pass objects between applications. Instead, it provides the means to interface between different objects in different languages.

I was once asked if SOAP, based on part of its definition (simple object), is for simple objects only. Its meaning is not to be confused with objects in the sense we use them in OOP languages such as C++. It does not need to know what an object does, how it exists, or how it works. In fact, the very use of the word object can be misleading. SOAP does not require objects to exist in a true OOP sense. They can be nothing more than public functions and methods and are not subject to OOP concepts such as encapsulation.

SOAP Transparency

Because SOAP is XML-based, it is nothing more than ASCII data that is being transmitted (and therefore is as simple as standard text). This is one of key benefits of SOAP. It uses simple character encoding (ASCII) as a file format. It can be transmitted on any protocol that supports the transmission of ASCII data. As it happens, most TCP/IP protocols do. This allows SOAP to be used across HTTP (the most common protocol to be used with SOAP), FTP, SMTP, and so on. This brings an added benefit in that such protocols don't require special ports and security measures such as firewalls. They run through commonly used ports. (This advantage is also enjoyed by other XML-based RPC methods, such as XML-RPC.)

This property is also apparent when you compare SOAP to other RPC methods. In PHP, you can also make use of DCOM (which is used with COM), CORBA (which uses the IIOP protocol), and Java (which natively supports the RMI protocol). Protocols that facilitate RPC are called wire protocols because they are low-level and require special ports. SOAP, however, has no such requirements.

SOAP's Makeup

What exactly is SOAP made of? A SOAP message is called a SOAP envelope. The following code exemplifies a SOAP envelope:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

</SOAP-ENV:Envelope>

An envelope contains a body, which can be either a SOAP body call or a SOAP body response.

SOAP Body Call

When a call is made to a public function or method, this is done with a SOAP body call. Such a body call looks something like this:

<SOAP-ENV:Body>
<SOAPSDK1:HelloFunc xmlns:SOAPSDK1="http://tempuri.org/message/">
<uname xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK3="http://www.w3.org/2001/XMLSchema"
 SOAPSDK2:type="SOAPSDK3:string">Andrew</uname>
</SOAPSDK1:HelloFunc>
</SOAP-ENV:Body>

Here the HelloFunc method is passed a string of data called "Andrew". Note that SOAP has added a mapping of what data we are passing to the public service or method:

type="SOAPSDK3:string"

A completed SOAP envelope calling a public service or method looks like the following:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAPSDK1:HelloFunc xmlns:SOAPSDK1="http://tempuri.org/message/">
<uname xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK3="http://www.w3.org/2001/XMLSchema"
 SOAPSDK2:type="SOAPSDK3:string">Andrew</uname>
</SOAPSDK1:HelloFunc>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP Body Response

In response to a call to a public function or method, SOAP can respond to that call using the SOAP body response:

<SOAP-ENV:Body>
<SOAPSDK1:HelloFuncResponse xmlns:SOAPSDK1="http://tempuri.org/message/">
<Result xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK3="http://www.w3.org/2001/XMLSchema"
 SOAPSDK2:type="SOAPSDK3:string">hello Andrew</Result>
<uname xmlns:SOAPSDK4="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK5="http://www.w3.org/2001/XMLSchema"
 SOAPSDK4:type="SOAPSDK5:string">Andrew</uname>
</SOAPSDK1:HelloFuncResponse>#
</SOAP-ENV:Body>

The SOAP body result contains any data that the public function or method returns:

<Result xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK3="http://www.w3.org/2001/XMLSchema"
 SOAPSDK2:type="SOAPSDK3:string">hello Andrew</Result>

along with the original method call:

<uname xmlns:SOAPSDK4="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAPSDK5="http://www.w3.org/2001/XMLSchema"
 SOAPSDK4:type="SOAPSDK5:string">Andrew</uname>

Web Services Description Language (WSDL)

In the web services sense, although SOAP helps you exchange data between the public functions or methods of a web service, it can't help you explain which public functions or methods are available and what they do. Without this information, you can't use SOAP to exchange data, because you have no idea what is available to help you facilitate the exchange.

For this purpose, we have Web Services Description Language (WSDL). Like SOAP, WSDL is an XML-based file format for describing what public functions and methods are available in a web service. Other applications use the WSDL file to find this information and then use SOAP against those described public functions or methods.

WSDL File Makeup

<?xml version='1.0' encoding='UTF-8' ?>
 <!-- Generated 09/24/01 by Microsoft SOAP Toolkit WSDL File Generator,
 Version 1.02.813.0 -->
<definitions name ='PHP4WINSOAP'
  targetNamespace = 'http://tempuri.org/wsdl/'
   xmlns:wsdlns='http://tempuri.org/wsdl/'
   xmlns:typens='http://tempuri.org/type'
   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
   xmlns:xsd='http://www.w3.org/2001/XMLSchema'
   xmlns:stk='http://schemas.microsoft.com/soap-toolkit/wsdl-extension'
   xmlns='http://schemas.xmlsoap.org/wsdl/'>
 <types>
  <schema targetNamespace='http://tempuri.org/type'
   xmlns='http://www.w3.org/2001/XMLSchema'
   xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'
   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
   elementFormDefault='qualified'>
  </schema>
 </types>
 <message name='Examples.HelloFunc'>
  <part name='uname' type='xsd:anyType'/>
 </message>
 <message name='Examples.HelloFuncResponse'>
  <part name='Result' type='xsd:anyType'/>
  <part name='uname' type='xsd:anyType'/>
 </message>
 <portType name='ExamplesSoapPort'>
  <operation name='HelloFunc' parameterOrder='uname'>
   <input message='wsdlns:Examples.HelloFunc' />
   <output message='wsdlns:Examples.HelloFuncResponse' />
  </operation>
 </portType>
 <binding name='ExamplesSoapBinding' type='wsdlns:ExamplesSoapPort' >
  <stk:binding preferredEncoding='UTF-8'/>
  <soap:binding style='rpc' transport=
  'http://schemas.xmlsoap.org/soap/http' />
  <operation name='HelloFunc' >
   <soap:operation soapAction=
   'http://tempuri.org/action/Examples.HelloFunc' />
   <input>
    <soap:body use='encoded' namespace='http://tempuri.org/message/'
      encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
   </input>
   <output>
    <soap:body use='encoded' namespace='http://tempuri.org/message/'
      encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
   </output>
  </operation>
 </binding>
 <service name='PHP4WINSOAP' >
  <port name='ExamplesSoapPort' binding='wsdlns:ExamplesSoapBinding' >
   <soap:address location='http://localhost/phpbook/Chapter9
   _SOAP/SOAP/Server/PHP4WINSOAP.ASP' />
  </port>
 </service>
</definitions>

As you can see, the WSDL file format is quite a complicated one. The three most important pieces are the port type, binding, and service name.

Port Type

The port type defines which public functions or methods are available. It uses the full name, which in this case is the HelloFunc method of the Examples class:

<portType name='ExamplesSoapPort'>
 <operation name='HelloFunc' parameterOrder='uname'>
  <input message='wsdlns:Examples.HelloFunc' />
  <output message='wsdlns:Examples.HelloFuncResponse' />
 </operation>
</portType>
Binding

Binding describes how calls are made to each public method or function of a web service. In other words, it describes what encoding a client application should use when querying that public method or function.

<binding name='ExamplesSoapBinding' type='wsdlns:ExamplesSoapPort' >
  <stk:binding preferredEncoding='UTF-8'/>
  <soap:binding style='rpc'
   transport='http://schemas.xmlsoap.org/soap/http' />
  <operation name='HelloFunc' >
   <soap:operation soapAction=
   'http://tempuri.org/action/Examples.HelloFunc' />
   <input>
    <soap:body use='encoded' namespace='http://tempuri.org/message/'
      encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
   </input>
   <output>
    <soap:body use='encoded' namespace='http://tempuri.org/message/'
      encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
   </output>
  </operation>
 </binding>

This line describes what encoding will use of the binding:

<stk:binding preferredEncoding='UTF-8'/>

This must match the encoding for your XML file (the encoding specified in the XML header). Next you specify what the binding is (RPC) and its transport (HTTP):

<soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http' />

Next you specify what function you will call as Hello Func:

<operation name='HelloFunc' >
<soap:operation soapAction='http://tempuri.org/action/Examples.HelloFunc' />

The WSDL file describes the encoding you use to query the web service and obtain a result:

<input>
<soap:body use='encoded' namespace='http://tempuri.org/message/'
 encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
</input>
<output>
<soap:body use='encoded' namespace='http://tempuri.org/message/'
 encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' />
</output>

Although it's important to understand the structure of a WSDL file, you won't often need to write a WSDL file yourself. Most web service toolkits have tools for creating WSDL files for you.

Service Name

The service name defines where the WSDL gateway will be. A WSDL gateway is used to query the web service's component. A WSDL file passes the gateway information back to the client application. Such queries from the client application go to the gateway and on to the web service component and back.

<service name='PHP4WINSOAP' >
 <port name='ExamplesSoapPort' binding='wsdlns:ExamplesSoapBinding' >
  <soap:address location='http://localhost/phpbook/Chapter9
  SOAP/SOAP/Server/PHP4WINSOAP.ASP' />
 </port>
</service>

A gateway is language-independent. For instance, our example uses ASP, but the gateway can be developed in any language you like, as long as it supports XML. All it does is work in unison with the WSDL file to allow client applications to pass SOAP queries back and forth between the web service and the client application.

Uniform Description, Discovery, and Integration (UDDI)

Although you can now look up and query a web service's public methods and functions, finding a web service presents a problem. It can't be found using normal means such as a web search engine. If your company wants to make use of web services, it needs to make them generally available to all calling applications, not simply provide URLs on web pages for people to connect to.

Web services need a source of information all their own that an application can search and use to find WSDL files to query. UDDI was created for this purpose (see http://www.uddi.org). UDDI is another emerging standard for web services. It allows a company to register its company details, web page, and so on with a business group (for example, a car dealership could register with an automotive association). A person can then search the UDDI for companies within a business group or type via an application or directly via a web browser. More interestingly, UDDI allows companies to publish details of what web services are available (using brief descriptions and keywords) as well as the URLs of the WSDL files. An application can use this information to discover which web services a company has available.

Several public UDDI registries have been set up to allow companies to test and publish details of real-world web services. The Microsoft UDDI registry (http://uddi.microsoft.com), shown in Figure 9.2, serves such a purpose.

Figure 9.2Figure 9.2 Microsoft's UDDI web site.

Microsoft also provides a test UDDI registry at http://test.uddi.microsoft.com (see Figure 9.3). Other UDDI registries are available from IBM at http://www.ibm.com/developerworks/webservices/.

Figure 9.3Figure 9.3 Microsoft's UDDI web site for testing.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020