SHARE
TWEET

Untitled

a guest May 23rd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // required headers
  3. header("Access-Control-Allow-Origin: *");
  4. header("Content-Type: application/json; charset=UTF-8");
  5. header("Access-Control-Allow-Methods: POST");
  6. header("Access-Control-Max-Age: 3600");
  7. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  8.  
  9. // get database connection
  10. include_once '../config/database.php';
  11.  
  12. // instantiate product object
  13. include_once '../objects/product.php';
  14.  
  15. $database = new Database();
  16. $db = $database->getConnection();
  17.  
  18. $product = new Product($db);
  19.  
  20. // get posted data
  21. $data = json_decode(file_get_contents("php://input"));
  22.  
  23. // make sure data is not empty
  24. if (
  25.     !empty($data->name) &&
  26.     !empty($data->price) &&
  27.     !empty($data->description) &&
  28.     !empty($data->category_id)
  29. ) {
  30.  
  31.     // set product property values
  32.     $product->name = $data->name;
  33.     $product->price = $data->price;
  34.     $product->description = $data->description;
  35.     $product->category_id = $data->category_id;
  36.     $product->created = date('Y-m-d H:i:s');
  37.  
  38.     // create the product
  39.     if ($product->create()) {
  40.  
  41.         // set response code - 201 created
  42.         http_response_code(201);
  43.  
  44.         // tell the user
  45.         echo json_encode(array("message" => "Product was created."));
  46.     }
  47.  
  48.     // if unable to create the product, tell the user
  49.     else {
  50.  
  51.         // set response code - 503 service unavailable
  52.         http_response_code(503);
  53.  
  54.         // tell the user
  55.         echo json_encode(array("message" => "Unable to create product."));
  56.     }
  57. }
  58.  
  59. // tell the user data is incomplete
  60. else {
  61.  
  62.     // set response code - 400 bad request
  63.     http_response_code(400);
  64.  
  65.     // tell the user
  66.     echo json_encode(array("message" => "Unable to create product. Data is incomplete."));
  67. }
  68. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top