如何从外部网站发送带curl的POST数据到magento控制器功能? [英] How can I send POST data with curl to a magento controller function from an external site?
问题描述
我有一个自定义控制器在我的magento网站的功能,它以编程方式创建一个客户,添加一个产品到他的购物车,并将他重定向到结帐页面。一切工作正常。可以通过URL mymagentopage.de/namespace/controllername/functionname 访问控制器函数,并将用户重定向到magento的一页结帐页面。
I have a custom controller with a function in my magento site which programmatically creates a customer, add a product to his cart and redirects him to checkout page. Everything works fine. The controller function is accessible through the URL mymagentopage.de/namespace/controllername/functionname and will redirect the user to magento's one-page-checkout page.
现在,我需要将来自外部页面的数据(姓名,电子邮件和地址)从外部页面传递到此功能。我想我可以用卷曲做到这一点。但它不会工作。我总是得到一个空白页没有任何错误。我以前从来没有使用curl,我也很新magento所以我真的不知道什么问题可能是。有人可以帮助我或给我一个提示吗?或者是否有其他/更好的方式从外部网站传递数据?
Now I need to pass data (name, email and adress from the user) from an external page to this function. I thought I could do this with curl. But it won't work. I always get a blank page without any errors. I never worked with curl before and I am also quite new to magento so I don't really know what the problem might be. Can somebody help me or give me a hint? Or is there another/better way to pass data from an external site?
我使用此示例在我的外部网站上使用链接mymagentopage.de/namespace/controllername/functionname将用户数据发布到我的mageno函数。外部网站上的curl代码在用户提交表单时执行,但只有一个空白页...
I use the code in this example on my external site to post the user data to my mageno function using the link mymagentopage.de/namespace/controllername/functionname. The curl code on the external site is executed when the user submits a form, but I got only a blank page...
magento控制器函数:
The magento controller function:
class MyModule_Test_CustomController extends Mage_Core_Controller_Front_Action {
// this is the action that loads the cart and redirects to the cart page
public function cartAction() {
// Get customer session
$session = Mage::getSingleton('customer/session');
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$email = 'test@test.de'
$price = '20';
function IscustomerEmailExists($email, $websiteId = null){
$customer = Mage::getModel('customer/customer');
if ($websiteId) {
$customer->setWebsiteId($websiteId);
}
$customer->loadByEmail($email);
if ($customer->getId()) {
return $customer->getId();
}
return false;
}
$cust_exist = IscustomerEmailExists($email,$websiteId);
if($cust_exist){
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
$session_customer = Mage::getSingleton('customer/session')->loginById($customer->getId());
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$customAddress = Mage::getModel('customer/address')->load($customerAddressId);
$customAddress->getData();
}
}
else{
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
if(!$customer->getId()) {
$customer->setStore($store);
$customer->setEmail($email);
$customer->setFirstname('John');
$customer->setLastname('Doe');
$customer->setPassword('somepassword');
}
try {
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$session_customer = Mage::getSingleton('customer/session')->loginById($customer->getId());
}
catch (Exception $ex) {
}
//Build billing address for customer, for checkout
$_custom_address = array (
'firstname' => 'John',
'lastname' => 'Doe',
'street' => 'Sample address part1',
'city' => 'Munich',
'region_id' => 'BAY',
'region' => 'BAY',
'postcode' => '81234',
'country_id' => 'DE',
'telephone' => '0123455677',
);
$customAddress = Mage::getModel('customer/address');
$customAddress->setData($_custom_address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try {
$customAddress->save();
}
catch (Exception $ex) {
}
}
Mage::getSingleton('checkout/session')->getQuote()->setBillingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$product = Mage::getModel('catalog/product');
$product->load('2');
$product->setPrice($price);
$product->save();
$cart->addProduct($product, array('qty' => 1));
$session->setCartWasUpdated(true);
$cart->save();
Mage::app()->getResponse()->setRedirect(Mage::helper('checkout/url')->getCheckoutUrl()); //redirect to Checkout
}
}
推荐答案
好吧,我想太复杂了...我只需要将我的外部网站的形式行动指向magento页面直接执行我的magento行动。然后我不得不抓住参数
Ok, I was thinking way too complicated... I just had to point the form "action" of my external site to the magento page directly which executes my magento action. Then I had to catch the parameters with
$this->getRequest()->getPost('email');
就是这样。这么简单...
in the magento action. And that's it. So simple...
这篇关于如何从外部网站发送带curl的POST数据到magento控制器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!