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();
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".

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.