PHP SOAP - Extreme newbie questions :/
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...
(Maybe the following example is not the simplest
(I whish I had found a little "Hello World", but I didn't).
Notice that I wasn't especially willing to use "nusoap"
but it is what I found first. I discovered later I could
have used the SOAP extension for PHP...
but as I said I know nothing about that).
1) Database:
~~~~~~~~~~~
So, in that example, I first created a database:
=================================================
CREATE DATABASE soap_tutorial;
CREATE TABLE stockprices
(
stock_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
stock_symbol CHAR(3) NOT NULL,
stock_price DECIMAL(8,2) NOT NULL,
PRIMARY KEY(stock_id)
);
INSERT INTO stockprices VALUES (1, 'ABC', '75.00');
INSERT INTO stockprices VALUES (2, 'DEF', '45.00');
INSERT INTO stockprices VALUES (3, 'GHI', '12.00');
INSERT INTO stockprices VALUES (4, 'JKL', '34.00');
==================================================
2) Server code:
~~~~~~~~~~~
it is supposed to connect to the above database and
return the "stock_price" of the tuple in which "stock_symbol"
is equal to the value of the getStockQuote() function
parameter: "$symbol". This value comes from the client
side...
==================================================
soap_tutorial_server.php
==================================================
<?php
function getStockQuote($symbol)
{
$retval = 0;
//----------------------------------------------------------------
$retval = mysql_connect("localhost", "user", "password");
if(!$retval)
{
echo "<br />";
echo "Database connection error";
echo "<br />";
return $retval;
}
//----------------------------------------------------------------
$retval = mysql_select_db("soap_tutorial");
if(!$retval)
{
echo "<br />";
echo "Database not found";
echo "<br />";
return $retval;
}
//----------------------------------------------------------------
$query = "SELECT stock_price FROM stockprices "
. "WHERE stock_symbol = '" . $symbol . "'";
$result = mysql_query($query);
if(!$result)
{
echo "<br />";
echo "Query error";
echo "<br />";
return $result;
}
//----------------------------------------------------------------
$row = mysql_fetch_assoc($result);
echo "<br />";
echo "stock_price = " . $row["stock_price"];
echo "<br />";
return $stock_price;
}
//=======================================
// Turn this function into a web service
//=======================================
//---------------------------------------------------------------
// Include the NuSOAP library
//---------------------------------------------------------------
require("nusoap-0.7.3/lib/nusoap.php");
//---------------------------------------------------------------
// Instantiate an instance of the soap_server class
//---------------------------------------------------------------
$server = new soap_server();
//---------------------------------------------------------------
// Tell NuSOAP information for the WSDL document it is going to
// create for us. Specifically we specify the name of the server
// and the namespace, in that order.
//---------------------------------------------------------------
$server->configureWSDL("stockserver", "urn:stockquote");
//---------------------------------------------------------------
// Register the function we created with the SOAP server.
// We pass several different parameters to the register method.
//
// The first is the name of the function we are registering.
//
// The next parameter specifies the input parameters to the
// function we are registering. Notice that it is an array.
// The keys of the array represent the names of the input
// parameters, while the value specifies the type of the
// input parameter. One thing that pure PHP programmers might
// find odd is that I had to specify what types my input and
// return parameters are with the designations of xsd:string
// and xsd:decimal. It is required that you describe your data
// properly. You are not dealing with a loosely typed language
// here.
//
// The third parameter to the register method specifies the
// return type of the registered function. As shown below, it
// is fashioned in the same way as the last parameter, as an
// array.
//
// The next two parameters specify the namespace we are
// operating in, and the SOAPAction.
//---------------------------------------------------------------
$server->register("getStockQuote",
array("symbol" => "xsd:string"),
array("return" => "xsd:decimal"),
"urn:stockquote",
"urn:stockquote#getStockQuote");
//---------------------------------------------------------------
// Now, we finally finish it off with two more lines of code.
// The first simply checks if $HTTP_RAW_POST_DATA is initialized.
// If it is not, it initializes it with an empty string.
//
// The next line actually calls the service. The web request is
// passed to the service from the $HTTP_RAW_POST_DATA variable
// and all the magic behind the scenes takes place.
//---------------------------------------------------------------
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
?>
==================================================
=> :confused: I understand
why the code inside the function is executed
because there is a function call in the client but I do not
understand when the rest of the code (outside the function)
is executed...
=> :confused: Will I see server errors
(for example if the database connection fails)?
3) Client side code
~~~~~~~~~~~~~~~~~
==================================================
soap_tutorial_client.php
==================================================
<?php
//---------------------------------------------------------------
// Include the NuSOAP library
//---------------------------------------------------------------
require("nusoap-0.7.3/lib/nusoap.php");
//---------------------------------------------------------------
// Instantiate the soapclient class.
// We pass in the URL of the SOAP Server we are dealing with.
//---------------------------------------------------------------
$c = new soapClient("soap_tutorial_server.wsdl");
//---------------------------------------------------------------
// Last make a call to the Web Service.
// The one caveat is that the parameters to the Web Service must
// be encapsulated in an array in which the keys are the names
// defined for the service. You will see that I have an array
// key named 'symbol' because that is the name of the input
// parameter of my function. If you remember how we specified
// the input parameters when we registered the function with the
// server, you will see that this is very similar.
//---------------------------------------------------------------
$stockprice = $c->call("getStockQuote", array("symbol" => "ABC"));
//---------------------------------------------------------------
// Output the result
//---------------------------------------------------------------
echo "<br />";
echo "The stock price for \"ABC\" is " . $stockprice . ".";
echo "<br />";
//---------------------------------------------------------------
// Kill object
//---------------------------------------------------------------
unset($c);
?>
==================================================
4) I put those two files ("soap_tutorial_server.php" and
"soap_tutorial_client.php") in my "htdocs" directory.
5) When I open "soap_tutorial_server.php" in my browser, I get a page which
contents are:
===================================================
stockserver
View the WSDL for the service. Click on an operation name to view
it's details.
* getStockQuote
===================================================
I can click on the word "WSDL" and I get the following URI :
http://localhost/soap_tutorial_server.php?wsdl
In the browser appears an XML file.
=> :confused: Should I do something with it?
(Save it somewhere?)
=> :confused: Can you explain me the syntax "soap_tutorial_server.php?wsdl"?
6) When I open "soap_tutorial_client.php", I get the following content:
====================================================
The stock price for "ABC" is .
====================================================
As you can see, the "stock_price" is missing.
I have tried to find a simple tutorial about SOAP but I haven't had
the chance to find something simple...
I know there is a lot to read above... but I would be very grateful if
you could give me a little help :-) !
Thanks and best regards :-),
--
Lmhelp
--
View this message in context: http://old.nabble.com/PHP-SOAP---Extreme-newbie-questions-%3A--tp28095423p28095423.html
Sent from the Php - Soap mailing list archive at Nabble.com.
--
PHP Soap Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
[PHP Home]
[PHP Users]
[Kernel Newbies]
[PHP Database]
[Yosemite]