[英]sending a post from iOS app to php script not working… Simple solution is likey
我之前已经做了好几次了但是由于某些原因我无法通过这个帖子...我尝试了设置为_POST且没有的变量的php脚本......当它们未设置为发布时它工作精细。 这是我的iOS代码:
NSDate *workingTill = timePicker.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *time = [formatter stringFromDate:workingTill];
NSString *post = [NSString stringWithFormat:@"shift=%@&username=%@", time, usernameString];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
NSURL *url = [NSURL URLWithString:@"http://wowow.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSLog(@"%@", post);
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
这里是php的一大块,POST变量不在正确的位置?
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$newShift = $_POST('shift');
$bartenderUsername = $_POST('username');
mysql_connect("host", "name", "pw") or die(mysql_error());
mysql_select_db("harring4") or die(mysql_error());
$result = mysql_query("SELECT * FROM BartenderTable WHERE username='".$bartenderUsername."'") or die(mysql_error());
$row = mysql_fetch_array($result);
$newfname = $row['fname'];
我想这对于更有经验的开发人员来说是一个相当简单的答案,感谢您的帮助!
$_POST
是一个数组,而不是一个函数。 您需要使用方括号来访问数组索引:
$newShift = $_POST['shift'];
$bartenderUsername = $_POST['username'];
使用以下代码。 确保帖子字符串的收据是一个密钥,并将在服务器中使用。 客户端代码
NSString *receipt1 = @"username";
NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
{
NSLog(@"Response: %@", result);
}
}
服务器端PHP脚本。
validation.php
<?php
if(_POST)
{
if($_POST['receipt'] == 'username')
{
echo "post successfull";
$receipt = $_POST['key1'];
echo $receipt;
}
else
{
echo "not post";
}
?>
由于我是iPhone开发人员,我可以说你的Objective-C代码是正确的。 还要确保您发送的数据不是空的
NSLog(@"%d,[postData length]");
它不应该打印0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.