Php 将文本从label.text更新到服务器并将其保存在文本文件中
我正在尝试将label.text上载到服务器上的文本文件中。这是我的代码。它并没有显示任何错误,只是在服务器上创建了文件,但其中并没有文本。请帮忙Php 将文本从label.text更新到服务器并将其保存在文本文件中,php,objective-c,xcode,http-method,Php,Objective C,Xcode,Http Method,我正在尝试将label.text上载到服务器上的文本文件中。这是我的代码。它并没有显示任何错误,只是在服务器上创建了文件,但其中并没有文本。请帮忙 NSString *post =[[NSString alloc] initWithFormat:@"VIN: %@",vincode.text]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
NSString *post =[[NSString alloc] initWithFormat:@"VIN: %@",vincode.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData
length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]
autorelease];
[request setURL:[NSURL URLWithString:@"Server Detail Here/upload_vin.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSLog(@"VIN is %@",vincode.text);
NSString *outputdata = [[NSString alloc] initWithData:urlData
encoding:NSASCIIStringEncoding];
NSLog(@"Outputdata is %@",outputdata);
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error==nil)
NSLog(@"Error is nil");
else
NSLog(@"Error is not nil");
NSLog(@"success!");
PHP file::::::::::::::
<?php
$file = "VinCodes.txt";
#$submitted_vincode = $_GET['VIN'] . " \n";
$submitted_vincode = @$_POST['VIN'] . " \n";
if(!empty($submitted_vincode)){
$Handle = fopen($file, 'a');
fwrite($Handle, $submitted_vincode);
print "Vin Code stored in file.";
fclose($Handle);
}else{
print "Vin code is missing. Try again";
}
?>
NSString*post=[[NSString alloc]initWithFormat:@“VIN:%@”,vincode.text];
NSData*postData=[post数据使用编码:NSASCISTRINGENCODING:NSASCISTRINGENCODING
allowLossyConversion:否];
NSString*postLength=[NSString stringWithFormat:@“%d”,[postData
长度]];
NSMutableURLRequest*请求=[[[NSMutableURLRequest alloc]init]
自动释放];
[请求设置URL:[NSURL URLWithString:@“此处服务器详细信息/upload_vin.php”];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“application/x-www-form-urlencoded”
forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
NSData*urlData=[NSURLConnection sendSynchronousRequest:请求
returningResponse:nil错误:nil];
NSLog(@“成功!接收到%d字节的数据”,[urlData长度];
NSLog(@“VIN为%@”,vincode.text);
NSString*outputdata=[[NSString alloc]initWithData:urlData
编码:NSASCIIStringEncoding];
NSLog(@“Outputdata为%@”,Outputdata);
NSURLResponse*响应;
n错误*错误;
[NSURLConnection sendSynchronousRequest:请求返回应答:&响应错误:&错误];
如果(错误==nil)
NSLog(@“错误为零”);
其他的
NSLog(@“错误不是零”);
NSLog(@“成功!”);
PHP文件:
对于许多代码来说,下面是我要做的(注意我上传的是异步的):
顺便问一下,您确定要NSASCIIStringEncoding
而不是NSUTF8StringEncoding
如果有疑问,我会使用Web Proxy(免费30天)查看发送的内容和响应 $filecontents=file_get_contents(“VinCodes.txt”);打印$filecontents;请您添加此代码并打印您的文件,谢谢zaph帮助我,但请您告诉我我的确切代码是什么?事实上,我是objective c的新手,因此如果您告诉main整个代码部分,将非常有帮助。我可以用sendAsync完成吗?如果是,请提供完整的代码?[请求设置HttpBody:数据];你没有这条线吗???请解释这里的数据是什么??(根据我的代码)
NSURL *url = [NSURL URLWithString: @"http://..."];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request ...