Link to home
Start Free TrialLog in
Avatar of Barry62
Barry62Flag for United States of America

asked on

Web Service is giving me a 404 page

I am using nusoap to create a PHP web service.  I coded the server on one website, and the client on another.  When I call the server from the client, I get a 404 error.  Any ideas?

Server:
<?
//call library
require_once ('../nusoap/lib/nusoap.php');
include('Calls.php');
include('currentSeason.php');



//using soap_server to create server object
$server = new soap_server;


//register a function that works on server
$server->register('getCurrentSeason');

// create the function
function getCurrentSeason()
{
	
	return $arrEntireSeason;

}

// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);

exit();

?>

Open in new window


Client:
<?
require_once ('../nusoap/lib/nusoap.php');

$client = new soapclient('http://www.tltjc.org/server_CurrentSeason.php');

$response = $client->call('getCurrentSeason');

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>Title</th>     
    	<th>Author</th>        
    	<th>Director</th>        
    </tr>
    <?
    for($i=0;$i<=$count-1;$i++){
	?>
    <tr>
    	<td><?=$r[$i]['showname']?></td>
    	<td><?=$r[$i]['credits']?></td>                
    	<td><?=$r[$i]['director']?></td>        
    </tr>
    <?
	}
	?>
    </table>
    <?
}
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Yes, I have an idea.  Since you're in control of the web service drop SOAP and choose REST instead.  The internet is littered with the carcasses of failed SOAP projects, but I have never met anyone who could not immediately understand and use a RESTful service.  You can learn more than you ever want to know about REST here:
https://en.wikipedia.org/wiki/Representational_State_Transfer
https://en.wikipedia.org/wiki/Roy_Fielding

You can see an example of a RESTful web service here:

<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);


// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT FAMILY NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');


// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
, 'Dave'    => 'Baldwin'
)
;

// RESPONSE CAN BE PLAIN TEXT OR XML FORMAT
$alpha = NULL;
$omega = NULL;
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    // PREPARE THE XML WRAPPER
    $alpha = '<?xml version="1.0" encoding="utf-8" ?>' . PHP_EOL . '<response>' . PHP_EOL;
    $omega = PHP_EOL . '</response>';
}


// TEST THE 'API KEY' - THIS COULD BE A DATA BASE VALIDATION LOOKUP - AS SIMPLE OR COMPLEX AS NEEDED
$key = (!empty($_GET["key"])) ? $_GET["key"] : FALSE;
if ($key !== 'ABC')
{
    echo $alpha . 'BOGUS API KEY' . $omega;
    die();
}


// LOOK UP THE FAMILY NAME
$name = (!empty($_GET["name"])) ? $_GET["name"] : 'UNKNOWN';

// IF THE NAME FROM THE URL IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE APPROPRIATE FAMILY NAME FROM THE DATA MODEL
    echo $alpha . $dataModel[$name] . $omega;
    die();
}

// RETURNS THE UNKNOWN NAME INDICATOR
else
{
    echo $alpha . 'UNKNOWN' . $omega;
    die();
}

Open in new window

HTH, ~Ray
Avatar of Barry62

ASKER

I'll look into it.
Avatar of Barry62

ASKER

I don't really want to use a framework for this.  I already have a PHP website built.  I just want a service to call.  I found an article by Arun Kumar Sekar regarding ESTful services that uses rest.inc.php, which I assume he also developed.  Where would I find that?
This may be a lot simpler than you think!  You don't need a framework.  A RESTful web service is just like a web page.  The consumer of the service simply reads the response from a URL.  The provider of the service writes the output to the browser output stream.  To see how simple this is, click on these links (runs the script posted above).  Use "view source" to look at the output.
http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray
http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Error
http://laprbass.com/RAY_REST_get_last_name.php?key=Wrong&resp=XML&name=Ray

I looked at http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html and I am not sure if that is the article you're referring to, but I can tell you that it appears to be enormously elaborate and full of non-value-added code.  If you stick to a simple service you should be OK.
Avatar of Barry62

ASKER

I'm starting to understand your code example.  What if I wanted to return multiple values from my database (i.e. several records?)
You would return these values in either an XML document or a JSON string.  Probably the underlying structure would be an array of objects (with one object representing one row from the results set).  The properties of each object would be the values from the query.  The names of the properties would be the column names or the query-assigned names.

XML can use almost any encoding.  JSON requires UTF-8.  If it's all ascii character material, there are no UTF-8 collisions below code point 128.  If you have accented characters, the Euro, etc., you will need to be careful about using consistent UTF-8 encoding from top to bottom.
Avatar of Barry62

ASKER

OK, I'm better at XML than JSON, but I'm not that great with XML, either.  Would this be a good structure to use?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shows>
  <show>
    <showname></showname>
      <credits></credits>
      <director></director>
  </show>
</shows>


Then how would I parse the XML into an array from my client?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Barry62

ASKER

Excellent!  I got it working.  Thanks, Ray!
Thanks for the points - you will be MUCH happier with this than with SOAP. ~Ray