我已经全神贯注,想要从一个字符串的iPhone应用程序进行简单的上传。我已经在线使用了所有代码。 iPhone有效。它从php文件中检索代码并将其打印到控制台。但是php脚本中的值返回空。
<?php
$string = $_POST["string"];
if(strlen ($string)==0){
echo"empty";
}else{
echo "The string is: ". $string;
}
?>
这是iOS代码
- (void) sendCode{
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"string=%@&",signInCode.text];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://artistfolio.net/signin.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Good");
}
else
{
NSLog(@"Bad");
}
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
}
答案 0 :(得分:0)
也许iOS会将数据发送到请求正文中。
尝试file_get_contents('php://input')
进入服务器端。或者将Content-Type: application/x-www-form-urlencoded
标题更改为客户端。
在第二种情况下,您应该阅读:POST request using application/x-www-form-urlencoded
答案 1 :(得分:0)
查看此答案,最初发布here。
PHP代码
<?php
$title = $_POST['title'];
$description = $_POST['description'];
$city = $_POST['city'];
echo "Title: ". $title;
echo "Description: ". $description;
echo "City: ". $city;
?>
iOS端编码:
目标C
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",eventTitle.text,eventDescription.text,eventCity.text];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.youardomain.com/phpfilename.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
<强>夫特强>
// Create your request string with parameter name as defined in PHP file
var myRequestString: String = "title=\(eventTitle.text!)&description=\(eventDescription.text!)&city=\(eventCity.text!)"
// Create Data from request
var myRequestData: NSData = NSData.dataWithBytes(myRequestString.UTF8String(), length: myRequestString.characters.count)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.youardomain.com/phpfilename.php")!)
// set Request Type
request.HTTPMethod = "POST"
// Set content-type
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
// Set Request Body
request.HTTPBody = myRequestData
// Now send a request and get Response
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
// Log Response
var response: String = String(bytes: returnData.bytes(), length: returnData.characters.count, encoding: NSUTF8StringEncoding)
NSLog("%@", response)