2011-04-05 3 views
4

Magento Web-Service를 처음 사용하고 있으며,이를 확장해야합니다. Webservice 셸은 고객에게 로그인 할 수있게되어 세션 쿠키를 다시 돌려서 쿠키를 다시 설정하고 리디렉션하는 파일로 리디렉션 할 수있게되어 장바구니를보고 Magento Store에서 체크 아웃 할 수 있습니다.PHP -> SOAP -> Magento Webservice : Magento에서 쿠키를 설정하십시오.

문제는 :. 젠토 쿠키 (즉, 필자가이 쿠키 설명서를 설정하려고하고는 메신저 로그인, 세션 ID 또는 무엇이든을 포함) 대신에 세션을 설정을 생성에서 고객 로그 나는 시도했습니다 때 이제 Magento 웹 서비스에서 magento로 설정된이 쿠키를 가져 오는 데 몇 시간 동안 걸립니다. 이 쿠키는 내가

$session = Mage::getSingleton('customer/session'); 
return $session->getCookie()->get('frontend'); 

를 호출 할 때 설정되어 있지 않은 것 같습니다 Heres는 내 완전한 코드 : 젠토 Webservice를 API 광고 : PHP에서

<?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'); 
} 

} 
?> 

Heres는 내 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' => '[email protected]', 
      '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


내가 답을 쓰고 있지만, 주석 상자가 나를 ...보다 문자를 더 쓸 거부 죄송합니다.

내가 올린 코드를 모두 시도했는데 그 중 하나가 빈 Array 또는 Bool false입니다. 나는 정적 함수를 썼다 : Mage_Core_Model_Session_Abstract_Varien에라고

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

시작 ::와 나는 프론트 엔드 쿠키 값 있어요 : 라인 (125)

에서

Customapi_Model_Core_Api::cookie($sessionName, $this->getSessionId()); 

을하지만 didnt가 해결할 내 기본 문제 : Api 호출에서 생성 된 세션은 올바른 값으로 설정되었지만 복원 할 수 없습니다.

도움 주셔서 감사합니다.

다음과 같은 명령을 사용하여 모든 쿠키의 배열을 얻을 수
+0

hello @Mru, 나는 magento에서 똑같은 일을하고 있습니다. 친절하게도 제게 안내해 주시겠습니까? magento 단계로 고객 로그인을위한 webservice를 어떻게 만들 수 있습니까? – Deep

답변

7

: 나는 당신이 볼 수있는 주석 코드에서

Mage::getModel('core/cookie')->get('frontend'); 

: 프론트 엔드 쿠키는 다음과 같이 검색 할 수 있습니다

Mage::getModel('core/cookie')->get(); 

을 이미 그것을 알고있었습니다.

내가 아는 한 사용자에게 로그인하면 Magento는 새로운 세션 ID를 만들지 않고 활성 연결의 세션 ID (PHP 자체에서 생성)를 사용합니다. 사용자를 로그인하고 API 클라이언트가 방금 Magento로 만든 세션에 연결합니다. 따라서 사용자가 주석을 작성한 코드는 사용자가 달성하려고 시도하는 것이 올바른 것으로 보입니다.

이제 반환 된 세션 ID를 가져와 새 요청에 '프런트 엔드'쿠키로 사용해야합니다.

편집 (두 번째 시간)

젠토는 다른 범위에 사용하는 단일 PHP 세션 내부에 다른 세션이 있습니다. 예를 들어, 핵심 범위, 고객 범위 등이 있습니다. 그러나 고객 범위는 특정 웹 사이트에만 적용됩니다. 따라서 customer_website_one 및 customer_website_two 범위를 가질 수 있습니다.

사용자로 로그인하려면 Magento에 웹 사이트가 무엇인지 말해야합니다. 이제 사용자가 당신이 당신의 API 메서드에서 반환 무엇에 젠토 쿠키를 설정하여 서버의 PHP 스크립트를 열 수 있도록하려면, 예를 들어

// 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 

내가 제대로 이해하면

으로 다음 코드를 가져 가라. 다음과 같이 액세스 할 수있는 다음 예제를 작성했습니다. 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 메소드를 통한 호출과 브라우저간에 일치하지 않습니다. 여기

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

당신이 설정에서 젠토 관리자의 확인의 켜 있는지 확인 API 방법에 세션을 설정 한 후 명령 print_r($session->getData())의 예 출력은> 설정> 일반> 웹> 세션 유효성 검사 설정

+0

여기에 충분한 문자가 허용되지 않아 답변을 게시했습니다. 죄송합니다. – michiruf

+0

다시 고마워합니다. 하지만 그건 내 문제를 해결하지 못했습니다. 나는 쿠키를 얻으려고하기 전에 이것을 시도했지만 세션이 "복원"될 수 없기 때문에 작동하지 않았습니다. 내가 tryto 내 문제를 다시 설명 : magento (쿠키를 설정하는 스크립트가 잘 작동)로 리다이렉트 할 때 비누 API 호출로 생성 된 세션은 "복원"되지 않습니다. Api 콜 이후 또는 다른 세션 후에 세션을 비우는 것이 있는지 알고 있습니까? – michiruf

+0

나는 나의 새로운 발견으로 나의 대답을 넓혔다. 당신이 볼 수있는 것처럼 실제로는 꽤 까다 롭습니다. 나는 이제 당신의 문제를 완전히 이해했기를 바랍니다. –