用于Mobile API的Magento Custom模块花费了太多时间来发送响应

时间:2016-09-05 11:41:10

标签: api magento curl loading magento-1.9

我使用以下代码为Mobile API创建了自定义模块:

AppController.php

Traceback (most recent call last):
from docx import Document
File "C:\Python27\lib\site-packages\docx-0.2.4-py2.7.egg\docx.py", line 17, in <module>
from lxml import etree
ImportError: DLL load failed: %1 is not a valid Win32 application.

的Config.xml

<?php

class <NameSpace>_<Module>_AppController extends Mage_Core_Controller_Front_Action {
    public function loginAction() {
        $postData = json_decode($_POST['login_details'], true);
        if (($postData['email'] != '') && ($postData['password'] != '')) {
            $email = $postData['email'];
            $password = $postData['password'];
            Mage::getSingleton("core/session", array("name" => "frontend"));
            $customer = Mage::getModel("customer/customer");
            $customer->website_id = Mage::app()->getWebsite()->getId();
            $customer->setStore(Mage::app()->getStore());
            try {
                $customer->loadByEmail($email);
                $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
                $session->login($email, $password);
                $response['success'] = '1';
                $response['message'] = 'Login Successfull.';
                $response['status_code'] = '200';
                $response['data'] = $customer->getData();
            } catch (Exception $e) {
                $response['success'] = '0';
                $response['message'] = $e->getMessage();
                $response['status_code'] = $e->getCode();
            }
        } else {
            $response['success'] = '0';
            $response['message'] = 'Login Fail : Please Enter Email Id and Password.';
            $response['status_code'] = '400';
        }
        echo json_encode($response);
    }
}

当我通过Curl调用登录API时,花费太多时间来显示响应:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <[NameSpace]_[Module]>
            <version>0.1.0</version>
        </[NameSpace]_[Module]>
    </modules>
    <frontend>
        <routers>
            <[module]>
                <use>standard</use>
                <args>
                    <module>[NameSpace]_[Module]</module>
                    <frontName>mobileapi</frontName>
                </args>
            </[module]>
        </routers>  
    </frontend>
</config>

CURL执行成功,但每次执行都花费了太多时间。 任何人都可以指导我减少自定义模块中API的执行时间吗?

1 个答案:

答案 0 :(得分:0)

我尝试了这个,它对我来说更快的API响应:

public function loginAction() {
            $postData = json_decode($_POST['login_details'], true);
            $flag = 0;
            $cacheId = 'login_' . $postData['email'].$postData['password'];

            if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
                $data = unserialize($data);
                if($data['success']==1){
                    $flag = 1;
                    $response=$data['response'];
                }
            }
            if ($flag == 0) {

                if (($postData['email'] != '') && ($postData['password'] != '')) {
                    $email = $postData['email'];
                    $password = $postData['password'];
                    Mage::getSingleton("core/session", array("name" => "frontend"));
                    $customer = Mage::getModel("customer/customer");
                    $customer->website_id = Mage::app()->getWebsite()->getId();
                    $customer->setStore(Mage::app()->getStore());
                    try {
                        $customer->loadByEmail($email);
                        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
                        $session->login($email, $password);
                        $response['success'] = '1';
                        $response['message'] = 'Login Successfull.';
                        $response['status_code'] = '200';
                        $response['data'] = $customer->getData();
                        $data['success'] = 1;
                        $data['response']=$response;
                        Mage::app()->getCache()->save(serialize($data), $cacheId);
                    } catch (Exception $e) {
                        $response['success'] = '0';
                        $response['message'] = $e->getMessage();
                        $response['status_code'] = $e->getCode();
                    }
                } else {
                    $response['success'] = '0';
                    $response['message'] = 'Login Fail : Please Enter Email Id and Password.';
                    $response['status_code'] = '400';
                }
            }
            echo json_encode($response);
            exit;
}