0

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?

Sajeev
  • 783
  • 3
  • 14
  • 46
  • @DanielO, Hi, Can you look in to this – Sajeev Oct 24 '18 at 12:20
  • `Could not connect to host` sounds like an network / dns / firewall issue. – odan Oct 25 '18 at 06:44
  • I have tried the below: <1>. I have tried from different network, but the same issue. <2>. Then default_socket_timeout = 600 (Increased) <3>. Wamp -> Soap is enabled I'm able to retrieve the response while requesting through Soap UI and Postman applications. Only the Php code is giving me this issue. Is there anu issue in my code? – Sajeev Oct 25 '18 at 06:53
  • Then try to delete the WSDL cache (wsdl-* files in in /tmp) and [disable](https://stackoverflow.com/a/303514/1461181) or enable the WSDL caching. Also try to disable tracing: `array('trace'=>0)` – odan Oct 25 '18 at 06:59
  • I have done in php.ini as follows <1> soap.wsdl_cache_enabled=0 <2> soap.wsdl_cache_ttl=0 <3> And in the code, I have added the line ini_set('soap.wsdl_cache_enabled',0); ini_set('soap.wsdl_cache_ttl',0); I have updated the code now. But the same issue exists. – Sajeev Oct 25 '18 at 07:08
  • Deleted the /tmp files -> the same result still. – Sajeev Oct 25 '18 at 07:11
  • The exception is :: SoapFault exception: [HTTP] Could not connect to host in C:\wamp64\www\SOAPTEST\Index.php:102 Stack trace: #0 [internal function]: SoapClient->__doRequest('__soapCall('getCarDetails...', Array) #2 {main} – Sajeev Oct 25 '18 at 07:12
  • 1
    It could also be an proxy or ssl related issue (e.g. a peer verification problem). Do you connect to a SSL WebService through a [proxy](https://bugs.php.net/bug.php?id=69783)? Is it a self signed ssl certificate? Try to disbale the peer verification. `'stream_context'=> stream_context_create(array('ssl'=> array('verify_peer'=>false,'verify_peer_name'=>false)))` – odan Oct 25 '18 at 07:41
  • <1>. Do you Connect through Proxy - NO. <2> Is it self signed ssl certificate: - NO <3> for disabling the peer verification, I have added the code as below. $context = stream_context_create(array('ssl'=> array('verify_peer'=>false,'verify_peer_name'=>false))); $client = new SoapClient($wsdl, array('trace'=>0, 'stream_context' => $context)); But still the same issue exists. – Sajeev Oct 25 '18 at 08:28
  • What I understood in this case, my side there is no SSL and the server side, they are using the SSL. So that the soap client connection is not able to done properly. Is that correct? – Sajeev Oct 25 '18 at 09:04
  • Both sides (client and server) still use SSL, but your client just doesn't verify the SSL peer and peer name. This is useful for self signed ssl certificates only. You are still using SSL, but it's not so secure anymore. By default all SSL checks are enabled. – odan Oct 25 '18 at 09:08
  • 1
    Please try another SoapClient like [zend-soap](https://docs.zendframework.com/zend-soap/client/) or try to post the SOAP data via [Curl](https://stackoverflow.com/a/7157785/1461181). – odan Oct 25 '18 at 09:11
  • In the production, I will use the SSL. This is my test environment so that I don't have the SSL. – Sajeev Oct 25 '18 at 09:12
  • Thanks @DanielO I will try zend-soap and update here. – Sajeev Oct 25 '18 at 09:18
  • 1
    Thanks @DanielO I have tried the curl and its working perfectly. No connection issues. I have replaced the code. I will add the code, may be helpful to some one else – Sajeev Oct 25 '18 at 11:37

1 Answers1

0

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)

Nate_T
  • 23
  • 1
  • 6