使用objective-c执行一个PHP脚本

我试图执行一个PHP脚本增加数据库中的字段。 我有脚本工作,我目前正在使用ASIHTTPRequest完美修改数据库,但我感觉好像我应该使用不同的方法,因为我不需要返回。

这就是所谓的HTTP POST?

incrementOrDecrementURL = [[NSString alloc] initWithFormat:@"http://myURL/doScript.php"]; NSURL *requestURL = [[NSURL alloc] initWithString:incrementOrDecrementURL]; //The actual request ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:requestURL]; // Becoming the request delegate //To get callbacks like requestFinished: or requestFailed: [request setDelegate:self]; // Start request [request startAsynchronous]; 

形成文档:使用ASIFormDataRequest发送表单POST

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:@"Ben" forKey:@"first_name"]; [request setPostValue:@"Copsey" forKey:@"last_name"]; [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 

但是也要注意,ASIHTTPRequest 不再由它的原始作者维护。

这将是一个单向的要求,而不是一个POST。

但是,您不应该使用GET请求来更新任何数据库,所以您应该使用POST。

一种方法是立即返回一个响应,然后更新数据库,但更好的方法,因为这个库不再维护( http://allseeing-i.com/ASIHTTPRequest/ )你可能想要使用NSUrlConnection只是忽略回应,然后继续。

ASIHTTPRequest默认发送一个GET请求,而不是POST。 GET不会更新服务器上的任何东西,所以你应该使用POST: [request setRequestMethod:@"POST"] 。 你可以让你的doScript.php返回一个HTTP 204 No Content响应。 PHP 头()函数。 但是,返回至less某种协议特定的状态代码是关于服务器上实际发生的事情的好devise。 我经常只返回一个简单的JSON响应,如{"status":"OK"}