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();
 
?>
 
Edited by Staggan
Link to comment
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>
    <?
}
?>
 
Edited by Staggan
Link to comment
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.

Edited by boompa
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.