How to run a background function in codeigniter? - php

I am using codeigniter with REST API for web service.
I want to send multiple apple push notification in an certain action. The problem is its taking time to response because its sending all the notification and response the web service call.
I need web service call should response immediately and then it will send notification in background.
Any ideas ???

Assign a common function in common.js ,then trigger a function for every 2 min and run it in background as ajax function
window.setInterval(function(){
/// call your function here
}, 5000); // for every 5 sec

Related

how to stop a function in laravel controller

I'm new to LARAVEL. I want to send an AJAX request to a function of controller that this function will run very big loop (takes about 2 minutes). I want to have a button that when click on it, it send a request with AJAX to the controller that stops the function that is running big loop. any idea?
After second click (to stop function) you can send to database information about it. In one of 10 loops function can check database and if function get message "stop" then will stopped.

AngularJS delayed HTTP POST to PHP

I'm using the http service in AngularJS to make requests to my PHP API. It's a Phonegap iOS app, using AngularJS as the main framework.
In one call, I'm using the Facebook Javascript API to get the friends list of a user, encode that and send it to my API, match the emails to a list of our app users, then send the data back to the app.
I do exactly the same thing with the contacts from the phone, which I access using Phonegap.
Using console.log() I've confirmed that loading the contacts and facebook friends data from the device take less than a second.
The problem is mapping the Facebook friends list via the API takes about 20 seconds, but contacts only takes about 2 seconds.
I've manually forced the facebook list to be empty, var friends = [], and the process is nearly instant, but if I send 1 or 300 facebook contacts it gets really slow.
I've check the code on the live server by processing it with hard coded data and it takes about 0.01 seconds (so no HTTP POST, just the php logic).
I've also placed console.log() commands before the http.post() and during the success function:
console.log('http.post started');
$http.post(
$rootScope.api_url + '/friends/map/',
{values: hashes.values},
).success(function(data,status,headers,config){
console.log('http.post finished');
// process data
}).error(function(data,status,headers,config){
// log error
});
And in addition to that used PHP error_log to tell me when the start of the PHP processing occurs, and just before the result is returned:
<?php
// includes
require_once("../../../configs/config.".get_current_user().".php");
function process_request() {
error_log('started processing');
// decode POST data, work out API method etc
$return_data = $caseObject->process_data( $method, $arr_rest_data, $arr_req_data, $extra_data );
error_log('data processed');
return $return_data;
}
echo GibberishAES::enc( json_encode( process_request() ), IOS_API_PASSPHRASE );
?>
So that gives me a log before the Javascript sends the data, before and after the PHP processes it and after the Javascript receives a response.
Watching the logs get output, the 20s delay is between the following:
console.log('http.post started');
error_log('started processing');
So the delay seems to be in the upload. No data has been AES decoded by that point, and I've checked and the facebook data is an encoded string about 17k characters long, and the contacts data is 22k characters long. So it's not the size of the post, at least I don't think.
To summarise, I've got an http.post request in AngualrJS that is taking about 20s, very similar to another request that takes around 2s, the PHP code itself executes in under 1 second, and the delay seems to be between the start of the javascript http.post call and the beginning of the php processing code.
Any one know how I can work out what is causing the delay? Where should I be looking to narrow this down?
For anyone landing here with a similar issue:
The delay seems to be related with $http's request pre-processing and internal handling.
Using "plain old" XHR (eliminating $http) could speed things up considerably (at the cost of lost functionality and Angular-integration, of course).

PHP SOAP calls not waiting for return

Is there a way of creating a SOAP call in PHP that is not waiting for the called function to return?
I am calling a Magento Api function from my system to create a new product in the catalog. This can take quite a long time and I don't want my end user to wait until it is done.
I would do just a normal call, but don't block the user doing other stuff during the call. This way you can have your cake (the call) and eat it (get the results).
Most common method would be to do an AJAX call: you do the request via script to the PHP file, but the interface will be responsive / usable. If the user doesn't browse from the page you did the request at, you can even serve a response.

