由于背景过程,我必须使用CURL调用动作。以下是我的代码。
$url = "http://www.domain.com/index.php/checkout/cart/deleteall";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$data = curl_exec($ch);
$errorMsg = curl_error($ch);
curl_close($ch);
echo $data;
echo '<br/>';
echo $errorMsg;
die();
购物车控制器操作
public function deleteallAction()
{
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item){
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
}
$this->_redirectReferer(Mage::getUrl('*/*'));
}
我在购物车控制器中创建了deleteallAction。但CURL不起作用。它也没有给我任何错误。我在ajax中称呼它。
如果我错了,请指导我。
谢谢!
答案 0 :(得分:1)
Jimmeh。您需要更改操作网址http://www.domain.com/index.php/checkout/cart/updatePost
并更改操作方法帖子
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.domain.com/index.php/checkout/cart/updatePost");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"update_cart_action=empty_cart&cart=$quoteId");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
当您在cartcontroller
空白购物车中添加新操作时,请使用代码$this->_emptyShoppingCart();
清空购物车。
在cartcaontollers上查看更多信息
public function updatePostAction()
{
$updateAction = (string)$this->getRequest()->getParam('update_cart_action');
switch ($updateAction) {
case 'empty_cart':
$this->_emptyShoppingCart();
break;
case 'update_qty':
$this->_updateShoppingCart();
break;
default:
$this->_updateShoppingCart();
}
$this->_goBack();
}
curl的工作原理http://davidwalsh.name/curl-post