Re: PHP SOAP - Extreme newbie questions :/

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Lmhelp,

I think that my problems came from:

1) The MySQL extension for PHP wasn't enabled on my computer:
To solve that problem:
uncomment in "php.ini" (PHP 5) the following line:
-----------------------------------------------------------------
extension=php_mysql.dll
-----------------------------------------------------------------
This is probably the reason why I got some errors. *** VERY MUCH DOUBTFUL ***

2) The following lines were missing in my client code:
-----------------------------------------------------------------
$symbol = 'ABC';

if(isset($_GET['symbol']) && !empty($_GET['symbol']))
  $symbol = $_GET['symbol'];
-----------------------------------------------------------------
This is probably the reason why the result was empty! *** DEFINITELY NOT WHY ***

Thanks for the thanks, but I was hoping that my examples showed why you might "think" something else is wrong, when in fact you aren't collecting/acting on the information available. Maybe I'll add some unit tests to help drive the idea home, but this was meant as a quick and fairly simple example using NuSOAP and WSDL.

There are major problems that could show up in your code below, that you would never
see from the client and then start guessing what the 'voodoo' problem might be:

* 1) MySQL function calls - return errors for a reason - ignore at your/clients' risk * Many things can go wrong with the Connect, Select, Query, Fetch chain of events
*    without the error checking you have _no idea_ if this was a problem
*  see - if(!function_exists('mysql_connect'))
*    see - dump($client); in my example client code and especially *why*
*  Better *guess* - you were getting a server get_object() or ereg() warning

* 2) all of that default/GET code was to make testing simpler and *NOT*
* a problem passing the parameters (see TESTING and request headers in client code)

* require_once 'nusoap.php';  // nothing else calls this code ever?
    vs require("nusoap.php"); // *might* work ok, but why depend on it?

I don't know when you downloaded the source zip, but there is also a non-NuSOAP client included, using some code from Richard's PHP SOAP extension reply to you earlier.

You can remove comments if you need to, but if you start to remove defensive programming practices you can make the comments like this:

  // and now... SHOOT SELF IN HEAD - *Guess* what happened if lucky

If you don't test, you don't know,
  John


[
- First: answer to John see below.
- Richard, I saw your post and I am trying now to get it work.
  Thanks.
- Thanks to zippy1981 too.
- Nota: I don't know why but there are some posts missing on the
  thread...
]

Hi John,

Thank you for your contribution.
I've tested your code and it works:
- with the dummy data,
- and with the database data.
So, folks, download and test:
http://www.steelesoftconsulting.com/code/stockquote.zip
you'll have a working code!

There are a lot of posts about this example on the web...
I tried to see what was wrong with "my" copy of the code.
I post here the version that works now
(I've removed the comments so that it's very compact):

=================================================================
soap_server.php
=================================================================
<?php

  function getStockQuote($symbol)
  {
    // Set "user and "password" for your system.
    mysql_connect('localhost', 'user', 'password');

    mysql_select_db('soap_tutorial');

    $query = "SELECT stock_price FROM stockprices "
           . "WHERE stock_symbol = '$symbol'";

    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);

    return $row['stock_price'];
  }

  require('nusoap.php');

  $server = new soap_server();

  $server->configureWSDL('stockserver', 'urn:stockquote');

  $server->register("getStockQuote",
                    array("symbol" => "xsd:string"),
                    array("return" => "xsd:decimal"),
                    "urn:stockquote",
                    "urn:stockquote#getStockQuote");

  $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
                        ? $HTTP_RAW_POST_DATA : "";

  $server->service($HTTP_RAW_POST_DATA);
?>

=================================================================
soap_client.php
=================================================================
<?php
  require("nusoap.php");

  // Set "SERVER_URL" for your system.
  define('SERVER_URL', 'http://localhost/soap/soap_server.php');
  $client = new nusoap_client(SERVER_URL. '?wsdl', true);

  $symbol = 'ABC';

  if(isset($_GET['symbol']) && !empty($_GET['symbol']))
    $symbol = $_GET['symbol'];

  $params = array('symbol' => $symbol);
  $result = $client->call('getStockQuote', $params);

  echo "=====================================================";
  echo "<br />";
  echo "The stock price for \"" . $symbol . "\" is " . $result . ".";
  echo "<br />";
  echo "=====================================================";
  echo "<br />";
?>

=================================================================
In your HTTP server root directory:
-- create a directory "soap"
-- and put in it the three files:
   - soap_server.php
   - soap_client.php
   - nusoap.php
=================================================================
You can then type in your browser:

- either: http://localhost/soap/soap_client.php
  in which case, you'll get the following result:
  =====================================================
  The stock price for "ABC" is 66.12.
  =====================================================

- or, for instance: http://localhost/soap/soap_client.php?symbol=JKL
  in which case, you'll get the following result:
  =====================================================
  The stock price for "JKL" is 34.
  =====================================================

=================================================================
I think that my problems came from:

1) The MySQL extension for PHP wasn't enabled on my computer:
To solve that problem:
uncomment in "php.ini" (PHP 5) the following line:
-----------------------------------------------------------------
extension=php_mysql.dll
-----------------------------------------------------------------
This is probably the reason why I got some errors.

2) The following lines were missing in my client code:
-----------------------------------------------------------------
$symbol = 'ABC';

if(isset($_GET['symbol']) && !empty($_GET['symbol']))
  $symbol = $_GET['symbol'];
-----------------------------------------------------------------
This is probably the reason why the result was empty!

Thanks a lot.
Sincerely,
--
Lmhelp

--
/* 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
 */


--
PHP Soap Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Kernel Newbies]     [PHP Database]     [Yosemite]

  Powered by Linux