ZiBaT => Peter Levinsky => Programming => exercise

PHP Pages
Consuming Web services

Updated : 2015-11-12


PHP consuming web-services

Mission:

From a PHP page consume and display a web-service.

Background:

Exercise

A) Install wsdl2php tool

This tool helps you to consume web services in a PHP page.
(a little bit like add service refence inVisualStudio)

I.e. 'Install' the wsdl2php tool : http://sourceforge.net/projects/wsdl2php/ (same as in Ebbe's tutorial)
download + unpack compressed file -> place the uncompressed folder somewhere reasonable - Remember location.

B) Find a web service to consume

Find a web service to consume e.g. from the old exercise comsuming existing webservices or webservices from the schoolclass 3R

Make the stub for php from a wsdl-file

In a 'console window' run to made the stub for concurrency converter be in the folder of your wsdl2php tool (e.g. ::some-root-folder::\wsdl2php-0.2.1-pear\wsdl2php-0.2.1\bin):

php wsdl2php.php http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

You need to add the path to php in your environment variable (Or use full path like: ::XAMPP-PATH::\XAMPP\php\php wsdl2php 'wsdl-file')

This will generate a PHP file (named CurrencyConvertor.php), which you have to move (or copy) to your php project, where to consume this web-service.

C) Use the stub in a php page

To use this newly created php-file you need to 'include' it in your page i.e. require_once './CurrencyConvertor.php';

Create an object (like in c#) to the stub i.e. $currencyConverter = new CurrencyConvertor;

Set up parameters to used in the web-service call e.g.

$conversionRate = new ConversionRate;
$conversionRate->FromCurrency = Currency::EUR;
$conversionRate->ToCurrency = Currency::DKK;

call the web service i.e.

$rate = $currencyConverter->ConversionRate($conversionRate);

Now $rate will hold the result of the call.

D) Make a page to use the currency converter

Make a page like in Ebbes tutorial to convert some amount from some currency to Danish Crowns (DKK)

E) Improve this page

Refactor the page to both have a from currency and a to currency

F) try anorther web service.

Try with another web service (see in exercise B) or one of your own web services

Extra A) Refactor your solution

Refactor your solution to encapsulate the web-service-stub call in php-functions.

Extra B) Try a web service that use user definded classes as parameters