I'm trying to set up sending data to a PHP server but I'm having no luck at all.
Here is my PHP code.
<?php
require_once "../config/config.php";
var_dump($_POST);
var_dump($_REQUEST);
// Read request parameters
if(isset($_REQUEST)){
$username = $db->escape($_REQUEST["username"]);
$email = $db->escape($_REQUEST["email"]);
$password = $db->escape($_REQUEST["password"]);
$id = MD5($email);
echo $username;
//$db->query("INSERT INTO user ('id','username','email','password') VALUES ('$id','$username','$email',PASSWORD('$password'))");
$returnValue = $id;
}else{
$returnValue = "No data received";
}
// Send back request in JSON format
echo json_encode($returnValue);
?>
Here is my Swift Code
let request = NSMutableURLRequest(URL: NSURL(string: "http://***************/register.php")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let data = "username=JoeBloggs&email=joe@bloggs.com&password=12345"
request.HTTPBody = (data as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
print(response)
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(strData)
print(error)
}
task.resume()
I'm not concerned about getting a result back yet (I have that working fine). I just can't get any data to the server.
Here is the result from the above script.
Optional(<NSHTTPURLResponse: 0x7ff9b3d6ea90> { URL: http://iep.almartin.co.uk/register.php } { status code: 200, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = close;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Tue, 01 Sep 2015 09:02:46 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = nginx;
"Set-Cookie" = "PHPSESSID=8j2d7oobg9plvdik1dcbqtoq70; path=/";
"Transfer-Encoding" = Identity;
} })
Optional(array(0) {
}
array(0) {
}
<br />
<b>Notice</b>: Undefined index: username in <b>/home/linweb34/i/iep.almartin.co.uk/user/htdocs/register.php</b> on line <b>7</b><br />
<br />
<b>Notice</b>: Undefined index: email in <b>/home/linweb34/i/iep.almartin.co.uk/user/htdocs/register.php</b> on line <b>8</b><br />
<br />
<b>Notice</b>: Undefined index: password in <b>/home/linweb34/i/iep.almartin.co.uk/user/htdocs/register.php</b> on line <b>9</b><br />
"d41d8cd98f00b204e9800998ecf8427e")
nil
As you can see both $_REQUEST and $_POST are returning empty arrays.
What am I doing wrong?