Php MagentoAPI:如何按ID加载客户会话

Php MagentoAPI:如何按ID加载客户会话,php,api,magento,Php,Api,Magento,我正在使用magento模块使用API登录客户 我可以登录客户并获取前端会话ID, 但是当我想用这个sessionId加载会话以检查客户是否已经登录时,我不能 这里是我使用的API函数: public function login($email,$password,$storecode){ $result = array(); $result['code'] = ''; $result['messages'] = ''; try{ $sessio

我正在使用magento模块使用API登录客户

我可以登录客户并获取前端会话ID, 但是当我想用这个sessionId加载会话以检查客户是否已经登录时,我不能

这里是我使用的API函数:

public function login($email,$password,$storecode){
    $result = array();
    $result['code'] = '';
    $result['messages'] =  '';
    try{
        $session = Mage::getSingleton('customers/session');
        $storemodel = Mage::getModel('core/store')->load($storecode);
        $store_id = $storemodel->getId();
        $websiteId = $storemodel->getWebsiteId();
        if($session->loginByApi($email, $password,$websiteId)){
            $result['code'] = 'SUCCESS';
            $result['sessionId'] = $session->getSessionId();
            $customer = $session->getCustomer();
            $result['customer']= array(
                'customerid' => $customer->getId(),
                'firstname' => $customer->getFirstname(),
                'lastname' => $customer->getLastname(),
            );
        }
    } catch (Mage_Core_Exception $e) {
        $result['code'] = 'ERRORS';
        $result['messages'] =  $e->getMessage();
    }
    return $result;
}

public function isloggedin($customerId,$customersessionId ,$storecode){
    if(!isset($storecode)){
        $storecode = 'default';
    }

    Mage::app($storecode, 'store');

    $core_session = Mage::getSingleton('core/session', array('name'=>'frontend'));
    if($customersessionId != null){
        $core_session->setSessionId($customersessionId);
        $core_session->start('frontend');
    }

    $session = Mage::getSingleton('customer/session', array('name'=>'frontend'));

    $customer = Mage::getModel('customer/customer')->load($customerId);
    $session->setCustomer($customer);


   if($session->isLoggedIn()){
        $session->setCustomerAsLoggedIn($customer);
        $result['sessionId'] = $session->getSessionId();
    }else{
        $result['logged'] = false;
    }
    return $result;

}

有人有想法吗?

不确定这是否有帮助,但以下代码:

Mage::app('2', 'store');
$s = Mage::getSingleton('customer/session', array('name'=>'frontend'));
$c = Mage::getModel('customer/customer')->load(1);
$s->setCustomer($c);

if($s->isLoggedIn()){
    echo $c->getName()." is logged in, session: ".$s->getSessionId().PHP_EOL;
} else {
    echo "not logged in".PHP_EOL;
}
似乎对我有用:

John Smith is logged in, session: d3rcgvd56md4u3cfctcvnt2ou6

如果您有会话ID,可以通过以下对核心会话单例的调用获取数据:

$sessId = 'sn1q4ndvr1kieumsmplhd39n83';
$sess = Mage::getSingleton('core/session', array( 'value' => $sessId ));
将检索任何用户(无论是否登录)的会话。通过检查客户对象,您可以确定会话是否属于客户:

$sessionCustomerId = $sess->getCustomer()->getId();

if ( $sessionCustomerId ) {
    // yay, they are a customer!
    echo $sessionCustomerId;
}
希望有帮助


编辑


您可以使用
Mage::getSingleton('core/session')->getEncryptedSessionId()

从核心会话获取会话id(鸡和蛋样式),当您说“不能”时,您的确切意思是什么
$session
为空,类似于这样的情况?$session作为访客加载,而不是客户登录,我使用$session->isLoggedIn()方法始终得到false检查客户是否从会话id登录的任何解决方案?Mike,在我的情况下,我不知道客户ID。我想通过加载会话来获得它,会话ID引用为
$sessiond
,这是从何而来的?前端cookie值是多少?因为(令人沮丧的)这对我不起作用<代码>$sess->getCustomer()变为NULL,我从inspector复制知道该值是正确的…@Jester代码片段假定您已经拥有会话id(注意第1行)。您可以从不同的位置获取id,最有可能的方法是遍历var/session文件夹并删除sess_uuu前缀。使用'value'参数可以获得很好的效果,但它仅在会话尚未初始化时才起作用。错误代码:if($sessionCustomerId=$sess->getCustomer()->getId())违反规则:条件中的赋值