PHP

 

Here is code that I used with to work with REST API on a LAMP (LINUX, APACHE, MYSQL, PHP) server that i setup on my LAPTOP using ubuntu and port forwarding from a router.
<html>
<head>
</head>
<body>
<p>
<?php

// Created by Daniel Taylor February 2017
// In order to test my code, uncomment the code near the bottom of
// code in the ——–function calls———- area
// Thanks for the opportunity.
// Declare Dependencies
require ‘vendor/autoload.php’;
use GuzzleHttp\Client;

// Array where we will store the Json arrays after decryption
$content_array = array();

// Create the Base uri, GET command, store it, decode the JSON
$client = new GuzzleHttp\Client([‘base_uri’ => ‘https://devassess.100tb.com/’, ‘timeout’ => 5.0,]);

//——— Item section——-
$response = $client->request(‘GET’, ‘testOrderItems/’);
$body = $response->getBody();

// Item Content
$content_array = json_decode($body,true);

// Array that will be filled with our custom class Item
$Item_array = array();

//——– Server section——-
$response = $client->request(‘GET’, ‘testServers/’);
$body = $response->getBody();

// Server Info
$s_content_array = json_decode($body,true);

// Array that will be filled with our custom class Server
$Server_array = array();
//——————————- Class Definitions ————————–
// Items
class Item {
private $id=5;
private $item_name= NULL;
private $item_desc= NULL;
private $order_group= NULL;
private $available= NULL;
private $client_id = NULL;
private $price= NULL;
function __construct($_id,$_item_name,$_item_desc,$_order_group,$_available,$_client_id,$_price)
{
$this->id = $_id ;
$this->item_name= $_item_name;
$this->item_desc = $_item_desc;
$this->order_group= $_order_group;
$this->available= $_available;
$this->client_id = $_client_id;
$this->price = $_price;
// Data modifiers
if ($this->price < 0) {$this->price = 0;}
}
// Returns a nice looking string containing all the variables of the item
public function toString() { return(“ID: “.”$this->id”.” Item Name: “. “$this->item_name”.” Item Description: “.”$this->item_desc”.” Order Group: “.”$this->order_group”.” Available: “.”$this->available”.” Client ID: “.”$this->client_id”.” Price: “.”$this->price”.”<br>”);}
// Returns group
public function getGroup() {return($this->order_group);}
// Changes price based on code
public function modPrice($dis_id)
{
if ($dis_id == “77777” && $dis_id == $this->client_id ){$this->price = $this->price*0.8;}
if ($dis_id == “12345” && $dis_id == $this->client_id ){$this->price = $this->price*0.9;}
if ($dis_id == “987789”){$this->price = $this->price*0.95;}
}
}
//——————————- Server ————————–
class Server {
private $id=5;
private $server_id= NULL;
private $server_name= NULL;
private $client_id= NULL;
private $available= NULL;
private $datacenter = NULL;
private $ip_address = NULL;
private $price= NULL;
private $server_description = NULL;
function __construct($_id,$_server_id,$_server_name,$_client_id,$_available,$_datacenter,$_ip_address,$_price,$_server_description)
{

$this->id = $_id ;
$this->server_id= $_server_id;
$this->server_name = $_server_name;
$this->client_id= $_client_id;
$this->available= $_available;
$this->datacenter = $_datacenter;
$this->ip_address = $_ip_address;
$this->price = $_price;
$this->server_description = $_server_description;

// Data modifiers
$this ->server_id = str_replace(“100TB_”, “”, $this->server_id);
}
// Returns a nice looking string containing all the variables of the item

public function toString() { return(“ID: “.”$this->id”.” Server ID: “. “$this->server_id”.” Server Name: “.”$this->server_name”.” Client ID: “.”$this->client_id”.”Available: “.”$this->available”.” Datacenter: “. “$this->datacenter”.” IP Adress: “.”$this->ip_address”.” Server Description: “.”$this->server_description”.”<br>”);}

public function getAvailability() {return($this->available);}
public function getDescription() {return($this->server_description);}
// Changes price based on code
public function modPrice($dis_id)
{
// if ($dis_id == “77777” && $dis_id == $this->client_id ){$this->price = $this->price*0.8;}
// if ($dis_id == “12345” && $dis_id == $this->client_id ){$this->price = $this->price*0.9;}
// if ($dis_id == “987789”){$this->price = $this->price*0.95;}
}

}
//————————————Necessary code and function definitions—————————
// Populate main Item array
foreach ($content_array as $counter)
{
$item = new Item($counter[“id”],$counter[“item_name”],$counter[“item_desc”],$counter[“order_group”],$counter[“available”],$counter[“client_id”],$counter[“price”]);
array_push($Item_array,$item);
}

// Sort Server Array
$id = array();
foreach ($s_content_array as $key => $row)
{
$id[$key] = $row[‘client_id’];
}

array_multisort($id, SORT_ASC, $s_content_array);

// Populate main Server array
foreach ($s_content_array as $counter)
{
$server = new Server($counter[“id”],$counter[“server_id”],$counter[“server_name”],$counter[“client_id”],$counter[“available”],$counter[“datacenter”],$counter[“ip_address”],$counter[“price”],$counter[“server_description”]);
array_push($Server_array,$server);
}

// Print out all items
function printAllItems($array, $id=NULL){

foreach ($array as $item_)
{
$item_->modPrice($id);

echo $item_->toString();
}

}
//Return Array of Items
function getAllItems()
{
return ($this->Item_array);
}

// Returns a single item
function getItem($id,$Item_array)
{
return ($Item_array[$id-1]);
}

// Prints items of the same group
function getItemsInGroup($group, $Item_array)
{

foreach ($Item_array as $item)
{

if ($item->getGroup() == $group)
{

echo $item->toString();
}
}
}

// Return servers by availability
function getServersByAvailability($available,$array)
{

foreach ($array as $server)
{

if ($server->getAvailability() == $available)
{

echo $server->toString();
}
}
}

// Return servers by description
function getServersByDescription($description,$array)
{

foreach ($array as $server)
{

if ($server->getDescription() == $description)
{

echo $server->toString();
}
}
}
//—————Function Calls——————–
// Uncomment the code to test it out
// Objective 1.a w/ optional code setting
//printAllItems($Item_array,77777);
// Objective 1.b
//echo getItem(3,$Item_array)->toString();

// Objective 1.c
//getItemsInGroup(“ram”,$Item_array);

// Objective 2.a
//printAllItems($Server_array,77777);

// Objective 2.b enter desired availability first, then the server array
//getServersByAvailability(1,$Server_array);

// Objective 2.c enter desired description first, then the server array
//getServersByDescription(“Available server”,$Server_array);

css.php