In 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 [googleplusone]
Code from the Video
xml_code.php
<?php // 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 require_once('StudentDB.php'); // 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 require_once('../../mysqli_connect.php'); // Check the connection if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // 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>'; ?>
Students.php
<?php 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_code.php
<html> <?php // 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 require_once('Students.php'); $student_data = new Students(); if(isset($_POST['submit'])){ switch($_POST['request']){ case "Get First Names" : $student_info = $student_data->getStudentFirstNames(); break; case "Get Last Names" : $student_info = $student_data->getStudentLastNames(); break; default: http_response_code(400); } echo json_encode($student_info); } ?> <form action="rpc_code.php" method="post"> Request: <select name="request"> <option>Get First Names</option> <option>Get Last Names</option> </select> <input type="submit" name="submit"> </form> </html>
Leave a Reply