Using Curl in Checkout Stops Redirect

I use observer sales_order_save_after to capture order information and send part of it to another web service.

After receiving the order information, I use the following snippet in the observer to send the information to the web service. Information is sent normally and the service receives it. However, the browser still remains on the verification page, even if the order is completed, 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); 
+4
source share
4 answers

So the problem was that the headers sent with the Curl request meant that Magento was unable to send the headers to redirect to the success page. I could not fix it, but after a lot of searching, I was able to find a solution that I still like. Basically, I used the Zend queue function to queue requests and get cron to deal with them in bulk.

I like the idea of ​​requests running asynchronously so that the user does not expect the web service to receive a response before posting to the success page.

This is where I got the idea:

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

+2
source

CURLOPT_FOLLOWLOCATION and CURLINFO_EFFECTIVE_URL may be helpful.

Something like that:

 $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 
+1
source

Turn off all output, then try. It may be CURL that displays some 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 experienced that CURL does not work for me, where file_get_contents works fine. Although you have to make sure its allowed external in php.ini

+1
source

It is important to add the following to the mix: otherwise, redirecting to the success page does not work:

 curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

Here is one fully working block of code that is 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); 
0
source

All Articles