SOAP and PHP
Post # 281 permalink Topic #278 by mreschke on 2011-11-16 08:10:52 (viewed 397 times)

Map
Connect to an PHP/SOAP or ASP.NET/SOAP web service with PHP and how to create a PHP SOAP web service (server)

Related


Info Summary[-][- -][++]

Learn how to connect to an an PHP/SOAP or ASP.NET/SOAP web service with PHP and how to create a PHP SOAP web service (server)

Code PHP SOAP Client[-][- -][++]

Code PHP Client Connect to ASP.NET ASMX[-][- -][++]

Here are two different methods for connecting to the ASP.NET .asmx file

Method 1

PHP Code Snippet
/*
Soap Client
Connect to an ASP.NET SOAP webservice
References: http://metrix.fcny.org/wiki/display/tips/How+to+Create+a+PHP+Client+for+a+.NET+and+SOAP-based+Web+Service+API
mReschke 2011-11-16
 
In this example, we have a webservice function called GetSpif that takes one parameter _spifID
*/
 
try {
    $client = new SoapClient("http://example.com/webservices/spifs.asmx?wsdl");
 
    //Parameters must be an associative array
    $params = array();
    $params["_spifID"] = 2;
 
    $result = $client->GetSpif($params)->GetSpifResult;
 
    var_dump($result);
    echo "<hr/>";
    echo $result->ownerID."<br />";
    echo $result->spifTypeID;
} catch (Exception $e) {
    echo "Error!<br />";
    echo $e -> getMessage ();
}
?> 
 

Method 2

PHP Code Snippet
/*
Soap Client
Connect to an ASP.NET SOAP webservice
References: http://php.net/manual/en/soapclient.soapcall.php
mReschke 2011-11-16
 
In this example, we have a webservice function called GetSpif that takes one parameter _spifID
*/
 
class Test {
    public $_spifID;
}
 
$parameters = new Test;
$parameters->_spifID = 2;
 
try {
    $client = new SoapClient ("http://example.com/webservices/spifs.asmx?wsdl", array('classmap' => array('GetSpif' => 'Test')));
    $result = $client->GetSpif($parameters);
    var_dump($result);
    echo "Success";
}
catch (Exception $e) {
    echo "Error!<br />";
    echo $e -> getMessage ();
}
 

Code PHP Client Connect to PHP SOAP Service[-][- -][++]

This would be the example PHP SOAP server

PHP Code Snippet
/*
 PHP SOAP Server From: http://www.ibm.com/developerworks/opensource/tutorials/os-php-webservice/section3.html
*/
function echoo($echo){
    return "ECHO: ".$echo;
}
 
$server = new SoapServer(null, array('uri' => "urn://tyler/res"));
$server->addFunction('echoo');
$server->handle();
 

And here is the PHP SOAP client connecting to the PHP SOAP server

PHP Code Snippet
try {
    /* This connects to a PHP SOAP server, not an ASP.NET asmx SOAP server */
    /* PHP Client connecting to PHP SOAP webservice from http://www.ibm.com/developerworks/opensource/tutorials/os-php-webservice/section3.html */
    $client = new SoapClient(NULL, array(
                             'location' => "http://example.com/soap/server1.php",
                             'uri'      => "urn://tyler/req"));
    $result = $client->__soapCall("echoo", array('Waz Up SOAP!!'));
 
    var_dump($result);
    echo "<hr />";
    print $result;
 
} catch (Exception $e) {
    echo "Error!<br />";
    echo $e -> getMessage ();
}
?> 
 

Code PHP SOAP WebService (Server)[-][- -][++]

PHP Code Snippet
/*
 PHP SOAP Server From: http://www.ibm.com/developerworks/opensource/tutorials/os-php-webservice/section3.html
*/
function echoo($echo){
    return "ECHO: ".$echo;
}
 
$server = new SoapServer(null, array('uri' => "urn://tyler/res"));
$server->addFunction('echoo');
$server->handle();
 

Resource Resources[-][- -][++]