How to make a Php response to Objective-C (iPhone)?
My problem is that I don't know how to send the data that I find (with Select From Where) in PHP to Objective-C. I know I need to make a dataset but how do I do it? Some people say I should use JSON but I can't find any solution. Indeed, I don't know how to make a response in PHP? Can anyone help me?
This is my PHP code:
$con = mysql_connect("url","username","password");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("Appiness", $con);
$bilgi= mysql_query("SELECT c_id FROM country WHERE c_name='$country'");
while($sutun= mysql_fetch_array($bilgi))
{
echo $sutun["c_id"];
}
mysql_close($con);
This is my Objective-C code:
NSURL *url = [NSURL URLWithString:@"someurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:year1 forKey:@"year"];
[request setPostValue:appy_level forKey:@"appy_level"];
[request setPostValue:reasons forKey:@"reasons"];
[request setPostValue:country forKey:@"country"];
[request setPostValue:city forKey:@"city"];
[request setPostValue:sex forKey:@"sex"];
[request setRequestMethod:@"POST"];
[request setValidatesSecureCertificate:NO];开发者_如何学运维
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request startAsynchronous];
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
NSLog(@"%@",response);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
}
Assuming you know xml parsing you can response with xml as follows using php code.
<?php
include_once('config.php');
header("Content-Type: text/xml");
$error_respond ='<?xml version="1.0" encoding="utf-8"?><result>failure</result>';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$return_val='<?xml version="1.0" encoding="utf-8"?>';
$id = $_POST['userid'];
$return_val=$return_val.'<UserDetails>';
$selusersql ="select * from users where userid = ".$id;
$selusertrs = mysql_query($selusersql);
$selusernum = mysql_num_rows($selusertrs);
while($userrow = mysql_fetch_array($selusertrs))
{
$return_val=$return_val.'<UserID>'.$userrow['User_ID'].'</UserID>';
$return_val=$return_val.'<UserName>'.$userrow['User_Name'].'</UserName>';
$return_val=$return_val.'<Email>'.xmlentities($userrow['User_Email']).'</Email>';
}
$return_val=$return_val.'</UserDetails>';
echo $return_val;
}
else
{
echo $error_respond;
exit;
}
?>
Sample Output of above code will be (assuming user id posted 1 and details stored in db is as follows)
<?xml version="1.0" encoding="utf-8"?>
<UserDetails>
<UserID>1</UserID>
<UserName>DummyUser</UserName>
<Email>Dummy@Dummy.com</Email>
</UserDetails>
if error occurred like someone tried to call your url from browser directly i.e. request method is not POST
so we will return following response showing error. (I believe post is better than get).
<?xml version="1.0" encoding="utf-8"?>
<result>failure</result>
Now last important part is you have to parse this response according to your requirement. Search around and you will get xmlparsing demo like how to do xml parsing in iphone.
精彩评论