Web Services Tutorial 2

RPC Video TutorialIn this part of my web services tutorial I cover 2 ways to create XML from a database. I then show a simple example on how Remote Procedure Calls work.

If you missed the previous web services tutorial, where I showed how to work with JSON data, you should watch that first. All of the code used in this video can be found below.

If you like videos like this, it helps to share with Google Plus with a click here

Code from the Video



// Tell whomever is receiving this data the content type
// Needs to be set on the first line
// header('Content-Type: text/xml');

// Insert to the StudentDB class

// XML is used when you want to provide detailed information
// about your data like attributes and data type

$dale_cooper = array("Dale", "Cooper", "123 Main St",
	"Seattle", "Washington");
// Define that all data will be between 2 <student> tags </student>
$xml = new SimpleXMLElement("<student />");

// Get each piece of data from the array and put it between <data> tags
foreach($dale_cooper as $info){

	$xml->addChild("data", $info);


// Generate a DOM document we can style
// A DOM Document represents an entire XML document
$dom = dom_import_simplexml($xml)->ownerDocument;

// State we want the data to be indented
$dom->formatOutput = true;

// saveXML converts the XML into a string to print
echo $dom->saveXML();

// Get a connection to the database

// Check the connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

// Query retrieves the student data
$query = "SELECT * FROM students WHERE student_id = 1";

// Will hold all students retreived
$student_array = array();

if($result = $dbc->query($query)){

	while ($obj = $result->fetch_object()){
		printf("%s %s %s %s %s %s %s %s %s %s %s %s %s <br />",
		$obj->first_name, $obj->last_name, $obj->email,
		$obj->street, $obj->city, $obj->state, $obj->zip,
		$obj->phone, $obj->birth_date, $obj->sex, 
		$obj->date_entered, $obj->lunch_cost, $obj->student_id);
		$temp_student = new StudentDB($obj->first_name, 
		$obj->last_name, $obj->email, $obj->street,
		$obj->city, $obj->state, $obj->zip, $obj->phone, 
		$obj->birth_date, $obj->sex, $obj->date_entered, 
		$obj->lunch_cost, $obj->studentid);
		$student_array[] = $temp_student;

echo '<?xml version="1.0" encoding="UTF-8" ?>';

echo '<students>';

// Cycle through all the students and print them surrounded by tags
foreach($student_array[0] as $key=>$value){

	echo '<' . $key . '>' . $value . '</' . $key . '>';


echo '</students>';




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;






// RPC (Remote Procedure Call) is used to call a function remotely
// You include the function name to execute and maybe attributes

// Insert to the Students class

$student_data = new Students();


	case "Get First Names" :
		$student_info = $student_data->getStudentFirstNames();
	case "Get Last Names" :
		$student_info = $student_data->getStudentLastNames();
	echo json_encode($student_info);



<form action="rpc_code.php" method="post">

<select name="request">

<option>Get First Names</option>
<option>Get Last Names</option>


<input type="submit" name="submit">



Leave a Reply

Your email address will not be published.