Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^306) CHAPTER 20 ■ ADVANCED WEB SERVICES
Listing 20-5. HTTP Authentication in Web Services
$client = new SoapClient(
'service.wsdl',
array(
'login'=>'username',
'password'=>'secret'
)
);


Communicated-Key Authentication


For the communicated-key authentication method, you add a subscriber ID token to every
method in your web service. This is the type of authentication used by most web services.
You saw this type of authentication in the example of using the Amazon Web Services API
in the previous chapter. Your AWSAccessKeyId is your subscriber ID token, which is handled just
like any other parameter to your web service.

Client-Certificate Authentication.


The client-certificate authentication mechanism involves using SSL certificates on both the
server and the client. This is the most secure form of web service authentication, but is also
very complicated to set up.
The certificate authentication options you need to know are local_cert and passphrase to
provide client certificates encoded as Privacy Enhanced Mail (PEM). If you’re not familiar with
administering certificates, no worries—I’ll cover the topic extensively in the next chapter.

Sessions


Using sessions in your web services is no different from using sessions in a regular web appli-
cation. The SoapClient class will set and track a cookie, which will maintain the session ID. This
automatic handling can be thought of as an until-object-destruction cookie. However, some
sessions are long-lived, lasting hours or even days, and you may be expected to use the same
session ID with each request. To do this, you will need to retrieve the session ID from the
SoapClient and store it somewhere for later use. Using session cookies with SoapClient is
demonstrated in Listings 20-6 and 20-7, which shows the client and server, respectively. This
example reuses the demo.wsdl file from Listing 19-1 in the previous chapter.

Listing 20-6. Multiple SOAP Clients, Same Session (client.php)

<?php

//Normal session calls, first call creates session
$client = new SoapClient('demo.wsdl');
echo $client->demo('a'); //1
echo $client->demo('a'); //2

McArthur_819-9.book Page 306 Friday, February 29, 2008 8:03 AM

Free download pdf