Re: [SOAP] PHP SOAP - Extreme newbie questions :/

From: Date: Fri, 02 Apr 2010 01:14:00 +0000
Subject: Re: [SOAP] PHP SOAP - Extreme newbie questions :/
Groups: php.soap 
Request: Send a blank email to php-soap+get-3072@lists.php.net to get a copy of this message
Hi everyone, I am an absolute beginner with SOAP :-( :-). I have been trying to follow a tutorial but I got stuck with "things" not working... [chopped for brevity]
Hello Lmhelp, Your question was probably best asked on the NuSOAP mailing list (CC'd): nusoap-general@lists.sourceforge.net You can find a working NuSOAP/WSDL client/server example at: http://www.steelesoftconsulting.com/code/stockquote.zip This includes a modified nusoap.php with the following changes: * renamed soapclient to nusoap_client to prevent PHP SOAP extension conflict * added (object) cast to get_class() calls in serialize_return() [PHP 5.3 change] I'm including the code for stock_server.php (with dummy data) and stock_client.php - hopefully the sparse comments will be enough to help you understand. Make sure you don't get any whitespace characters outside the <?php and ?> tags. This might get a bit munged by some email clients, get the source zip if so. Hope this helps, John ==== <?php /* stock_server.php */ /** * NuSOAP Server Example - getStockQuote and hello * Demonstrate how to use NuSOAP to create SOAP web services using WSDL. * Call this directly from your browser to view the operations & WSDL documentation. * * @author John Steele <john@steelesoftconsulting.com> * @website http://www.steelesoftconsulting.com/ */ // ini_set('soap.wsdl_cache_enabled', '0'); // disable WSDL cache // Pull in the NuSOAP code require_once 'nusoap.php'; // Create the server instance $server = new soap_server(); // Initialize WSDL support (servicename, namespace) $server->configureWSDL('stockquote', 'urn:stockquote'); // Register the method to expose (last 3 optional)
$server->register('getStockQuote',    // method name
array('symbol' => 'xsd:string'), // input parameters array('return' => 'xsd:decimal'), // output parameters
  'urn:stockquote',          // namespace
  'urn:stockquote#getStockQuote',    // soapaction
  'rpc',                // style
  'encoded',              // use
  'Return Stock Quote by Symbol'    // documentation
); // Register the method to expose
$server->register('hello',        // method name
    array('name' => 'xsd:string'),    // input parameters
    array('return' => 'xsd:string'),  // output parameters
    'urn:stockquote',          // namespace
    'urn:stockquote#hello',        // soapaction
    'rpc',                // style
    'encoded',              // use
    'Says Hello to the Caller'      // documentation
); // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); /** * Returns the Stock Price, or -99.99 if not found * * @param string $symbol * @return float */ function getStockQuote($symbol) {
  $stock_price = -99.99;    // flag for not found
    // use some dummy data instead of database for testing
$row = array('ABC' => 75.00, 'DEF' => 45.00, 'GHI' => 12.00); if(isset($row[$symbol])) {
    $stock_price = $row[$symbol];
} return (float) $stock_price; /* // You would *never* really echo anything that isn't XML SOAP
    if(!$retval = mysql_connect('localhost', 'user', 'password')) {
      echo '<p>Database connection error.</p>';
      return -1;
    }
if(!$retval = mysql_select_db('soap_tutorial')) {
      echo '<p>Unable to select database.</p>';
      return -2;
    }
    $query = "SELECT stock_price FROM stockprices ".
       "WHERE stock_symbol = '" . $symbol . "'";
    if(!$result = mysql_query($query)) {
      echo '<p>Query error in '. $query. '</p>';
      return -3;
    }
    if($row = mysql_fetch_assoc($result)) {
    $stock_price = $row['stock_price'];
} return (float) $stock_price; */ } /** * Says Hello to the Caller * * @param string $name * @return string */ function hello($name) {
    return 'Hello, '. $name. '.';
} ?> <?php /* stock_client.php */ /** * NuSOAP Client Example - getStockQuote and hello * Demonstrate how to use NuSOAP to consume SOAP web services using WSDL. * * @author John Steele <john@steelesoftconsulting.com> * @website http://www.steelesoftconsulting.com/ */ // This certainly will need to be changed to fit your server environment define('SERVER_URL', 'http://localhost/ws/stockquote/stock_server.php'); // Set to true for testing/debugging (a LOT of info), false for production define('TESTING', false);
$symbol = 'ABC';            // Set fictional default, or from URL GET
if(isset($_GET['symbol']) && !empty($_GET['symbol'])) // ?symbol=XYZ $symbol = $_GET['symbol'];
require_once 'nusoap.php';        // Pull in the NuSOAP code
if(!TESTING)              // turn off debugging for speed/production
nusoap_base::setGlobalDebugLevel(0); // Create the client instance $client = new nusoap_client(SERVER_URL. '?wsdl', true);
if($err = $client->getError()) {    // Check for an error
echo '<h2>Constructor error</h2><pre>'. $err. '</pre>'; } if(TESTING) dump($client->debug_str); // This block isn't stock quote related, but a simple example
$params = array('name' => 'Bob');      // Say hello to Bob
$result = $client->call('hello', $params); // No Error Checking at all! dump($result); $params = array('symbol' => $symbol); // Return Stock Quote by Symbol // Call the SOAP method $result = $client->call('getStockQuote', $params);
if ($client->fault) {          // Check for a fault
    echo '<h2>Client Fault</h2>'. dump($result);
} else { if ($err = $client->getError()) { // Check for an error
        echo '<h2>Call Error</h2>'. dump($err);
    }
    else {                // Display the result
        // echo '<h2>Result</h2>'. dump($result);
      $result = sprintf('%4.2f', $result);  // format to 2 decimal places
      if($result != -99.99)
      echo '<p>The stock price for "'. $params['symbol'].
         '" is <strong>'. $result. '</strong></p>';
    else
      echo '<p>Stock symbol "'. $params['symbol']. '" not found.';
    }
} if(TESTING) {
    // Display the request and response
  echo '<h2>Request</h2><pre>'. htmlspecialchars($client->request, ENT_QUOTES). '</pre>';
  echo '<h2>Response</h2><pre>'. htmlspecialchars($client->response, ENT_QUOTES). '</pre>';
    // Display the debug messages
  echo '<h2>Debug</h2><pre>'. htmlspecialchars($client->debug_str, ENT_QUOTES). '</pre>';
    // Display the client object - very handy for server PHP warnings, etc.
echo '<h2>Client</h2>'; dump($client); }
unset($client);              // Kill the instance to free up memory
// some handy functions function dump($this) { echo '<pre style="font:normal 14px Courier New,Courier,Times New Roman,serif">'.
     print_r($this, 1). '</pre>';
} function get_xml($xml) { return str_replace('><', ">\n<", $xml); } ?> --
/* Steelesoft Consulting     John Steele - Systems Analyst/Programmer
* We also walk dogs... Dynamic Web Design, PHP/MySQL/Linux/Hosting * http://www.steelesoftconsulting.com/ (541) 708-1708 */

« previous php.soap (#3072) next »