/** * Checks if string is encoded with JSON data * @param string $string * @return boolean */ public static function isJSON($string) { try { JSONUtil::decode($string); } catch (Exception $e) { return false; } return true; }
private function setCookie($key, $val) { $exp = $this->opt['expire'] ? $this->opt['expire'] : 0; if (false === $val || null === $val) { $exp = time() - 3600; } else { if (is_array($val)) { $val = JSONUtil::arrayToJSON($val); } } //else $val = json_encode($val); $this->log[] = "Set cookie: [{$key}]=[{$val}]"; setcookie($key, $val, $exp, $this->opt['path'], $this->opt['domain'], $this->opt['secure']); }
/** Returns a JSON string containing the message data. The format of the contained JSON depends on the $withWrapper parameter. If it is true then the output will look a little something like (but quoted using JSON conventions): {$this->getWrapperName(): { ... other properties ... } } if $withWrapper is false then the outermost container object is omitted. */ public function toJSON($withWrapper = true) { return JSONUtil::arrayToJSON($withWrapper ? array($this->getWrapperName() => $this->fields) : $this->fields); }
<?php include 'util.php'; include 'chat_service_db_pdo.php'; header('Content-Type: application/json'); $entityBody = file_get_contents('php://input'); // $entityBody='{ // "email":"*****@*****.**", // "password":"******" // }'; $userInfo = JSONUtil::getArray($entityBody); $db = new ChatServiceDB(); $exisiting_id = $db->getUserId($userInfo['email'], $userInfo['password']); if ($exisiting_id != NULL) { $user = $db->getUserInfoJSON($exisiting_id); if ($user != null) { echo $user; } else { echo Error::getDataBaseError(); } } else { echo Error::getLoginFailedError(); } ?>
<?php include 'util.php'; include 'chat_service_db_pdo.php'; header('Content-Type: application/json'); $entityBody = file_get_contents('php://input'); //$entityBody='{ // "sender_user_id": 1, // "receiver_user_id": 2, // "message" : "Example text" //}'; $message_info = JSONUtil::getArray($entityBody); $db = new ChatServiceDB(); $insert_id = $db->sendMessage($message_info['sender_user_id'], $message_info['receiver_user_id'], $message_info['message']); if ($insert_id != NULL) { $response = array("success_code" => 200, "success_title" => "Message Sent", "success_message" => "Message was sent successfully"); $json_response = json_encode($response); echo $json_response; } else { echo Error::getDataBaseError(); }