Jump to content

Simple soap service


Staggan

Recommended Posts

Hello

 

I am trying to write a very basic web service using php / nusoap and am having a few problems...

 

Here is my server code:

 
<?
 
// setup dbase connection
  $db = mysql_connect('IP ADDRESS:3306', 'USERNAME', 'PASSWORD');
 
    if (!$db) {
        echo "Unable to establish connection to database server";
        exit;
    }
 
    if (!mysql_select_db('DBASENAME', $db)) {
        echo "Unable to connect to database";
        exit;
    }
 
//call library
require_once ('lib/nusoap.php');
 
//using soap_server to create server object
$server = new soap_server;
 
//register a function that works on server
$server->register('getAllPlayers');
 
 
// create the function
function getAllPlayers()
{
 
$sql = "SELECT * FROM player";
$q = mysql_query($sql);
while($r = mysql_fetch_assoc($q)){
 $players[] = $r['name']; 
}
return $players;
 
}
 
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
 
exit();
 
?>
 
Link to comment
https://forums.phpfreaks.com/topic/284323-simple-soap-service/
Share on other sites

For some reason my post only posted half...

 

Here is my client code...

 
<?
require_once ('lib/nusoap.php');
 
 
$_HTTP = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
// Set WSDL Server Path
$wsdl_path = 'http://WEBSITE.com/restApi/apiServer.php?wsdl';
// Make a soap client
$client = new nusoap_client($wsdl_path,'wsdl');
 
 
$response = $client->call('getAllPlayers');
print_r ($response,1);
 
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode.")</p>";
echo "String: ".$client->faultstring;
}
else
{
$r = $response;
$count = count($r);
?>
    <table border="1">
    <tr>
     <th>Player</th>
    </tr>
    <?
    for($i=0;$i<=$count-1;$i++){
?>
    <tr>
     <td><?=$r[$i]['player']?></td>
    </tr>
    <?
}
?>
    </table>
    <?
}
?>
 
Link to comment
https://forums.phpfreaks.com/topic/284323-simple-soap-service/#findComment-1460323
Share on other sites

A ReST API would not use SOAP. ReST is an alternative -- in fact, a vastly superior alternative -- to SOAP. These days in creating an API server you should avoid SOAP. And NuSOAP is not necessary if you're using any modern version of PHP (i.e., PHP 5+), as there are SOAP classes in PHP itself. NuSOAP dates back to PHP 4.

 

A ReST call to get all players, as you are trying here, would look something like this:

 

GET http://myserver/restapi/players

 

Getting a single player would look like

 

GET http://myserver/restapi/players/42 (where 42 would be the player ID)

 

In general you would want to return JSON to the caller. Using a PHP framework like Laravel (or any other frameworks) makes this easy. Here is a relevant tutorial.

 

Also, if you don't know what those GET lines above mean (i.e., what GET does), you don't know how the web works, you're not ready to be writing an API server. Start by learning how the web works by reading up on HTTP.

Link to comment
https://forums.phpfreaks.com/topic/284323-simple-soap-service/#findComment-1460622
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.