In the article for step 4, we looked at the contents of the trivia folder and the PHP code designed to retrieve the question/hint/answer for the id being fed in as a parameter. Over the next two steps we will be getting into the main functionality of this REST service – looking up details for wineries, distilleries and meaderies. Let’s jump right in …
We will be looking at the files and procedures created to read counties and regions. The main SQL clause used in the two functions called by these procedures is DISTINCT. The code shown below is for read_counties.php
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
// include database and object files
include_once '../config/database.php';
include_once '../objects/wineries.php';
$cred = 0;
$user = $_SERVER['PHP_AUTH_USER'];
$pwd = $_SERVER['PHP_AUTH_PW'];
if(sha1('* usr *') == $user and sha1('* pwd *') == $pwd)
$cred = 1;
if($cred == 0) {
// set response code - 403 Not found
http_response_code(401);
// tell the user product does not exist
echo json_encode(array("message" => "unauthorized"));
die;
}
// get database connection
$database = new Database();
$db = $database->getConnection();
// prepare product object
$wineries = new Wineries($db);
// set ID property of record to read
//$wineriest->id = isset($_GET['id']) ? $_GET['id'] : die();
// read the details of product to be edited
$stmt = $wineries->readCounties();
$num = $stmt->rowCount();
if($num > 0){
// create array
$county_arr = array();
$county_arr["records"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//$county_item = array(
//"county" => $County
//);
array_push($county_arr["records"], $State . "-" . $County);
}
// set response code - 200 OK
http_response_code(200);
// make it json format
echo json_encode($county_arr);
}
else{
// set response code - 404 Not found
http_response_code(404);
// tell the user product does not exist
echo json_encode(array("message" => "counties not read"));
}
?>
After verifying the authentication info fed into our REST service and setting up the database connection, we call the ReadCounties function. Since we have a multi-state database, we are feeding the state and county name back to the calling web page using an encoded JSON array. Next, let’s look at the read_regions.php code.
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
// include database and object files
include_once '../config/database.php';
include_once '../objects/wineries.php';
$cred = 0;
$user = $_SERVER['PHP_AUTH_USER'];
$pwd = $_SERVER['PHP_AUTH_PW'];
if(sha1('* user *') == $user and sha1('* pwd *') == $pwd)
$cred = 1;
if($cred == 0) {
// set response code - 403 Not found
http_response_code(401);
// tell the user product does not exist
echo json_encode(array("message" => "unauthorized"));
die;
}
// get database connection
$database = new Database();
$db = $database->getConnection();
// prepare product object
$wineries = new Wineries($db);
// set ID property of record to read
//$wineriest->id = isset($_GET['id']) ? $_GET['id'] : die();
// read the details of product to be edited
$stmt = $wineries->readRegions();
$num = $stmt->rowCount();
if($num > 0){
// create array
$county_arr = array();
$county_arr["records"] = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//$county_item = array(
//"county" => $County
//);
array_push($county_arr["records"], $State . "-" . $Region);
}
// set response code - 200 OK
http_response_code(200);
// make it json format
echo json_encode($county_arr);
}
else{
// set response code - 404 Not found
http_response_code(404);
// tell the user product does not exist
echo json_encode(array("message" => "counties not read"));
}
?>
Does this code look familiar? Yes, it is certainly close to the PHP routine shown above, with the exceptions that it is calling the readRegions function and sending back state and region.
In step 6 we get to move on to the PHP routines which will search for wineries, distilleries and meaderies using selected county, region or the partial name entered.
1 thought on “Building Your PHP Rest Service (step 5)”