Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone-PHP Post请求_Php_Iphone_Objective C_Post_Ios5 - Fatal编程技术网

iPhone-PHP Post请求

iPhone-PHP Post请求,php,iphone,objective-c,post,ios5,Php,Iphone,Objective C,Post,Ios5,这是我的第一个问题,我希望得到一些帮助: 我使用了几种方法(并尝试了不同的http传输框架)在localhost中对php脚本进行post操作,但没有得到好的结果。我不知道还能找什么。我已经被这个问题困扰了至少6到7个小时,最后终于得出结论: 目标C代码: if (name != nil && message != nil) { NSMutableString *postString = [NSMutableString stringWithString:TFPo

这是我的第一个问题,我希望得到一些帮助:

我使用了几种方法(并尝试了不同的http传输框架)在localhost中对php脚本进行post操作,但没有得到好的结果。我不知道还能找什么。我已经被这个问题困扰了至少6到7个小时,最后终于得出结论:

目标C代码:

if (name != nil && message != nil) {
        NSMutableString *postString = [NSMutableString stringWithString:TFPostURL];

        [postString appendString:[NSString stringWithFormat:@"%@=%@",TFName,name]];

        [postString appendString:[NSString stringWithFormat:@"%@=%@",TFMessage,message]];

        [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];

        [request setHTTPMethod:@"POST"];

        postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];


    }
tfpostrl、TFName、TFMessage、name和message是我定义的常量

我使用数据库只是为了检查传输是否成功:

PHP脚本

<?php

$username = "root";
$database = "db";

mysql_connect(localhost, $username);

@mysql_select_db($database) or die("Unable to find database");

$name = $_POST["name"];

$message = $_POST["message"];

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysql_query($query) or die(mysql_error("error"));

mysql_close();

?>
我建议使用。它们很好。下面是一些示例代码:

ASIHTTPRequest* req = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"localhost"]] autorelease];
[req setCompletionBlock:^{  
   NSLog(@"It works! Response was %@",[req responseString]);
 }];
[req startAsynchronous];
它使异步响应变得简单,并允许您像boss一样使用闭包


我已经尝试处理内置的NSURLRequest方法一段时间了,并且强烈希望能够简单地发出异步请求,而无需创建单独的委托和方法来处理它。它还支持发布数据和文件,以及一些我还没有使用过的其他技巧。

正如您所知,模拟器上的localhost应该可以工作-模拟器上的Safari可以访问我mac的http服务器。尝试在iOS模拟器的Safari浏览器中打开php脚本,看看是否会弹出数据库条目。如果是这样,那么你可以确定是应用程序代码导致了问题,而不是php。欢迎来到这里,Fernando!谢谢大家!我已经解决了我的问题。。。真的,我从没见过这样的事。检查编辑!嗨,Tim,我已经尝试了ASIHTTP库,但是没有什么好结果,尽管每个人都在使用它们,没有任何问题。这说明有些奇怪的错误。您确定您的php代码正常工作吗?您可以尝试上面的代码,但只让您的网页有一个简单的字符串,并验证模拟器是否接收到响应?什么不起作用?全部请求?发送的值?使用了网络调试器,一切正常,开始在我的php文件中进行更改,正如您所想,这就是问题所在。只需将HTTP POST更改为GETs并通过它即可。傻吧?太棒了。很高兴能为您提供帮助。请注意,AsitpRequest已不再积极开发。
ASIHTTPRequest* req = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"localhost"]] autorelease];
[req setCompletionBlock:^{  
   NSLog(@"It works! Response was %@",[req responseString]);
 }];
[req startAsynchronous];