dongrong7267 2011-01-11 14:15
浏览 49
已采纳

使用PHP的SoapClient保留请求之间的会话数据

I have developed an 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?

  • 写回答

1条回答 默认 最新

  • douyazi1129 2011-01-11 14:46
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题