duanca3415 2016-02-21 00:21
浏览 44
已采纳

如何将curl的POST数据从外部站点发送到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.

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?

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...

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
    }
}
  • 写回答

2条回答 默认 最新

  • dongpu2727 2016-02-22 09:11
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line