iOS 和 PHP (Web) 服务问题之间的通信


Communication between iOS and PHP (Web) Service Issues

好的,所以我想创建一个与WEB服务通信的iPhone应用程序。但是有两点我不清楚。

  1. 当用户按下应用中的按钮时,它会通知 Web 服务该操作,Web 服务会向相应的用户发送推送通知。自 No.必须向其发送通知的用户可能很大,我正在考虑向iPhone应用程序发送响应(正常/错误),然后继续发送通知。换句话说,PHP 脚本应该接受请求,响应它,并在响应后继续发送通知,以便 iPhone 应用程序不必等待服务。

  2. 该服务将需要向许多用户发送推送通知。我正在用PHP编写Web服务。那么有没有更有效的PHP替代品呢?还是这是实现服务的最佳方式?

谢谢。

好吧,对于#1,你可以做这样的事情......

在手机应用中有一个方法,例如:

- (void) send_to_server {
    NSString *urlString = [NSString stringWithFormat:@"http://weburl.com/?variable=%@", variable];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        receivedData = [NSMutableData data];
        if ([[NSString stringWithFormat:@"%@", receivedData] isEqualToString:@"Success"]){
            //Do something here
        }else{
            //Do something else here
        }
    }
}

在你的PHP中,你可以做这样的事情:

<?php
$variable = $_GET['variable'];
//do what you need to do here...
//Put things into queue or what-not
if ($success){
    echo "Success";
}else{
    echo "Failure";
}
?>

更新

<?php
if (isset($_GET['variable']){
    echo "Success";
    $variable = $_GET['variable'];
    //do what you need to do here...
}else{
    echo "Failure";
}
?>

至于向其他手机发送推送通知,我不知道该怎么做。 但我认为PHP在效率方面没有问题。

我假设关键问题是您希望立即响应用户,然后执行其他工作,例如异步发送通知。

我建议使用某种队列系统,例如Gearman(尽管有很多替代方案,例如Rabbit)。您可以让接口将作业放入 Gearman 队列,然后编写一个 Gearman Worker 以从队列中获取通知任务

你可以在PHP中做这种工作,但如果你这样做,那么你可能需要使用Gearman Manager PHP库,因为PHP不是长时间运行进程的最佳语言。Gearman Manager 为您启动和停止脚本,使在 PHP 中编写工作线程变得更加容易。

http://gearman.org/?id=gearman_php_extensionhttps://github.com/brianlmoon/GearmanManager

  1. 听起来是个聪明的主意。发送响应并继续在同一线程中处理通知,或者将作业排队以在服务器上执行通知。

  2. 有很多人在争论不同语言执行Web服务的优点。如果你对PHP最熟悉,并且性能要求不是很大,那可能是你最好的选择。