Skip to content

Commit

Permalink
Improve examples and comments. Partly by Torsten Roehr.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/SOAP/trunk@262787 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Jan Schneider committed Jul 15, 2008
1 parent 06bbacd commit cae58e4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
78 changes: 53 additions & 25 deletions example/client.php
Expand Up @@ -28,82 +28,110 @@
* does not use WSDL to run these requests, but that can be changed easily by
* simply adding '?wsdl' to the end of the url. */
$soapclient = new SOAP_Client('http://localhost/SOAP/example/server.php');
// This namespace is the same as declared in server.php.
$options = array('namespace' => 'urn:SOAP_Example_Server',
'trace' => true);

/* Set a few options. */
$options = array();

/* This namespace is the same as declared in server.php. */
$options['namespace'] = 'urn:SOAP_Example_Server';

/* Trace the communication for debugging purposes, so we can later inspect the
* data with getWire(). */
$options['trace'] = true;

/* Uncomment the following lines if you want to use Basic HTTP
* Authentication. */
// $options['user'] = 'username';
// $options['pass'] = 'password';

header('Content-Type: text/plain');

/* Calling echoStringSimple. */
$ret = $soapclient->call('echoStringSimple',
$params = array('inputStringSimple' => 'this is a test string'),
array('inputStringSimple' => 'this is a test string'),
$options);
// print $soapclient->getWire();
// echo $soapclient->getWire();
print_r($ret);
echo "\n";

/* Calling echoString. */
$ret = $soapclient->call('echoString',
$params = array('inputString' => 'this is a test string'),
array('inputString' => 'this is a test string'),
$options);
// echo $soapclient->getWire();
print_r($ret);
echo "\n";

/* Calling divide with valid parameters. */
$ret = $soapclient->call('divide',
$params = array('dividend' => 22, 'divisor' => 7),
array('dividend' => 22, 'divisor' => 7),
$options);
// print $soapclient->getWire();
// echo $soapclient->getWire();
if (PEAR::isError($ret)) {
echo 'Error: ' . $ret->getMessage() . "\n";
echo 'Error: ' . $ret->getMessage();
} else {
echo 'Quotient is ' . $ret . "\n";
echo 'Quotient is ' . $ret;
}
echo "\n";

/* Calling divide with invalid parameters. */
$ret = $soapclient->call('divide',
$params = array('dividend' => 22, 'divisor' => 0),
array('dividend' => 22, 'divisor' => 0),
$options);
// echo $soapclient->getWire();
if (PEAR::isError($ret)) {
echo 'Error: ' . $ret->getMessage() . "\n";
echo 'Error: ' . $ret->getMessage();
} else {
echo 'Quotient is ' . $ret . "\n";
echo 'Quotient is ' . $ret;
}
echo "\n";


// SOAPStruct is defined in the following file.
/* The SOAPStruct class is defined in example_types.php. */
require_once 'example_types.php';

$struct = new SOAPStruct('test string', 123, 123.123);

/* Send an object, get an object back. Tell the client to translate to classes
* we provide if possible. */
$soapclient->_auto_translation = true;

/* You can explicitly set the translation for a specific
* class. auto_translation works for all cases, but opens ANY class in the
* script to be used as a data type, and may not be desireable. Both can be
* used on client or server. */
$soapclient->setTypeTranslation('{http://soapinterop.org/xsd}SOAPStruct',
'SOAPStruct');

/* Calling echoStruct. */
$ret = $soapclient->call('echoStruct',
$p = array('inputStruct' => $struct->__to_soap()),
array('inputStruct' => $struct->__to_soap()),
$options);
// print $soapclient->getWire();
// echo $soapclient->getWire();
print_r($ret);

/* PHP doesn't support multiple OUT parameters in function calls, so we must
* do a little work to make it happen here. This requires knowledge on the
* developers part to figure out how they want to deal with it. */
/* Calling echoStructAsSimpleTypes.
* PHP doesn't support multiple return values in function calls, so we must do
* a little work to make it happen here, for example returning an array
* instead. This requires knowledge on the developers part to figure out how
* they want to deal with it. */
$ret = $soapclient->call('echoStructAsSimpleTypes',
$p = array('inputStruct' => $struct->__to_soap()),
array('inputStruct' => $struct->__to_soap()),
$options);
// echo $soapclient->getWire();
if (PEAR::isError($ret)) {
echo 'Error: ' . $ret->getMessage() . "\n";
echo 'Error: ' . $ret->getMessage();
} else {
list($string, $int, $float) = array_values($ret);
echo "varString: $string\nvarInt: $int\nvarFloat: $float\n";
echo "varString: $string\nvarInt: $int\nvarFloat: $float";
}
echo "\n";

/* Calling echoMimeAttachment.
* We want to use MIME encoding here, the default is to use DIME encoding. */
$options['attachments'] = 'Mime';
$attachment = new SOAP_Attachment('attachment', 'text/plain', null,
'This is a MIME attachment');
$ret = $soapclient->call('echoMimeAttachment',
$params = array($attachment),
array($attachment),
$options);
// echo $soapclient->getWire();
print_r($ret);
12 changes: 12 additions & 0 deletions example/server.php
Expand Up @@ -20,6 +20,18 @@
* @link http://pear.php.net/package/SOAP
*/

/* If you want to implement Basic HTTP Authentication, uncomment the following
* lines of code. */
// if (!isset($_SERVER['PHP_AUTH_USER']) ||
// !isset($_SERVER['PHP_AUTH_PW']) ||
// $_SERVER['PHP_AUTH_USER'] !== 'username' ||
// $_SERVER['PHP_AUTH_PW'] !== 'password') {
// header('WWW-Authenticate: Basic realm="My Realm"');
// header('HTTP/1.0 401 Unauthorized');
// echo 'Not authorized!';
// exit;
// }

/* First, include the SOAP_Server class. */
require_once 'SOAP/Server.php';
$server = new SOAP_Server;
Expand Down

0 comments on commit cae58e4

Please sign in to comment.