【问题标题】:PHP -> SOAP -> Magento Webservice: get Cookies set by MagentoPHP -> SOAP -> Magento Webservice:获取 Magento 设置的 Cookie
【发布时间】:2011-04-05 14:37:31
【问题描述】:

我是 Magento Web 服务的新手,必须扩展它。 Webservice shell 能够登录客户,将会话 cookie 还给我,以便我可以重定向到再次设置 cookie 的文件,重定向我,然后我可以看到我的购物车并继续在 Magento 商店结帐。

问题: Magento 创建一个 cookie(包含会话 id 或其他内容,我尝试设置此 cookie 手册并且他们即时登录)而不是在客户登录时设置会话。 我已经尝试了几个小时来获取这个由 magento 在我的 magento 网络服务中设置的 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);
?>

感谢您的每一个帮助! MRu


对不起,我正在写一个答案,但评论框拒绝我写超过...信。

我已经尝试了您发布的两个代码,但我得到的只是一个空数组或 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 调用中创建的 Session 无法恢复,尽管它已设置为正确的值。

感谢您的帮助!

【问题讨论】:

  • 你好@MRu,我是magento的新手,有同样的任务,请你指导我如何逐步为magento中的客户登录创建Web服务?

标签: php cookies soap magento


【解决方案1】:

您可以使用以下命令获取所有 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 方法和浏览器的调用之间将不匹配。这是在 API 方法中设置会话后命令 print_r($session-&gt;getData()) 的示例输出

[_session_validator_data] => Array
(
    [remote_addr] => 172.20.1.1
    [http_via] => 
    [http_x_forwarded_for] => 
    [http_user_agent] => PHP-SOAP/5.3.5
)

确保在 Magento Admin 中的 Settings > Configuration > General > Web > Session Validation Settings 中打开验证

【讨论】:

  • 我发布了一个答案,因为这里没有足够的字符允许。对不起:)
  • 再次感谢您的辛勤付出。但这并没有解决我的问题。我在尝试获取 cookie 之前尝试了此操作,但它不起作用,因为会话也无法“恢复”。我再次尝试描述我的问题:当我重定向到 magento 时,soap api 调用创建的会话没有“恢复”(设置 cookie 的脚本工作正常)。你知道在 Api 调用或其他任何事情之后是否有任何类型的清除会话吗?
  • 我用我的新发现扩展了我的答案。如您所见,这实际上非常棘手。我希望我现在已经完全理解了您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 2020-09-06
  • 2018-08-30
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
相关资源
最近更新 更多