PHP - > SOAP - > Magento Webservice:获取由Magento设置的Cookie

mic*_*ruf 4 php cookies soap magento

我是Magento Web-Service的新手,必须扩展它.Webservice shell能够登录客户,给我回复会话cookie,以便我可以重定向到再次设置cookie的文件,重定向我,我可以看到我的购物车并继续在Magento商店结账.

问题:Magento创建了一个cookie(包含会话ID或其他任何东西,我试图设置这个cookie手册并且他们已登录),而不是在客户登录时设置会话.我已经尝试了几个小时才能获得这个cookie由magento在我的magento web服务中设置.我打电话时似乎没有设置cookie

$session = Mage::getSingleton('customer/session');
return $session->getCookie()->get('frontend');
Run Code Online (Sandbox Code Playgroud)

继承人我的完整代码: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');
}

}
?>
Run Code Online (Sandbox Code Playgroud)

继承人在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);
?>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!MRU


对不起,我正在写一个答案,但评论框拒绝我写更多...信件.

我已经尝试过你发布的两个代码,我得到的只是一个空数组或Bool错误.我写了一个静态函数:

private static $cookie = array();
public static function cookie($key, $value)
{
    if($key == 'frontend') {
        self::$cookie['key']   = $key;
        self::$cookie['value'] = $value;
    }
}
Run Code Online (Sandbox Code Playgroud)

在Mage_Core_Model_Session_Abstract_Varien :: start中调用,我得到了前端cookie值:

Customapi_Model_Core_Api::cookie($sessionName, $this->getSessionId());
Run Code Online (Sandbox Code Playgroud)

在第125行.

但这并没有解决我的基本问题:虽然将Api调用设置为正确的值,但无法恢复在Api调用中创建的会话.

谢谢你的帮助!

Tia*_*ves 7

您可以使用以下命令获取所有cookie的数组:

Mage::getModel('core/cookie')->get();
Run Code Online (Sandbox Code Playgroud)

可以像这样检索前端cookie:

Mage::getModel('core/cookie')->get('frontend');
Run Code Online (Sandbox Code Playgroud)

从你评论的代码我可以看出你已经知道了.

据我所知,当您登录用户时,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
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,您现在想让用户在您的服务器中打开一个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');
?>
Run Code Online (Sandbox Code Playgroud)

应该这样做.您的用户现已登录(至少我让它在我的测试环境中工作).您应该记住的一件事是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
)
Run Code Online (Sandbox Code Playgroud)

确保在设置>配置>常规> Web>会话验证设置中关闭Magento管理中的验证