Search code examples
phpmagentocurlhttprequestmagento-1.9

How can I send POST data with curl to a magento controller function from an external site?


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 = '[email protected]'
        $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
    }
}

Solution

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