我是Magento Web-Service的新手,必须扩展它。Webservice shell能够登录客户,返回会话cookie,这样我就可以重定向到一个再次设置cookie的文件,重定向我,我就可以看到我的购物车并继续在Magento Store上结帐。
问题:Magento创建一个cookie(其中包含会话id或其他内容,我尝试过设置这个cookie手册,当我登录时),而不是在客户登录时设置会话。我现在已经尝试了几个小时来获得magento在我的magento web服务中设置的这个cookie。当我调用
时,cookie似乎没有设置$session = Mage::getSingleton('customer/session');
return $session->getCookie()->get('frontend');
以下是我的完整代码:Magento Webservice Api:
<?php
class Customapi_Model_Core_Api
{
public function __construct()
{
}
public function checkout($user, $cart)
{
$ret['cookie'] = $this->login($user);
//$coreCookie = Mage::getSingleton('core/cookie');
//$ret['cookie'] = $_COOKIE['frontend'];
return $ret;
}
function login($user)
{
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session');
try
{
$session->loginById($user['id']);
}
catch (Exception $e)
{
}
return $session->getCookie()->get('frontend');
}
}
?>
这是我在Php中的Api调用:
<?php
$teambook_path = 'http://localhost/magento/';
$soap = new SoapClient('http://localhost/magento/api/?wsdl');
$soap->startSession();
$sessionId = $soap->login('ApiUser', 'ApiKey');
$userSession = $soap->call(
$sessionId,
'customapi.checkout',
array(
array(
'id' => 1,
'username' => 'Ruf_Michael@gmx.de',
'password' => '***'
),
array(
)
)
);
echo '<pre>';
var_dump($userSession)."'n";
if(isset($userSession['sid']))
echo '<a href="'.$teambook_path.'session.php?sid='.$userSession['sid'].'" target="_blank">'.$userSession['sid'].'</a>'."'n";
echo '</pre>';
$soap->endSession($sessionId);
?>
谢谢你的帮助!个系统
对不起,我正在写一个答案,但评论框拒绝我写超过…字母。
我已经尝试了你发布的两个代码,所有我得到的是一个空数组或Bool false。我写了一个静态函数:
private static $cookie = array();
public static function cookie($key, $value)
{
if($key == 'frontend') {
self::$cookie['key'] = $key;
self::$cookie['value'] = $value;
}
}
在Mage_Core_Model_Session_Abstract_Varien::start中被调用我得到了前端cookie值:
Customapi_Model_Core_Api::cookie($sessionName, $this->getSessionId());
第125行
但这并没有解决我的基本问题:在Api调用中创建的会话不能被恢复,尽管它被设置为正确的值。
谢谢你的帮助!
您可以使用以下命令获得所有cookie的数组:
Mage::getModel('core/cookie')->get();
前端cookie可以像这样检索:
Mage::getModel('core/cookie')->get('frontend');
从你的注释代码中我可以看出你已经知道了。
据我所知,当你登录一个用户时,Magento不只是创建一个新的会话id,它使用活动连接的会话id(这是由PHP自己生成的)。您正在登录用户并将其关联到您的API客户端刚刚使用Magento创建的会话。因此,您所注释的代码对于您想要实现的目标似乎是正确的。
现在你应该只需要得到返回的会话id,并在你的新请求中使用它作为"前端"cookie。
第二次编辑
Magento在一个PHP会话中有不同的会话,用于不同的范围。例如,有核心范围、客户范围等。然而,客户范围也是特定于给定网站的。因此,您可以拥有一个customer_website_one和一个customer_website_two作用域。
如果你想登录你的用户,你必须告诉Magento在哪个网站。以以下代码为例
// This code goes inside your Magento API class method
// These two lines get your website code for the website with id 1
// You can obviously simply hardcode the $code variable if you prefer
// It must obviously be the website code to which your users will be redirected in the end
$webSites = Mage::app()->getWebsites();
$code = $webSites[1]->getCode();
$session = Mage::getSingleton("customer/session"); // This initiates the PHP session
// The following line is the trick here. You simulate that you
// entered Magento through a website (instead of the API) so if you log in your user
// his info will be stored in the customer_your_website scope
$session->init('customer_' . $code);
$session->loginById(4); // Just logging in some example user
return session_id(); // this holds your session id
如果我理解正确的话,您现在想让用户在您的服务器中打开一个PHP脚本,将Magento Cookie设置为您刚刚在API方法中返回的内容。我编写了以下示例,您可以这样访问:example.com/set_cookie.php?session=THE_RETURNED_SESSION_ID
<?php
// Make sure you match the cookie path magento is setting
setcookie('frontend', $_GET['session'], 0, '/your/magento/cookie/path');
header('Location: http://example.com');
?>
应该可以了。您的用户现在已经登录(至少我让它在我的测试环境中工作)。您应该记住的一件事是,Magento有一个会话验证机制,如果启用,该机制将失败。这是因为您的会话存储了有关您正在使用的浏览器,连接的IP等信息。这些数据在通过API方法调用和稍后的浏览器之间将不匹配。下面是命令print_r($session->getData())
在API方法
[_session_validator_data] => Array
(
[remote_addr] => 172.20.1.1
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => PHP-SOAP/5.3.5
)
确保您在设置>配置>常规> Web>会话验证设置