I am generating an excel report which takes around 10-25mins based on user selection. Page is served to admins only.
When user presses export button after selecting filters, I am trying to call action method using CURL and timeout curl call to make this process asynchronous. At the end of process an email is generated to let user know that file is exported.No response from script needed.
However, I am unable to call action method using below code. CURL is working for other sites (tested with google). Error returned is 403, although I can directly access the URL from browser and it works.
This code is present inside a controller action which is triggered by export button, same controller contains "exportorders".
$url = $this->getUrl('*/*/exportorders’);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); // Return from curl_exec rather than echoing
curl_setopt($c, CURLOPT_FRESH_CONNECT, true); // Always ensure the connection is fresh
// Timeout super fast once connected, so it goes into async.
curl_setopt( $c, CURLOPT_TIMEOUT, 1 );
curl_exec( $c );
$curlInfo = curl_getinfo($c);
curl_close($c);
if($curlInfo['http_code'] == 200){
$success = ‘Process successfully running’;
}
$this->_redirectReferer();
2 Answers 2
You need to be authenticated to access action in BO. So your CURL call must send cookie information.
But the best way for doing big process is a cron. When you click on the button you add a line in cron_schedule table, and when cron.sh run, your export start "asynchronously".
-
C - I can see the token at the end. Complete URL looks like this: (example.com/index.php/test/adminhtml_export/exportorders/key/…). Do I need to send something else? If I setup a CRON job for this, will the job start executing immediately and for every click of export button?F-Z– F-Z2015年11月30日 12:32:24 +00:00Commented Nov 30, 2015 at 12:32
-
You need to send additional header (cookie, etc). Look in Chrome dev tools or Firebug the request headers for a normal navigation in BO. If you use cron, the script will be launch on next cron.sh execution (Magento recommend every 5minutes)bchatard– bchatard2015年11月30日 14:35:16 +00:00Commented Nov 30, 2015 at 14:35
-
I have tried by adding additional headers (cookies) etc. but still got same issue. I have now added a CRON job and it works fine. One issue though is that how to pass user specified criteria to CRON job. I am using AOE Scheduler to schedule job. Thanks for your helpF-Z– F-Z2015年12月01日 16:10:52 +00:00Commented Dec 1, 2015 at 16:10
-
With an additional table with data for the cron job. When you click on button you add job in
cron_scheduleand job data in a custom table.bchatard– bchatard2015年12月01日 23:10:06 +00:00Commented Dec 1, 2015 at 23:10 -
Thanks for your help. I am now using AOE Scheduler method: $schedule->schedule(); to schedule job and also adding parameters like below as this is now supported by AOE: $schedule->setParameters(serialize($array));F-Z– F-Z2015年12月04日 10:15:38 +00:00Commented Dec 4, 2015 at 10:15
Try this:
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'module_name/controller_name/action_name';
//OR
$url = Mage::getUrl('module_name/controller_name/action_name');
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); // Return from curl_exec rather than echoing
curl_setopt($c, CURLOPT_FRESH_CONNECT, true); // Always ensure the connection is fresh
// Timeout super fast once connected, so it goes into async.
curl_setopt( $c, CURLOPT_TIMEOUT, 1 );
curl_exec( $c );
$curlInfo = curl_getinfo($c);
curl_close($c);
if($curlInfo['http_code'] == 200){
$success = "Process successfully running";
}
-
I have tried both suggestions and still no success. Thanks for quick response.F-Z– F-Z2015年12月01日 05:33:50 +00:00Commented Dec 1, 2015 at 5:33
-
are you runing curl in admin side or root folder ?Abdul– Abdul2015年12月01日 05:51:28 +00:00Commented Dec 1, 2015 at 5:51
-
Admin side, I think script is not running because action requires logged in user.F-Z– F-Z2015年12月01日 09:36:00 +00:00Commented Dec 1, 2015 at 9:36
Explore related questions
See similar questions with these tags.