问题
I'm trying to pass the Header and Body to a SOAP Request. Due to the wrong practice, I'm getting the Connection error. When I tried the same using SOAP UI, Im getting the Proper response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:adp="http://abcddetails.com/">
<soapenv:Header>
<adp:UserIdentifierSoapHeaderIn>
<!--Optional:-->
<adp:UserName>USER1</adp:UserName>
<!--Optional:-->
<adp:Password>PASS</adp:Password>
</adp:UserIdentifierSoapHeaderIn>
</soapenv:Header>
<soapenv:Body>
<adp:getVehicleDetails>
<!--Optional:-->
<adp:request>
<adp:SystemCode>101</adp:SystemCode>
<!--Optional:-->
<adp:UserID>101</adp:UserID>
<!--Optional:-->
<adp:PlateInfo>
<adp:PlateNo>44444</adp:PlateNo>
<adp:PlateOrgNo>1</adp:PlateOrgNo>
<adp:PlateColorCode>48</adp:PlateColorCode>
<adp:PlateKindCode>1</adp:PlateKindCode>
<adp:PlateTypeCode>1</adp:PlateTypeCode>
<adp:PlateSourceCode>3</adp:PlateSourceCode>
</adp:PlateInfo>
</adp:request>
</adp:getVehicleDetails>
</soapenv:Body>
</soapenv:Envelope>
The below is my code:
<?php
echo "Hello world";
echo "ADDED the below two lines"
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$wsdl = "https://abcddetails.com/getSoapDetails.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace'=>1));
$auth = array(
'Username'=>'USER1',
'Password'=>'PASS',
);
$header = new SOAPHeader($wsdl, 'UserIdentifierSoapHeaderIn', $auth);
$client->__setSoapHeaders($header);
echo "Header Passed... Body starts";
// web service input params
$request_param = array(
'getCarDetails' => array(
'request' => array(
'SystemCode' => 101,
'UserID' => 101),
'PlateInfo' => array(
'PlateNo' => 44444,
'PlateOrgNo' => 1,
'PlateColorCode' => 48,
'PlateKindCode' => 1,
'PlateTypeCode' => 1,
'PlateSourceCode' => 3 )
)
);
$responce_param = null;
try
{
$responce_param = $client->__soapCall('getCarDetails', ['parameters' => $request_param]);
}
catch (Exception $e)
{
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
print_r($responce_param);
?>
The error message is
Could not connect to host
But as said above, the same xml request is giving proper response through Soap UI application. What could be the issue here? I doubt on the Header assignment, Is that so, or somewhere else?
回答1:
Try adding these to your client when you instantiate it. I've had it in the past where it caches from the wsdl and doesn't always connect, but this helps that. The trace and exceptions aren't essential but help IMO.
array('trace' => 1, 'cache_wsdl'=>WSDL_CACHE_NONE, 'exceptions' => true)
来源:https://stackoverflow.com/questions/52967419/how-to-pass-soap-headers-and-body-as-parameter-in-php