开发者

Preserving session data between requests using PHP's SoapClient

I have developed a开发者_开发技巧n ASP.NET Web Service that performs queries to a database. Now I am developing a PHP Web site that consumes the Web Service for anything that requires access to the database. I have written the following function which creates a new SoapClient to consume the Web Service:

function get_soap_client()
{
    if (!file_exists('wsdl_path.txt')
        return NULL;

    if (!$file = fopen('wsdl_path.txt', 'r'))
        return NULL;

    $path = fgets($file);
    fclose($file);

    return new SoapClient($path);
}

I have already tested this function as means to get a client to a stateless Web Service, and it does work. For example, this is my Web site's login function (whose control flow is not affected by state):

function try_login($user, $pass)
{
    $client = get_soap_client();

    // ASP.NET Web Services encapsulate all inputs in a single object.
    $param = new stdClass();
    $param->user = $user;
    $param->pass = $pass;

    // Execute the WebMethod TryLogin and return its result.
    // ASP.NET Web Services encapsulate all outputs in a single object.
    return $client->TryLogin($param)->TryLoginResult;
}

Which in turn calls the following WebMethod:

[WebMethod(EnableSession = true)]
public bool TryLogin(string user, string pass)
{
    SqlParameter[] pars = new SqlParameter[2];
    pars[0] = new SqlParameter("@User", SqlDbType.VarChar, 20);
    pars[0].Value = user;
    pars[1] = new SqlParameter("@Pass", SqlDbType.VarChar, 20);
    pars[1].Value = pass;

    // The Stored Procedure returns a row if the user and password are OK.
    // Otherwise, it returns an empty recordset.
    DataTable dt = Utilities.RetrieveFromStoredProcedure('[dbo].[sp_TryLogin]', pars);
    if (dt.Rows.Count == 0) // Wrong user and/or password
    {
        Context.Session.Abandon();
        return false;
    }
    else
        return true;
}

However, I don't know whether the Web Service's Context.Session data is preserved between consecutive requests made from SoapClient objects retrieved by get_soap_client(). So I have the following questions:

  1. Is the Web Service's Context.Session data preserved between consecutive requests made from a SoapClient object retrieved by get_soap_client()?

  2. Is the Web Service's Context.Session data preserved between consecutive requests made from different SoapClient objects retrieved on the fly as I need them?

  3. If the answer to the previous question is "no", is there any way to serialize the SoapClient's session data into a PHP state variable?


Ah, you mean is it sending the SESSION ID. I imagine you'll need to call __setCookie manually to set a cookie with the SESSION ID to your web service.

http://php.net/manual/en/soapclient.setcookie.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