I am working with a SOAP API for an e-commerce shopping cart and I can't seem to be able to get the session to persist through different pages.
As an example, I have some test code below (with a bunch of debug messages) which adds an item to the cart and then views the cart. When I run this in my browser it works perfectly, but if I refresh the page, I am expecting there to then be two items in the cart (one from each of the two page calls). However, it doesn't seem to remember anything from previous calls to the page. I know the API is working because if I just call "AddToCart" twice on this page and then call "GetCartContents" I see the 2 items in the cart correctly.
Am I missing something?
<?php
session_start();
$url = "https://www.example.com/soap-checkout.php?wsdl";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
if (!empty($_SESSION['soapcookies'])) {
foreach($_SESSION['soapcookies'] AS $name=>$value) {
if (is_array($value)) {
foreach($value AS $k=>$v) {
$client->__setCookie($name[$k], $v);
}
} else {
$client->__setCookie($name, $value);
}
}
}
echo "<pre style='background: grey;'>browser cookies: ".print_r($_COOKIE, true)."</pre>";//debug
// Add to Cart
$data = array(
"domain" => 'www.example.net',
"sku" => "1234",
"qty" => 1
);
$result = $client->__soapCall("AddToCart", $data);
$responseHeader = $client->__getLastResponseHeaders();//debug
echo "<pre style='background: purple;'>responseHeader: ".print_r($responseHeader, true)."</pre>";//debug
echo "<pre style='background: grey;'>cookies: ".print_r($client->_cookies, true)."</pre>";//debug
echo "<pre style='background: yellow;'>Result of 'AddToCart': ".print_r($result, true)."</pre>";//debug
// Set SOAP Cookies in PHP Session
$_SESSION['soapcookies'] = $client->_cookies;
// View Cart
$data = array(
"domain" => 'www.example.net'
);
$result = $client->__soapCall("GetCartContents", $data);
echo "<pre style='background: skyblue;'>Result of 'GetCartContents': ".print_r($result, true)."</pre>";//debug
?>
FYI, the PHP version is 5.2.6
Your approach looks correct, if your goal is to attempt to tie an active user session to particular interactions with the SOAP API. Your approach to store and set the SOAP cookies in the session and restoring to SOAP session via setCookie is also the way to go.
Your code is not working probably due to the error on the line:
$client->__setCookie($name[$k], $v);
Which needs to be:
$client->__setCookie($name, $v);
Hope that helps.