当我尝试将字段从iOS更新/PUT到PHP服务器时,却在那里没有得到这些值。
这是我的 iOS 代码
NSString *url=@"http://example.com/project/php/basicinfo.php";
NSString *stringData = [NSString stringWithFormat:@"fname=%@&mname=%@&lname=%@&email=%@&country=%@&city=%@&dob=%@&role=%@",firstNameCell.fname.text ,mnameCell.mname.text,lnameCell.lname.text,emailCell.email.text,[countryCell.country currentTitle],cityCell.city.text,[dobCell.dob currentTitle],@"Model"];
NSLog(@"%@",stringData);
NSURL *aUrl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"UPDATE"];
[request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
当我在PHP中echo
时,它是空的。
请帮助我
这是接收响应
Did Receive Response <NSHTTPURLResponse: 0x14ef17e40> { URL: http://example.com/project/php/basicinfo.php } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 1;
"Content-Type" = "text/html; charset=utf-8";
Date = "Fri, 15 Jan 2016 15:15:40 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Ubuntu)";
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.11";
} }
PHP代码
<?php
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$country=$_POST['country'];
$city=$_POST['city'];
$dob=$_POST['dob'];
$role=$_POST['role'];
echo $name;
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
exit();
} else {
mysqli_close($link);
echo $email;
$sql="UPDATE `basicinfo` SET `fname`='$fname',`mname`='$mname',`lname`='$lname',`email`='$email',`country`='$country',`city`='$city',`dob`='$dob',`role`='$role' WHERE `email`='$email'";
if ($conn->query($sql) === TRUE) {
// echo $email;
} else {
echo "fail";
}
//printf("Current character set: %sn", mysqli_character_set_name($conn));
}
?>
如果你使用更新HTTP方法调用你的脚本$_POST将不起作用。
你可以做这样的事情:
$rest = $_SERVER['REQUEST_METHOD'];
if(isset($_SERVER['CONTENT_TYPE'])) {
$content_type = $_SERVER['CONTENT_TYPE'];
}
if ($rest=="PUT") {
$body = file_get_contents("php://input");
parse_str($body, $parsed);
switch ($content_type)
{
case "application/json":
// do stuff
break;
case "application/x-www-form-urlencoded":
// do stuff
break;
}
break;
}
}
或使用开机自检