Advertisement
olcayertas

Web Service

Dec 14th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.85 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. header('Content-type: application/json; charset=utf-8');
  5.  
  6. class ArendiWebServices {
  7.  
  8.     private $db;
  9.  
  10.     // Constructor - open DB connection
  11.     function __construct() {
  12.         $this->db = new mysqli('localhost', 'root', '47yk2d8r6c', 'arendi');
  13.         $this->db->autocommit(FALSE);
  14.         $stmt1 = $this->db->prepare("SET NAMES 'utf8'");
  15.         $stmt1->execute();
  16.         $stmt2 = $this->db->prepare('SET CHARACTER SET "utf8"');
  17.         $stmt2->execute();
  18.     }
  19.  
  20.     // Destructor - close DB connection
  21.     function __destruct() {
  22.         $this->db->close();
  23.     }
  24.  
  25.     // Main method to redeem a code
  26.     function getProjects() {
  27.         error_log("getProjects: giriş\n", 3, "/php.log");
  28.         $stmt = $this->db->prepare('SELECT * FROM projects');
  29.         $stmt->bind_result($id, $no, $title, $subject, $description, $attachment, $status);
  30.  
  31.         if ($stmt->execute()) {
  32.             error_log("getProjects: if\n", 3, "/php.log");
  33.             //$result = "{\"success\":1";
  34.             $result = array();
  35.  
  36.             while ($stmt->fetch()) {
  37.                 $row = array("id" => $id, "no" => $no, "title" => $title, "subject" => $subject, "description" => $description, "attachment" => $attachment, "status" => $status);
  38.                 $result[] = $row;
  39.             }
  40.  
  41.             echo json_encode($result);
  42.         }
  43.  
  44.         $stmt->close();
  45.         error_log("getProjects: çıkış\n", 3, "/php.log");
  46.     }
  47.  
  48.     function getUser($email, $password) {
  49.         error_log("in service\n", 3, "/php.log");
  50.         $stmt = $this->db->prepare("SELECT * FROM users WHERE email=? && password=?");
  51.         $stmt->bind_param("ss", $email, $password);
  52.         $id = 0;
  53.         $companyid = 0;
  54.         $name = "";
  55.         $surname = "";
  56.         $email2 = "";
  57.         $password2 = "";
  58.         $stmt->bind_result($id, $companyid, $name, $surname, $email2, $password2);
  59.  
  60.         if ($stmt->execute()) {
  61.             if ($stmt->fetch()) {
  62.                 $stmt->close();
  63.                 error_log($id . " - " . $companyid . " - " . $name . " - " . $surname . " - " . $email2 . " - " . $password2 . "\n", 3, "/php.log");
  64.                 $stmt2 = $this->db->prepare("SELECT * FROM companies WHERE id=?");
  65.                 $stmt2->bind_param("i", $companyid);
  66.                 $cid = 0;
  67.                 $cname = "";
  68.                 $caddress = "";
  69.                 $cphone = "";
  70.                 $cwebpage = "";
  71.                 $stmt2->bind_result($cid, $cname, $caddress, $cphone, $cwebpage);
  72.                 $result = "{\"success\":1,";
  73.                 $result = $result . "\"id\":\"" . $id . "\",";
  74.                 $result = $result . "\"companyid\":\"" . $cid . "\",";
  75.                 $result = $result . "\"companyname\":\"" . $cname . "\",";
  76.                 $result = $result . "\"name\":\"" . $name . "\",";
  77.                 $result = $result . "\"surname\":\"" . $surname . "\",";
  78.                 $result = $result . "\"email\":\"" . $email2 . "\",";
  79.                 $result = $result . "\"password\":\"" . $password2 . "\"}";
  80.                 $stmt2->close();
  81.                 //echo "{\"success\":1,\"id\":\"".$id."\",\"companyid\":\"".$cid."\",\"companyname\":\"".$cname."\",\"name\":\"".$name."\",\"surname\":\"".$surname."\",\"email\":\"".$email2."\",\"password\":\"".$password2."\"}";
  82.                 echo $result;
  83.                 error_log($cid . " - " . $cname . " - " . $caddress . " - " . $cphone . " - " . $cwebpage . "\n", 3, "/php.log");
  84.             } else {
  85.                 echo "{\"success\":0,\"error_message\":\"Kullanıcı adı ya da" . "şifre hatalı.1\",\"email\":\"" . $email . "\",\"password\":\"" . $password . "\"}";
  86.             }
  87.         } else {
  88.             echo "{\"success\":0,\"error_message\":\"Kullanıcı adı ya da " . "şifre hatalı.2\"}";
  89.         }
  90.     }
  91.  
  92.     function getFoods() {
  93.         error_log("getFoods: giriş\n", 3, "/php.log");
  94.         $stmt = $this->db->prepare("SELECT * FROM yiyecekler");
  95.         $stmt->bind_result($id, $name, $imageUrl, $price, $ingredient, $cookingTime, $star, $videoUrl, $category, $recipe);
  96.  
  97.         if ($stmt->execute()) {
  98.             error_log("getFoods: if\n", 3, "/php.log");
  99.             //$result = "{\"success\":1";
  100.             $result = array();
  101.  
  102.             while ($stmt->fetch()) {
  103.                 $row = array("id" => $id, "name" => $name, "imageUrl" => $imageUrl, "price" => $price, "ingredient" => $ingredient, "cookingTime" => $cookingTime, "star" => $star, "videoUrl" => $videoUrl, "category" => $category, "recipe" => $recipe);
  104.                 $result[] = $row;
  105.             }
  106.  
  107.             echo json_encode($result);
  108.         }
  109.  
  110.         $stmt->close();
  111.         error_log("getFoods: çıkış\n", 3, "/php.log");
  112.     }
  113.  
  114.     function getCategories() {
  115.         error_log("getCategory: giriş\n", 3, "/php.log");
  116.         $stmt = $this->db->prepare('SELECT * FROM category');
  117.         $stmt->bind_result($id, $categoryName);
  118.  
  119.         if ($stmt->execute()) {
  120.  
  121.             $result = array();
  122.             while ($stmt->fetch()) {
  123.                 $row = array("id" => $id, "category" => $categoryName);
  124.                 $result[] = $row;
  125.             }
  126.  
  127.             echo json_encode($result);
  128.         }
  129.  
  130.         $stmt->close();
  131.         error_log("getCatergory: çıkış\n", 3, "/php.log");
  132.     }
  133.  
  134.     function getCookbyName($name) {
  135.         //statement
  136.         $stmt = $this->db->prepare("SELECT * FROM restoran WHERE name=? ");
  137.         $stmt->bind_param("s", $name);
  138.         $stmt->bind_result($id, $name2, $imageurl, $price, $ingredient, $cookingTime, $star, $videoUrl, $category, $recipe);
  139.  
  140.         if ($stmt->execute()) {
  141.             if ($stmt->fetch()) {
  142.                 //printf("Kullanıcı adı şifre veri tabanında mevcut: %s %s\n",
  143.                 //$email, $password);
  144.                 //echo '{"success":1,"email":"' . $email2 . '","password":"' .
  145.                 //$password2 . '"}';
  146.                 echo '{"success":1,"name":"' . $name2 . '","imageUrl":"' . $imageurl . '","price":"' . $price . '"}';
  147.             } else {
  148.                 echo "{\"success\":0,\"error_message\":\"Yemek adı hatalı.1\"," . "\"email\":\"" . $name . '"}';
  149.             }
  150.         } else {
  151.             echo '{"success":0,"error_message":"Yemek adi hatali.2"}';
  152.         }
  153.        
  154.         $stmt->close();
  155.     }
  156.  
  157.     function getMenu() {
  158.         $menu = array("İmam Bayıldı", "Kuru Fasulye", "Pilav", "Mercimek Çorbası", "Bamya");
  159.         echo json_encode($menu);
  160.     }
  161. }
  162.  
  163. $post = filter_input_array(INPUT_POST);
  164.  
  165. if (isset($post['username']) && isset($post['password'])) {
  166.     error_log("Arendi Web Services\n", 3, "/php.log");
  167.     error_log("Post data loaded.\n", 3, "/php.log");
  168.     $arendiService = new ArendiWebServices();
  169.     error_log("Service instance created\n", 3, "/php.log");
  170.     error_log("servis: login\n", 3, "php.log");
  171.     error_log($post['username'] . " - " . $post['password'] . "\n", 3, "php.log");
  172.     if ($arendiService)
  173.         $arendiService->getUser($post['username'], $post['password']);
  174.     else
  175.         error_log("servis: failed to create service instane\n", 3, "php.log");
  176. }
  177. //  else if (isset($post['request'])) {
  178. // error_log("servis: getprojects\n", 3, "/php.log");
  179. // if ($post['request'] == "getprojects") {
  180. // error_log("servis: request - getprojects\n", 3, "/php.log");
  181. // $arendiService->getProjects();
  182. // } else if ($post['request'] == "getMenu") {
  183. // error_log("servis: request - getMenu", 3, "/php.log");
  184. // $arendiService->getMenu();
  185. // } else if ($post['request'] == "getCategories") {
  186. // error_log("servis: request - getMenu", 3, "/php.log");
  187. // $arendiService->getCategories();
  188. // } else {
  189. // error_log("servis: tanımsız servis\n", 3, "/php.log");
  190. // echo '{"success":0,"error_message":"Tanımsız istek!"}';
  191. // }
  192. // } else {
  193. // error_log("servis: son\n", 3, "/php.log");
  194. // echo '{"success":0,"error_message":"Kullanıcı adı ya da şifre hatalı."}';
  195. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement