加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP – > SOAP – > Magento Webservice:获取由Magento

发布时间:2020-12-13 17:37:38 所属栏目:PHP教程 来源:网络整理
导读:我是Magento Web-Service的新手,必须扩展它. Webservice shell能够登录客户,给我回复会话cookie,以便我可以重定向到再次设置cookie的文件,重定向我,我可以看到我的购物车并继续在Magento商店结账. 问题: Magento创建一个cookie(包含会话ID或其他任何内容,我
我是Magento Web-Service的新手,必须扩展它.
Webservice shell能够登录客户,给我回复会话cookie,以便我可以重定向到再次设置cookie的文件,重定向我,我可以看到我的购物车并继续在Magento商店结账.

问题:
Magento创建一个cookie(包含会话ID或其他任何内容,我试图设置此cookie手册并且他们已登录),而不是在客户登录时设置会话.
我已经尝试了几个小时来获取由我的magento web服务中的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错误.
我写了一个静态函数:

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调用设置为正确的值,但无法恢复在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'],'/your/magento/cookie/path');
header('Location: http://example.com');
?>

应该这样做.您的用户现已登录(至少我让它在我的测试环境中工作).您应该记住的一件事是Magento有一个会话验证机制,如果启用它将失败.这是因为您的会话存储了您正在使用的浏览器,您要连接的IP等信息.这些数据在通过API方法和稍后的浏览器之间的调用之间不匹配.以下是在API方法中设置会话后命令print_r($session-> 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管理员中关闭验证>配置>一般>网络>会话验证设置

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读