PHP is a widely-used general purpose scripting language that is
specially designed to work on the Web and very well suited for web
development. Although it is possible to develop Web Services in PHP we
will focus on the consumption of the Web Services using PHP language.
In the following section we will provide code snipets and full listings
of PHP code that can be used to consume
SOAP based web services.
You can install NuSOAP pretty much anywhere you want as long as PHP
system can find it. In our case we have include_path variable in
php.ini set to
include_path = ".;c:\php\includes"
and NuSOAP installed in the C:\PHP\Includes\NuSOAP folder.
NOTE: We will be using CDYNE's Phone
Notify! for all our examples.
In order to consume a webservice from PHP using NuSOAP you have to do 3
main steps:
Step 1 Create an instance of the soapclient class:
$client = new nusoap_client('http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx?wsdl', true, $proxyhost, $proxyport, $proxyusername, $proxypassword);
Step 2 Create parameters array for a method that you want to use, we
will be using NotifyPhoneEnglishBasic which has 3 parameters:
$param = array(
'PhoneNumberToDial' => '7777777777',
'TextToSay' => 'Hello, this is a test message using PHP and NuSOAP',
'LicenseKey' => ''''You have to use valid license here or empty string in demo mode''''
);
Step 3 Invoke the method:
$result = $client->call('NotifyPhoneEnglishBasic', array('parameters' => $param), '', '', false, true);
Here is the test form that we used for NotifyPhoneEnglishBasic
form1.php:
<form action="NotifyPhoneEnglishBasic.php" method="post" ENCTYPE="multipart/form-data">
Phone to Dial: <input type="edit" name="phonenumber"/>
Text to say: <input type="edit" name="textotsay"/>
License: <input type="edit" name="license"/>
<input type="submit" value="Make call"/>
</form>
NotifyPhoneEnglishBasic.php looks like this:
<?php
//make sure that 'nusoap/nusoap.php' is in the include path
require_once('nusoap/nusoap.php');
//Proxy setup
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
//Create wrapper for Web Service
$client = new soapclient('http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx?wsdl', true, $proxyhost, $proxyport, $proxyusername, $proxypassword);
$err = $client->getError();
if ($err) {
echo '<h2>Creation of the wrapper failed</h2><pre>' . $err . '</pre>';
}
//Lets wrap parameters for the call
$param = array(
'PhoneNumberToDial' => $_POST['phonenumber'],
'TextToSay' => $_POST['textotsay'],
'LicenseKey' => $_POST['license']
);
//Make a call
$result = $client->call('NotifyPhoneEnglishBasic', array('parameters' => $param), '', '', false, true);
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
?>
All Notify methods ( NotifyPhoneAdvanced, NotifyPhoneBasic,
NotifyPhoneBasicWithTransfer, NotifyPhoneBasicWithTryCount,
NotifyPhoneEnglishBasic ) return a result structure. That structure is
slightly different depending on the method you call, but it has some
common fileds. For example this is what is returned for
NotifyPhoneEnglishBasic:
Array
(
[NotifyPhoneEnglishBasicResult] => Array
(
[ResponseCode] => 0
[ResponseText] => Queued
[CallAnswered] => false
[QueueID] => 12778
[TryCount] => 0
[Demo] => true
[Duration] => 0
[StartTime] => 0001-01-01T00:00:00
[EndTime] => 0001-01-01T00:00:00
[MinuteRate] => 0
)
)
We are interested in QueueID field. That field is one of the fields
that present in the result structure for all Notify methods. QueueID
is a unique identifier of the call that you have made with the one of
the Notify methods. All Notify methods are asynchronous methods meaning
they return right away even if the call is still in progress. In order
to obtain the current status of the call you can use
GetQueueIDStatus method using the QueueID value from the result
of the Notify call:
...
//Make a call
$result = $client->call('NotifyPhoneEnglishBasic', array('parameters' => $param), '', '', false, true);
...
//Wrap parameters for GetQueueIDStatus call
$param = array('QueueID' => $result['NotifyPhoneEnglishBasicResult']['QueueID']);
//Get status
$status = $client->call('GetQueueIDStatus', array('parameters' => $param), '', '', false, true);
...
GetQueueIDStatus returns another structure:
Array
(
[GetQueueIDStatusResult] => Array
(
[ResponseCode] => 0
[ResponseText] => Queued
[CallAnswered] => false
[QueueID] => 12846
[TryCount] => 1
[Demo] => false
[MachineDetection] => UNKNOWN
[Duration] => 0
[StartTime] => 2006-08-10T19:27:32.378Z
[EndTime] => 0001-01-01T00:00:00
[MinuteRate] => 0
)
)
Because of the asynchronous nature of the Notify methods you will need
to call GetQueueIDStatus several times if you want to get confirmation
of the call completion and all information associated with the completed
call.
Test form form2.php:
<form action="UploadSoundFile.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload">
</form>
UploadSoundFile.php:
<?php
require_once('nusoap/nusoap.php');
$tempfile = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
//Create wrapper for Web Service
$client = new soapclient('http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx?wsdl', true, $proxyhost, $proxyport, $proxyusername, $proxypassword);
$err = $client->getError();
if ($err) {
echo '<h2>Creation of the wrapper failed</h2><pre>' . $err . '</pre>';
}
if(is_uploaded_file($tempfile))
{
//Get contents of the file
$handle = fopen($tempfile, "r");
$contents = fread($handle, filesize($tempfile));
fclose($handle);
//Encode for web service
$base64string = base64_encode($contents);
//Wrap parameters
$param = array(
'FileBinary' => $base64string,
'SoundFileID' => $filename,
'LicenseKey' => ''''Make sure you have valid license here, there is no demo mode for this method''''
);
//Make call
$result = $client->call('UploadSoundFile', array('parameters' => $param), '', '', false, true);
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
}
?>
As you can see PHP language has excellent toolbox that allows you to use
Web Services. Please feel free to add your comments and question on the
Talk page.
CDYNE Team.