我正在尝试以magento编程方式登录客户。我已经使用curl并在magento中开发了我的soap api。
要求:我有两个网站一个在laravel和其他在magento和所有的电子邮件和密码都设置在两个网站。当用户登录到laravel网站,然后客户必须登录到magento网站,反之亦然。单点登录概念。
使用Soap Api,将参数email和密码传递给我的Soap客户端。下面是代码:
class Axovel_Customlogin_Model_Custom_Api extends Mage_Api_Model_Resource_Abstract
{
public function login($email,$password) {
$login_customer_result = Mage::getModel('customer/customer')->setWebsiteId(1)->authenticate($email, $password);
if ($login_customer_result == 1) {
Mage::getSingleton("core/session", array("name" => "frontend"));
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
try {
$customer->loadByEmail($email);
//$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
$session = Mage::getSingleton('customer/session');
$session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
return $session->getSessionId();
}catch(Exception $e){
echo $e;
}
}
}
}
通过soap调用调用此方法,它将返回会话id并验证客户,但不会在前端登录客户。
我尝试了curl调用客户模块的loginPost动作并传递表单键,下面是curl代码:
$string = "login[username]=xyz@mymail.com&login[password]=123456&form_key=B3pr9GSMp75kwW20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/performer/index.php/customer/account/loginPost');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.session_id());
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_HEADER, true);
$output = (string) curl_exec($ch);
这也将工作,没有任何错误,但客户没有登录。
我已经创建了一个html表单在magento根使用操作
http://127.0.0.1/performer/index.php/customer/account/loginPost
和文本字段是电子邮件,密码和表单密钥,在提交表单时,它将成功登录到magento。
Try $session->loginById and $session->setCustomerAsLoggedIn:
摘自我过去的一个项目:
$session = Mage::getSingleton( 'customer/session' );
try
{
$session->loginById( $customer->getId());
$session->setCustomerAsLoggedIn( $session->getCustomer() );
}
catch( Exception $e )
{
return false;
}
Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/index'));