Terminate connection to jQuery AJAX request, keep processing php on server side?

I have a signup form that calls a PHP script which can interact with our CRM's API, like so:
CRM API <--> PHP script <--> Signup form
The signup form passes some information to the PHP script in one
AJAX call
The PHP script run a dozen API calls to the CRM to create
an account and attach various data
The CRM returns the new account id it just created to the PHP script
The PHP script passes the account id back to the signup form, at which point the AJAX call is complete and the signup form can continue.
The problem is #2, those dozen calls take about 20 seconds to complete, but the data the signup form needs is generated after the first API call so it could in theory return that data much sooner and do the rest of the stuff server side without holding that AJAX call open the whole time.
I tried flush() and ob_flush() which does output account id to the client before processing is complete, but the jQuery AJAX connection remains open so I'm still stuck waiting for the connection to be closed on the signup form side before anything happens.
So what's the easiest route for returning that account id to the form as fast as possible?
Maybe break out using curl and exec?
if(signing up){
stuff
exec(curl myself, notsignup)
}
else {
bunch of api calls
}
You should probably think about creating a seperate process for the rest of the steps that are needed. One way is that you could after the #1 first api calls has been completed. It responds back to the user, and doesn't try to complete the rest of the 20 calls on the user side.
Then create a queue that will finish the rest. You could always create a table in mysql to store the queue.
Next just create a cronjob that will run in the background, knocking the queue out.
Note: You will not want this cronjob to just start and never stop. Maybe have it run every 5 minutes, but before it starts to run, check to see if another cron is still in progress. If it is then it will check in another 5 minutes to see if it is ok to run.
Hope this helps!
If you only need the information from the first API call to return the form, then I would probably try a different workflow:
Form calls PHP Script
PHP Calls first API Call
PHP Returns to Form
Form processes response
Form calls second PHP Script to complete the process
PHP finishes API Calls (the form can abandon at this point since it sounds like you don't care what happens from here on out).
The workflow requires a little more work and co-ordination for the developer, but presents the most responsive interface to the user.

dont wait for script to finish

I have a php script that sends SMS, the problem is that it takes some time before every SMS is sent. In my site the page will wait until this script has finished running. How can I give user a message that SMS will be sent and resume the site's normal operation.
The only issue here is that the browser thinks it is are waiting for more output from the script when there will be none. You could offload to a seperate process, or use an asynchronous web call, or you could simply.....
<?php
register_shutdown_function('when_alls_done');
.... // render page
exit;
function when_alls_done()
{
if ($_REQUEST['send_to_phone']) {
send_sms($_REQUEST['send_to_phone'], $_REQUEST['message']);
}
}
The webserver should flush the request at the 'exit' and let the browser know that the response is complete (an explicit flush in the PHP code prior to that will either not flush the webserver buffer or it will result in the output being chunk encoded with another chunk to come).
C.
Put the messages in a queue in your database. Then have a script running as a cron job in the background to take care of the queue.
It's very easy when you know how:
<?php
echo 'A sms will be sent!';
fastcgi_finish_request();
// Put all your time consuming code here!
?>
Instead or sending the SMS right there and then you can store it somewhere (for example the database). You can then build an extra script (in PHP or whatever else you want) that polls the database looking for SMS to send and dispatches them. You can have this run every x seconds or minutes via cron or scheduled task according to your OS. This way you take a time consuming task out of the page whose job is to communicate with user in a timely manner.
If you are on LAMP
Write a cron job that will query a queue (in database) for pending SMS and send them.
In your script add the SMS to the queue.
Show the user status of his SMS on another page.
With Ajax you can query in background for the status of recently sent SMS. As soon as you see sent notify user.
to do this you can use ajax.
on some button press just send an ajax request to the php file moulded to send sms. and put a notification in the screen like 'sending sms!!!' and on the response of the ajax action change the 'sending sms' to 'success!!!'....
for using ajax you can use jquery..
if you dont know jquery comment me for the video tutorials... i have some beginners video tutorial for jquery....
Have a nice day!!!!!

Categories