2

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();
Praful Rajput
3,8578 gold badges34 silver badges48 bronze badges
asked Nov 30, 2015 at 12:20

2 Answers 2

1

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".

answered Nov 30, 2015 at 12:26
5
  • 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? Commented 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) Commented 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 help Commented 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_schedule and job data in a custom table. Commented 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)); Commented Dec 4, 2015 at 10:15
0

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";
}
answered Nov 30, 2015 at 13:03
3
  • I have tried both suggestions and still no success. Thanks for quick response. Commented Dec 1, 2015 at 5:33
  • are you runing curl in admin side or root folder ? Commented Dec 1, 2015 at 5:51
  • Admin side, I think script is not running because action requires logged in user. Commented Dec 1, 2015 at 9:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.