在Checkout中使用Curl停止重定向

时间:2013-04-23 18:43:42

标签: php magento curl

我正在使用观察者sales_order_save_after来捕获订单信息并将其中一些信息发送到另一个Web服务。

获取订单信息后,我在观察者中使用以下curl片段将信息发送到Web服务。信息发送正常,服务收到它。但是,浏览器仍保留在结帐页面上,即使订单已完成,用户也不会被重定向到成功页面。

$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 个答案:

答案 0 :(得分:2)

所以问题是使用Curl请求发送的标头意味着Magento无法将重定向的标头发送到成功页面。我无法解决这个问题,但经过大量的谷歌搜索,我能够找到一个我更喜欢的解决方法。基本上我使用Zend Queue功能对请求进行排队,并获得一个cron来批量处理它们。

我喜欢请求异步工作的想法,这样用户就不会在转发到成功页面之前等待webservice获得回复。

这就是我的想法:

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

答案 1 :(得分:1)

CURLOPT_FOLLOWLOCATIONCURLINFO_EFFECTIVE_URL可能会有用。

这样的事情:

$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

答案 2 :(得分:1)

禁用所有输出,然后尝试。可能是CURL正在输出一些文本,甚至是空格,这会中断你的重定向。

<?php

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

也可以尝试使用file_get_contents而不是CURL。我经历过CURL不适合我,其中file_get_contents工作正常。虽然你必须确保它在php.ini

中允许外部

答案 3 :(得分:0)

必须将以下内容添加到组合中;否则,重定向到成功页面不起作用:

curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

这是一个完整工作的代码块,它是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);