Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. let myUrl = NSURL(string: "myURL");
  2. let request = NSMutableURLRequest(URL: myUrl!);
  3. request.HTTPMethod = "POST";
  4.  
  5. let params : [String : AnyObject] = ["username": username!, "password": password!];
  6. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type");
  7.  
  8. do{
  9. let jsonData = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions());
  10. request.HTTPBody = jsonData;
  11. let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
  12. print(jsonString);
  13. print(request.HTTPBody);
  14.  
  15. }catch {
  16. print(error)
  17. }
  18.  
  19. let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
  20.  
  21. do{
  22. let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as? NSDictionary
  23.  
  24. print(response)
  25. if let parseJSON = json {
  26. let resultValue = parseJSON["type"] as? Int
  27. print("result: (resultValue)")
  28. }
  29. }catch{
  30. print(error)
  31. }
  32. })
  33.  
  34. task.resume()
  35.  
  36. require("Conn.php");
  37. require("MySQLDao.php");
  38.  
  39.  
  40. $fp = fopen("data.txt", "a+");
  41.  
  42.  
  43. $data = file_get_contents('php://input');
  44. $json = json_decode($data, false);
  45. $username = $_POST['username'];
  46. $password = $_POST['password'];
  47.  
  48.  
  49. fwrite($fp, $data);
  50. fwrite($fp, $json);
  51. fwrite($fp, $username);
  52. fwrite($fp, $password);
  53. fclose($fp);
  54. $returnValue = array();
  55.  
  56. if(empty($username) || empty($password))
  57. {
  58. $returnValue["status"] = "error";
  59. $returnValue["message"] = "Missing required field";
  60. echo json_encode($returnValue);
  61. return;
  62. }
  63.  
  64.  
  65. $dao = new MySQLDao();
  66. $dao->openConnection();
  67. $userType = $dao->passwordAuthentification($username, $password);
  68.  
  69. if($userType != null){
  70. $returnValue["type"] = (int)$userType;
  71. echo json_encode($returnValue);
  72. }else{
  73. $returnValue["type"] = -1;
  74. $returnValue["message"] = "user is not found";
  75. echo json_encode($returnValue);
  76. }
  77.  
  78. $dao->closeConnection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement