PHP & # 8594; SOAP & # 8594; Magento Webservice: Get Cookies Set by Magento

I am new to Magento Web-Service and should extend it. The Webservice shell can log in with the client, give me a session cookie so that I can redirect the cookie that sets the cookie again, redirect me, and I can see my cart and proceed to checkout in the Magento store.

Problem: Magento creates a cookie (which contains a session ID or something else, ive tried to set this guide for cookies and logging in) instead of setting up a session when a user logs in. I tried for several hours now to get this cookie set by magento on my magento web service. Cookie does not seem to be set when I call

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

Here is my complete code: 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'); } } ?> 

Heres my Api Call in Php:

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

Thanks for the help! CPM


Sorry to write the answer, but the comment box has forbidden me to write more than ... letters.

I tried both the codes you sent, and all I get is an empty array or a Bool false. I wrote a static function:

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

which is called in Mage_Core_Model_Session_Abstract_Varien :: start, and I got a cookie value for frontend:

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

on line 125.

But this did not solve my main problem: The session created in the Api call cannot be restored, although it is set to the correct value.

Thank you for your help!

+4
source share
1 answer

You can get an array of all your cookies with the following command:

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

The cookie for frontend can be restored as follows:

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

From your commented code, I see that you already knew this.

As far as I know, when logging in, Magento does not just create a new session identifier, it uses the session identifier of the active connection (which is generated by PHP itself). You log in to the user and associate it with the session that the API client just created, with Magento. So the code you commented seems to be correct for what you are trying to achieve.

Now you just need to get the returned session identifier and use it in the new request as the cookie 'frontend'.

Change (second time)

Magento has different sessions within the same PHP session, which it uses for different areas. For example, there is a main area, the scope of the client, etc. However, the scope of the client is also specific to this website. That way you can have client_website_one and a customer_website_two area.

If you want to log in to your account, you must tell Magento which website. Take the following code as an example.

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

If you understand correctly, now you want the user to open a PHP script on your server that sets Cookie Magento to what you just returned in your API method. I wrote the following example, which you would get as follows: 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'); ?> 

That should do it. Now your user has registered (at least I got it to work in a test environment). The only thing you should keep in mind is that Magento has a session verification mechanism that will work if it is enabled. This is because your session stores information about which browser you are using, the IP address from which you are connecting, etc. This data will not coincide between calls through the API methods and the browser later. The following is an example output from the print_r($session->getData()) command print_r($session->getData()) after setting up a session in the API method

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

Make sure you return the confirmation to Admin Magento in Settings> Configuration> General> Web> Session Verification Settings

+7
source

Source: https://habr.com/ru/post/1346754/


All Articles