How do I use php to hit a web service I created using gSoap and C++?
I have created the gSOAP Calculator Service example found at: http://www.genivia.com/Products/gsoap/demos/index.html
I have my web service running as a deamon on my Solaris box.
Now I'm trying to use a php page to hit this new web service. I have been looking at http://www.ph开发者_运维知识库p.net/manual/en/class.soapclient.php, and have tried to make an example, but have had no luck. Can someone please point me to an example of doing this? or show me the code for doing it?
I have spent two days looking at web sites and trying different things and am running out of time on my project. Thank you so much for your help.
fyi: I have my apache server set to port 7000.
<?php
function customError($errno, $errstr)
{
echo "<b>Error: </b> [$errno] $errstr";
}
set_error_handler("customError");
define("SOAP_ENCODED", 1);
define("SOAP_RPC", 1);
$options = array(
'compression'=>true,
'exceptions'=>false,
'trace'=>true,
'use' => SOAP_ENCODED,
'style'=> SOAP_RPC,
'location'=> "http://localhost:7000",
'uri' => "urn:calc"
);
echo "1";
$client = @new SoapClient(null, $options);
echo "2";
$args = array(2, 3);
$ret = $client->__soapCall("add", $args);
echo "3";
if (is_soap_fault($ret))
{
echo 'fault : ';
var_dump($client->__getLastRequest());
var_dump($client->__getLastRequestHeaders());
}
else
{
echo 'success : ';
print '__'.$ret.'__';
}
$client->ns__allAllowed();
?>
The web page does not return any errors.
Michael
At the top of the script:
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
Some things to check:
- Include Unicode Signature (BOM) is off in your editor
- No white space after
?>
(You should just remove it) - Run the script in cli
php /path/myscript.php
In the tutorial you mentioned written that Calc web service generates WSDL. WSDL is a file that describes all methods and types of web service. Keeping this in mind you can create SOAP client in PHP:
$client = new SoapClient('http://www.mysite.com/calc.wsdl',
array('trace' => true, 'exceptions' => true));
Then you can call any method provided by Web service:
try {
$client = new SoapClient('http://www.mysite.com/calc.wsdl',
array('trace' => true, 'exceptions' => true));
$result = $client->methodName($param1, $param2);
} catch (SoapFault $e) {
var_dump($e);
}
var_dump($result);
If some error will occur you'll catch it in try/catch block.
精彩评论