在PHP中向SOAP webservice发出请求



第三方给了我他的webservice文档进行测试。我尝试通过传递值到header和body来连接到soap web服务。

我要使用的方法是ConsultarAfiliado。

soap结构为:

POST /WSAutorizaciones/WSAutorizacionLaboratorio.asmx HTTP/1.1
Host: 191.97.91.43
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://arssenasa.gob.do/ConsultarAfiliado"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthenticationHeader xmlns="https://arssenasa.gob.do/">
<Cedula>string</Cedula>
<Password>string</Password>
<Proveedo>int</Proveedo>
</AuthenticationHeader>
</soap:Header>
<soap:Body>
<ConsultarAfiliado xmlns="https://arssenasa.gob.do/">
<TipoDocumento>int</TipoDocumento>
<NumDocumento>string</NumDocumento>
</ConsultarAfiliado>
</soap:Body>
</soap:Envelope>

我试过这些代码:

$wsdl =http://191.97.91.43/WSAutorizaciones/WSAutorizacionLaboratorio.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace' => 1));  // The trace param will show you errors stack
$auth =array('Cedula' => '001-0945751-5', 'Password' => 'dmfvmxm2', 'Proveedo' => '12077');
$header = new SoapHeader('NAMESPACE','AuthenticationHeader',$auth,false);
var_dump($client->__setSoapHeaders($header));
// web service input params
$request_param = array('TipoDocumento' => '2', 'NumDocumento' => '021827151');
$responce_param = null;
try {
$responce_param = $client->ConsultarAfiliado($request_param);
print_r($responce_param->ConsultarAfiliadoResponse);
} catch (Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}

我得到这个错误:

异常错误!服务器无法处理请求。——比;对象引用未设置为对象的实例

请有人帮我解决这个问题。
WSDL: http://191.97.91.43/WSAutorizaciones/WSAutorizacionLaboratorio.asmx?WSDL

最后我成功地找出了那个SOAP报头的问题。我们知道webservice在API上已经过时了,但是一些网站仍然使用webservice (WSL)来给客户端响应。

我所做的唯一更改是在NAMESPACE中,只需将其替换为AuthenticationHeader中的url:

错误:

$header = new SoapHeader('NAMESPACE','AuthenticationHeader',$auth,false);

更正:

$header = new SoapHeader('https://arssenasa.gob.do/','AuthenticationHeader',$auth,false);

我看到了一些问题:

  1. wsdl变量缺少开头引号
  2. SoapHeader中的
  3. 必须设置为命名空间"https://arssenasa.gob.do/">
  4. 你不能直接调用`$ client-> ConsultAffiliate ($ request_param)`,你必须使用' ' ' ' __soapCall ' ' ' ',然后指定调用者的名字

我让你在代码下面工作

$wsdl = "http://191.97.91.43/WSAutorizaciones/WSAutorizacionLaboratorio.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace' => 1));  // The trace param will show you errors stack
$auth = array('Cedula' => '001-0945751-5', 'Password' => 'dmfvmxm2', 'Proveedo' => '12077');
//        $header = new SoapHeader('NAMESPACE', 'AuthenticationHeader', $auth, false);
$header = new SoapHeader('https://arssenasa.gob.do/', 'AuthenticationHeader', $auth, false);
$client->__setSoapHeaders($header);
// web service input params
$request_param = array('TipoDocumento' => '2', 'NumDocumento' => '021827151');
$responce_param = null;
try {
$responce_param = $client->__soapCall('ConsultarAfiliado',$request_param);
//            $responce_param = $client->ConsultarAfiliado($request_param);
var_dump($responce_param);
} catch (Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}

现在服务回答我这个,但我不知道这是否是预期的答案:

{
+"ConsultarAfiliadoResult": {#801
+"Contrato": 0
+"IdEstado": 0
+"CodigoFamiliar": 0
+"IdRegimen": null
+"Edad": "0"
+"TipoDocumento": 0
+"CodigoAfiliado": 0
+"MensajeAfiliado": """
System.NullReferenceException: Object reference not set to an instance of an object.
at SeNaSa.Autorizaciones.Services.AutorizationUtils.DocumentValidations(Int32 TipoDocumento, String NumDocumento)
at SeNaSa.Autorizaciones.Services.AfiliadoServices.GetAfiliadoInfo(Int32 tipoDocumento, String numDocumento, Int32 Proveedo)
at WSAutorizaciones.WebService1.ConsultarAfiliado(Int32 TipoDocumento, String NumDocumento)
"""
}
}

最新更新