Web Services Tutorial 3

REST Web Service ExampleIn this part of my web services tutorial, I provide both a non-WSDL SOAP server and a REST Web Service Example program. The goal is to keep everything simple and easy to understand because many people are confused by both.

I decided to avoid covering WSDL (Web Service Description Language) because it is very common for these to be generated by software. I also decided to focus on just the GET method with REST because it is most commonly used.

If you like videos like this, it helps to tell others on Google Plus with a click here [googleplusone]

Code From the Video



class Students{

public function getStudentFirstName(){

    $studentFN = array("Dale", "Harry", "Shelly", "Bobby",
			"Donna", "Audrey", "James", "Lucy", "Tommy",
			"Andy", "John");
    return $studentFN;


    public function getStudentLastName(){

        $studentLN = array("Cooper", "Truman", "Johnson", "Briggs",
			"Hayward", "Horne", "Hurley", "Moran", "Hill",
			"Brennan", "Smith");
        return $studentLN;


    public function getStudentNames(){
	$studentNames = "Dale Cooper, Harry Truman, Shelly Johnson, " .
			"Bobby Briggs, Donna Hayward, Audrey Horne, " .
			"James Hurley, Lucy Moran, Tommy Hill, " .
			"Andy Brennan, John Smith";
	return $studentNames;




// SOAP (Simple Object Access Protocol) is based on XML and
// it is a way to access web services
// A WSDL (Web Service Description Language) is an XML format
// commonly used with SOAP, for describing the functionality of network 
// services.
// A WSDL file describes how the service is called, what parameters 
// it expects, and the data structures it returns.

// Non-WSDL SOAP Server

// Insert to the Students class

// Because a WSDL isn't used we must provide a URI for the service
$options = array("uri" => "http://localhost");

// Create a SOAPServer object
// You'll either pass it a WSDL file or null and the array that
// contains the URI
$server = new SoapServer(null, $options);

// Define the class that holds the functions we want made available

// handle() is used to process SOAP requests and responds



// We must define the location of the service in the client
// because we don't have a WSDL file
$options = array("location" => "http://localhost/ws/soap_service.php",
				"uri" => "http://localhost");

// Provides a client to read from the service
// It either receives a WSDL file, or null and the options
$client = new SoapClient(null, $options);

// Call the function in the Students class
$students = $client->getStudentNames();

echo $students;


catch(SoapFault $ex){






// REST (Representational State Transfer) allows anything to work with your data // that can send a HTTP request

// The most common methods used are GET, POST, PUT, and DELETE
// GET: Used to retrieve data from a resource
// POST: Used to create a new resource, but is considered unsafe
// PUT: Used to update a resource, but is considered unsafe
// DELETE: Used to delete a resource and also is unsafe

function get_student_info($id){
    $student_info = array();
    // Data that normally is pulled from a database
        case 1:
            $student_info = array("first_name" => "Dale", "last_name" => "Cooper", "address" => "123 Main St Yakima, WA"); 
        case 2:
            $student_info = array("first_name" => "Harry", "last_name" => "Truman", "address" => "202 South St Vancouver, WA");
        case 3:
            $student_info = array("first_name" => "Shelly", "last_name" => "Johnson", "address" => "9 Pond Rd Sparks, NV");
        case 4:
            $student_info = array("first_name" => "Bobby", "last_name" => "Briggs", "address" => "14 12th St San Diego, CA");
        case 5:
            $student_info = array("first_name" => "Donna", "last_name" => "Hayward", "address" => "120 16th St Davenport, IA");
    return $student_info;

function get_student_list(){
    // Data that normally is pulled from a database
    $student_list = array(array("id" => 1, "name" => "Dale Cooper"),
                          array("id" => 2, "name" => "Harry Truman"),
                          array("id" => 3, "name" => "Shelly Johnson"),
                          array("id" => 4, "name" => "Bobby Briggs"),
                          array("id" => 5, "name" => "Donna Hayward"));
    return $student_list;

// Execute the proper method above based on request

        case "get_student_list":
            $value = get_student_list();
        case "get_student":
            $value = get_student_info($_GET["id"]);





// Check if one of the student name links was clicked

if(isset($_GET["action"]) == "get_student"){
    // Get the specific student data
    $student_info = file_get_contents('http://localhost/ws/api.php?action=get_student&id=' . $_GET["id"]);
    // Decode from JSON into an array
    $student_info = json_decode($student_info, true);

First Name : <?php echo $student_info["first_name"] ?><br />
Last Name : <?php echo $student_info["last_name"] ?><br />
Address : <?php echo $student_info["address"] ?><br />



 else // else print out the list of students
    // Call the method get_student_list in the API to get the list
    $student_list = file_get_contents('http://localhost/ws/api.php?action=get_student_list');
    // Convert from JSON and into an array
    $student_list = json_decode($student_list, true);

<!-- Cycle through the student list and print them out with the correct id -->

<?php foreach($student_list as $student): ?>

<a href=<?php echo
"http://localhost/ws/rest_client.php?action=get_student&id=" . $student["id"] ?>><?php echo $student["name"] ?></a><br />
 <?php endforeach; ?>
 } ?>


6 Responses to “Web Services Tutorial 3”

  1. Manu says:

    Thanks for the web services tutorials Derek. That’s a wonderful work.
    I have recently come across your tutorials and have liked them a lot!!
    Which is the next tutorial coming up?
    Also, can you please cover Springs, Hibernate?

    • Derek Banas says:

      Thank you 🙂 The next video will go up today. I’ll show how to make a translation web service. I also show how to use MySQL with utf-8. I’ll cover the other topics after I finish up with Android.

  2. Salihu Adam says:

    Nice tutorial, exposed me to web services but my question is must i use object oriented php before i can be able to create a web service.

Leave a Reply

Your email address will not be published.