如何从外部站点将带有 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 的单页结帐页面。

现在我需要将数据(用户的姓名、电子邮件和地址)从外部页面传递到此函数。我以为我可以用卷曲来做到这一点。但这是行不通的。我总是得到一个没有任何错误的空白页。我以前从未使用过 curl,而且我对 magento 也很陌生,所以我真的不知道问题出在哪里。有人可以帮助我或给我提示吗?或者是否有 another/better 从外部站点传递数据的方法?

我在我的外部站点上使用此 example 中的代码 post 使用 link mymagentopage.de/namespace/controllername/functionname 将用户数据发送到我的 mageno 函数。当用户提交表单时执行外部站点上的 curl 代码,但我得到的只是一个空白页面...

magento 控制器功能:

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

在您的 POST 代码中,字段没有被 url 编码,这可能会产生错误的请求或其他失败。

试试这个:

public function post_to_url($url, $data) {
     $fields = http_build_query($fields); // encode array to POST string

     $post = curl_init();

     curl_setopt($post, CURLOPT_URL, $url);
     curl_setopt($post, CURLOPT_POST, 1);
     curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
     curl_setopt($post, CURLOPT_USERAGENT, 'Mozilla/5.0');
     curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
     //curl_setopt($post, CURLOPT_FOLLOWLOCATION, true);

     $result = curl_exec($post);

     // for debugging
     var_dump(curl_getinfo($post), $result);

    //  if(false === $result) { // request failed
    //       die('Error: "' . curl_error($post) . '" - Code: ' . curl_errno($post));
    // }

     curl_close($post);

  }

好吧,我想的太复杂了...我只需要将我的外部站点的表单 "action" 直接指向执行我的 magento 操作的 magento 页面。然后我不得不用

捕捉参数
$this->getRequest()->getPost('email');

在 magento 动作中。就是这样。如此简单...