Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 20 ■ ADVANCED WEB SERVICES^311

Listing 20-12. SOAP Client for Transmitting Binary Data (photoclient.php)

<?php

$client = new SoapClient('photo.wsdl', array('trace'=>true));

$fileData = base64_encode(file_get_contents('test.png'));
echo "Input: ";
echo md5_file('test.png');

echo " Output: ";
echo $client->AddPhoto($fileData) ."\n";

Input: cd5ea9e8a94a571508161cd606a72a4b Output: cd5ea9e8a94a571508161cd606a72a4b

You will notice that the input md5_file signature matches the return value from the service.
The matching of these two hash codes indicates that the service is working correctly and that
the file sent was the file received.

Just the Facts


This chapter covered some advanced web service concepts, including complex types, authen-
tication, sessions, persistence, and binary data transmission.
Complex types are the WSDL equivalent of objects. You can define complex types and use
them to encode PHP objects. You can also map PHP objects to the SOAP extension for auto-
matic serialization and deserialization.
Methods for handling authentication in your web services include HTTP, keys, and certif-
icates. In the next chapter, you will learn how to implement client-side certificate authentication
and bring your API to the next level of security.
You can use session cookies with web services, just as with regular web applications. To
use the same session ID with multiple clients, you can get and set the session ID manually
using SoapClient.
Another way to track information between web service calls is through persistence. You
can use the SoapServer method setPersistence() to turn on persistence mode.
You can transmit binary data, such as image files, using web services by encoding the files
using base64.

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

Free download pdf