简体   繁体   English

使用 objective-c 执行 PHP 脚本

[英]Executing a PHP script using objective-c

I'm trying to execute a PHP script which increments a field in a database.我正在尝试执行一个 PHP 脚本,该脚本会增加数据库中的一个字段。 I have the script working and I am currently modifying the database perfectly using ASIHTTPRequest, but I feel as though I should be using a different method given that I do not need a return.我有脚本工作,我目前正在使用 ASIHTTPRequest 完美地修改数据库,但我觉得我应该使用不同的方法,因为我不需要返回。

Is this what's called an HTTP POST?这就是所谓的 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]; 

form the docs: Sending a form POST with ASIFormDataRequest形成文档:Sending a form POST with ASIFormDataRequest

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"];

But also note, that ASIHTTPRequest is not maintained anymore by it's original author.但还要注意,ASIHTTPRequest不再由其原始作者维护。

ASIHTTPRequest by default sends a GET request, not POST. ASIHTTPRequest默认发送 GET 请求,而不是 POST。 It's proper that GETs don't update anything at the server, so you should use a POST: [request setRequestMethod:@"POST"] . GET 不更新服务器上的任何内容是正确的,因此您应该使用 POST: [request setRequestMethod:@"POST"] You can just have your doScript.php return an HTTP 204 No Content response.您可以只让doScript.php返回 HTTP 204 No Content 响应。 PHP header() function. But it's good design to return at least some sort of protocol-specific status code regarding what actually happened at the server. PHP header() function。但是,至少返回某种关于服务器实际发生的协议特定状态代码是一个很好的设计。 I often just return a simple JSON response such as {"status":"OK"} .我通常只返回一个简单的 JSON 响应,例如{"status":"OK"}

It would be a oneway request, not a POST.这将是单向请求,而不是 POST。

But, you should never update any database with a GET request, so you should use a POST.但是,您不应该使用 GET 请求更新任何数据库,因此您应该使用 POST。

One way to do this is to immediately return a response, and then update the database, but the better approach, since this library is no longer maintained ( http://allseeing-i.com/ASIHTTPRequest/ ) you may want to use NSUrlConnection and just ignore the response, and go on.一种方法是立即返回响应,然后更新数据库,但更好的方法是,由于不再维护此库 ( http://allseeing-i.com/ASIHTTPRequest/ ),您可能需要使用 NSUrlConnection并忽略响应,然后打开 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM