title: Connecting Swift with php server
body: <p>I'm trying to post from swift to php and then return json encoded data back to swift.</p>
  
  <p>However, <code>print(task.response)</code> is nil. My <a href="https://www.flashkase.com/API/createAccount.php" rel="nofollow">link</a> does what it is supposed to but my app still responds with nil. Any idea why?</p>
  
  <pre><code>let request = NSMutableURLRequest(URL: NSURL(string: "https://www.flashkase.com/API/createAccount.php")!)
  request.HTTPMethod = "POST"
  let postString = "username=frogg222"
  request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
      guard error == nil &amp;&amp; data != nil else { // check for fundamental networking error
          print("error=\(error)")
          return
      }
  
      if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
          print("statusCode should be 200, but is \(httpStatus.statusCode)")
          print("response = \(response)")
      }
  
      let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
      print("responseString = \(responseString)")
  }
  task.resume()
  print(task.response)
  </code></pre>
  
  <p>And my PHP file</p>
  
  <pre><code>&lt;?php 
  include($_SERVER["DOCUMENT_ROOT"] . "/inc/database.php");
  $arr = [];
  
  $arr[] = 'Reporting for duty';
  
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
      $arr[] = 'Post received';
      $arr[] = $_POST['username'];
  }
  
  echo json_encode($arr);
  ?&gt;
  </code></pre>
  
  <p>Update: How can I check for data after completion? If I uncomment sleep(3) it will display results...but that's obviously not ideal.</p>
  
  <pre><code>func postToServer(postURL: String, postString: String) {
  
      let request = NSMutableURLRequest(URL: NSURL(string: postURL)!)
      request.HTTPMethod = "POST"
      request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  
      let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
          data, response, error in 
  
          print("\(data), \(response), \(error)")
  
      }
      task.resume()
      //sleep(3)
  }
  
  
  let sendURL: String = "https://www.flashkase.com/API/createAccount.php"
  let sendData: String = "username=frogg222&amp;email=fake123@gmail.com&amp;password=fake1234"
  
  postToServer(sendURL,postString: sendData)
  </code></pre>