Staggan Posted November 27, 2013 Share Posted November 27, 2013 (edited) 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 November 27, 2013 by Staggan Quote Link to comment Share on other sites More sharing options...
Staggan Posted November 27, 2013 Author Share Posted November 27, 2013 (edited) 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 November 27, 2013 by Staggan Quote Link to comment Share on other sites More sharing options...
Staggan Posted November 27, 2013 Author Share Posted November 27, 2013 Hmm... Missing the end of my post now... So, I have printed $response array but am seeing nothing... Any suggestions would be appreciated Thanks Quote Link to comment Share on other sites More sharing options...
jcbones Posted November 28, 2013 Share Posted November 28, 2013 Chrome doesn't like this forum, and that means you lose anything after a code, quote, or any other tag. Perhaps you could repost your question with a different browser, so that the question is less confusing. Quote Link to comment Share on other sites More sharing options...
boompa Posted November 29, 2013 Share Posted November 29, 2013 (edited) 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 November 29, 2013 by boompa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.