简体   繁体   中英

Communication between iOS and PHP (Web) Service Issues

Okay so I wanna create an iPhone app that communicates with a WEB Service. BUT there are 2 points that aren't clear to me.

  1. When A user presses a button in the app, it informs the web service of the action and web service sends push notifications to the corresponding users. Since the no. of users to whom the notification has to be sent may be large, I was thinking of sending a response (OK/Error) to the iPhone app and then continue with sending the notifications. In other words, the PHP script should accept the request, respond to it and after responding, continue with sending notifications so that the iPhone app does not have to wait for the service.

  2. The service will require to send push notifications to a lot of users. And I'm writing the web service in PHP. So is there a more effective alternate to PHP? Or is it the best way to implement the service?

Thanks.

Well for #1, you could do something like this...

Have a method in your phone app like:

- (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
        }
    }
}

In your PHP, you can do something like:

<?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";
}
?>

Updated

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

As for sending push notifications to other phones, I don't know how to do that part. But I would assume PHP would have no issue with efficiency.

I'm assuming the key issue is that you want to respond to the user immediately and then do other work such as sending out notifications asynchronously.

I would suggest using some kind of queue system such as Gearman (though there are plenty of alternatives such as Rabbit). You can get your interface to put a job into the Gearman Queue and then write a Gearman Worker to pick up notification tasks from the queue

You can do this kind of work in PHP, but if you do then you might want to use the Gearman Manager PHP library as PHP is not the best language for long running processes. Gearman Manager starts and stops scripts for you and makes it easier to write workers in PHP.

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

  1. Sounds like a smart idea. Either send the response and continue processing notifications in the same thread, or queue a job to perform the notifications on the server.

  2. There are a lot of people debating the virtue of different languages for performing web services. If you are most comfortable with PHP and the performance requirements aren't huge, thats probably your best option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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