Reputation: 554
I am creating simple application. I have a one php script that accept 5 parameters 'firstname','lastname','email','password' and 'key'.
This is my php file code.
<?php
define("SERVER", 'localhost');
define("USER", 'root');
define("PASSWORD", '');
define("DB", 'test');
mysql_connect(SERVER,USER,PASSWORD,DB);
$con = mysql_select_db(DB);
if($_SERVER['REQUEST_METHOD'] == "POST"){
$firstname = isset($_POST['firstname']) ? mysql_real_escape_string($_POST['firstname']) : "";
$lastname = isset($_POST['lastname']) ? mysql_real_escape_string($_POST['lastname']) : "";
$email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";
$password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";
$key = isset($_POST['key']) ? mysql_real_escape_string($_POST['key']): "";
if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($password) && !empty($key) ){
if ($key == "12345") {
$sql = "INSERT INTO `users` (`firstname`,`lastname`, `email`, `password`) VALUES ('$firstname', '$lastname', '$email', '$password');";
$qur = mysql_query($sql);
$json = array("status" => 1, "msg" => "Done User added!");
}
else{
$json = array("status" => 0, "msg" =>"key not match");
}
}
else{
$json = array("status" => 0, "msg" => "Error adding user!");
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
@mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>
This is screenshot of Advanced Rest Client.
All is going good.But when i am trying to post request from ios app using AFNetworking i always getting wrong response.
This is my Objective-C code.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
[parameters setValue:@"ramu" forKey:@"firstname"];
[parameters setValue:@"kaka" forKey:@"lastname"];
[parameters setValue:@"[email protected]" forKey:@"email"];
[parameters setValue:@"11111" forKey:@"password"];
[parameters setValue:@"12345" forKey:@"key"];
AFHTTPSessionManager *managertwo = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
managertwo.requestSerializer = [AFJSONRequestSerializer serializer];
[managertwo.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[managertwo POST:[NSString stringWithFormat:@"http://localhost:80/Testing/jsonpost.php"] parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success! %@",responseObject);
NSLog(@"%@",parameters);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
}];
}
And I am Getting Response like this
2016-02-16 12:31:36.783 TestingLocal[1626:110064] success! {
msg = "Error adding user!";
status = 0;
}
2016-02-16 12:31:36.783 TestingLocal[1626:110064] {
email = "[email protected]";
firstname = ramu;
key = 12345;
lastname = kaka;
password = 11111;
}
I am not understand why this happen.I searched everywhere what is the problem but i am getting nothing. Please help.
Upvotes: 1
Views: 195
Reputation: 775
In the server code getting data in $_POST, try using following:
$postdata = file_get_contents("php://input");
//replace $_POST with $postdata
Actually php://input allows you to read raw POST data.
It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.
php://input is not available with enctype="multipart/form-data".
refer http://infopotato.com/blog/index/post
Upvotes: 2