Matthew Dolman
Matthew Dolman

Reputation: 1800

Using Curl in Checkout Stops Redirect

I am using the observer sales_order_save_after to capture order information and send some of it to another web service.

After getting the order information I use the following curl snippet in the observer to send the information on to the web service. The information sends ok and the service receives it. However the browser is still left on the checkout page, even though the order is complete, the user is not redirected to the success page.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://myapp.com/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"field": 'data'}');
curl_setopt($ch, CURLOPT_USERPWD, 'blahblah:blahblah');

curl_exec($ch);
curl_close($ch);

Upvotes: 4

Views: 808

Answers (4)

Insan3
Insan3

Reputation: 11

It is essential to add the following into the mix; otherwise, the redirect to the success page does not work:

curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

Here is one fully working block of code that is a part of Observer.php:

    $APIURL = 'https://example.com/submit.php';
        $UID = 'username';
        $APIKEY = 'password';
$fields = array(
    'uid'                   => $UID,
    'apikey'                => $APIKEY,
    'name'                  => $customerName,
);

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $APIURL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post   
curl_exec($ch);
//close connection
curl_close($ch);

Upvotes: 1

beiller
beiller

Reputation: 3135

Disable all output, then try. It could be CURL is outputting a slight bit of text, even spaces, and this will interrupt your redirect.

<?php

error_reporting(E_ERROR); //this should disable all warnings.

Also try file_get_contents instead of CURL. I have experienced CURL not working for me where file_get_contents worked fine. Although you have to make sure its allowed external in php.ini

Upvotes: 1

Matthew Dolman
Matthew Dolman

Reputation: 1800

So the problem was that the headers being sent with the Curl request meant that Magento was unable to send the headers for the redirect to the success page. I was unable to fix this but after much googling I was able to find a workaround which I like better anyway. Basically I used the Zend Queue feature to queue the requests and get a cron to deal with them in bulk.

I like the idea of the requests working asynchronously so that the user is not waiting for the webservice to get a reply before being forwarded to the success page.

This was where I got the idea:

http://www.kingletas.com/2012/08/zend-queue-with-magento.html

Upvotes: 2

Andrey Volk
Andrey Volk

Reputation: 3549

CURLOPT_FOLLOWLOCATION and CURLINFO_EFFECTIVE_URL might be useful.

Something like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://myapp.com/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // for redirects
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"field": 'data'}');
curl_setopt($ch, CURLOPT_USERPWD, 'blahblah:blahblah');

curl_exec($ch);

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // get last effective URL

curl_close($ch);

header("Location: ".$last_url); // force browser to redirect

Upvotes: 1

Related Questions