Reputation: 2648
I am trying to post a NSDictionary (encoded as Json format) to webserver (handled by php), in php, i am trying to decode json data and retrieve the variables one by one, then insert these variables into mysql DB here are some of my code:
-(void)postTest{
//build up the request that is to be sent to the server
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:8888/index.php"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
//create data that will be sent in the post
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"p7878" forKey:@"id"];
[dictionary setValue:@"tmd" forKey:@"first_name"];
[dictionary setValue:@"nmb" forKey:@"last_name"];
[dictionary setValue:@"goog" forKey:@"industry"];
//serialize the dictionary data as json
NSData *data = [[dictionary copy] JSONValue];
[request setHTTPBody:data]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}
i can see those data are post to webserver successfully. but somehow i cannot deserilized the json data and stored in different variables on php:
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","wenetwork");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($_SERVER['HTTP_METHOD'] === 'postValues'){
$body;
/*Sometimes the body data is attached in raw form and is not attached
to $_POST, this needs to be handled*/
if($_POST == null){
$handle = fopen('php://input', 'r');
$rawData = fgets($handle);
$body = json_decode($rawData);
}
else{
$body == $_POST;
}
echo json_encode($body);//just return the post you sent it for testing purposes
$id = $body['id'];
$first_name = $body['first_name'];
$last_name = $body['last_name'];
$industry = $body['industry'];
$insertProfile = "INSERT INTO basic_profile (id, first_name, last_name, industry) VALUES ('". $id ."','". $first_name ."','". $last_name ."','". $industry ."')";
mysqli_query($con, $insertProfile) or die (mysqli_error("Insert item's profile failed"));
echo "Item added successfully!";
}
mysqli_close($con);
?>
those $id, $firstname etc. are not able to hold correct data which sent from ios... therefore, when i excuted the sql statement, it doesn't work... can someone suggest me with solutions? cheers
Upvotes: 1
Views: 915
Reputation: 2648
problem solved by myself, just replace the if statement in php with following...
if ($_SERVER['HTTP_METHOD'] === 'postValues'){
$jsonArray;
/*Sometimes the body data is attached in raw form and is not attached
to $_POST, this needs to be handled*/
if($_POST == null){
$jsonString = file_get_contents('php://input');
$jsonArray = json_decode($jsonString, true);
}
else{
$jsonArray == $_POST;
}
echo json_encode($jsonArray);//just return the post you sent it for testing purposes
// with [] instead of ()
$email = $jsonArray['firstkey'];
$id = $jsonArray['id'];
$first_name = $jsonArray['first_name'];
$last_name = $jsonArray['last_name'];
$industry = $jsonArray['industry'];
$insertProfile = "INSERT INTO basic_profile (id, first_name, last_name, industry) VALUES ('". $id ."','". $first_name ."','". $last_name ."','". $industry ."')";
mysqli_query($con, $insertProfile) or die (mysqli_error("Insert item's profile failed"));
echo "Item added successfully!";
}
Upvotes: 2