PHP – > SOAP – > Magento Webservice:获取由Magento
我是Magento Web-Service的新手,必须扩展它.
Webservice shell能够登录客户,给我回复会话cookie,以便我可以重定向到再次设置cookie的文件,重定向我,我可以看到我的购物车并继续在Magento商店结账. 问题: $session = Mage::getSingleton('customer/session'); return $session->getCookie()->get('frontend'); 继承我的完整代码: <?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错误. 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行. 但这并没有解决我的基本问题: 谢谢你的帮助! 解决方法
您可以使用以下命令获取所有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管理员中关闭验证>配置>一般>网络>会话验证设置 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |