Using CURL to Call Controller Action in Magento

actioncontrollershttp-requestscript

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

Best Answer

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