Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Soap Fault: Could not connect to host

There are many question is similar like this. But I didn't got the solution when tried. Find my codes below:

Soap XML:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pos="http://PostWebService.org/">
   <soap:Header>
      <pos:Authentication>
         <!--Optional:-->
         <pos:UserName>xxxxxxx</pos:UserName>
         <!--Optional:-->
         <pos:Password>yyyyyyyy</pos:Password>
      </pos:Authentication>
   </soap:Header>
   <soap:Body>
       .....
   </soap:Body>
</soap:Envelope>

PHP Code:

$client = new SoapClient({soapurl},$params);
$auth = new stdClass();
$auth->UserName = 'xxxxxxx';
$auth->Password = 'yyyyyyyy'; 
$header = new SoapHeader('NAMESPACE','Authentication',$auth,false);
$client->__setSoapHeaders($header); 

$result = $client->__soapCall('{soap function}',array()); // when this line executes it throws me the error "Could not connect to the host"

Please note, My Soap URL is in https. I tried lots other solutions from stack overflow but none of them work. I want anyone tell me why i am getting that error and what i did wrong in the request.

Another Code tried but still the same:

    $opts = array(
        'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
    );
    // SOAP 1.2 client
    $params = array ('encoding' => 'UTF-8', 'verifypeer' => false, 'verifyhost' => false, 'soap_version' => SOAP_1_2, 'keep_alive' => false,'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create($opts) );
    $client = new SoapClient({soapurl},$params);

   //Remaining as the same above

When I tried with SoapUI it gives me a response.

like image 591
Pradeep shyam Avatar asked Dec 23 '22 15:12

Pradeep shyam


1 Answers

Finally, I got the solution,

I found the problem. The host I was trying to connect is redirecting to another domain name. For some reason, PHP 5.6 doesn't carry the location automatically. So i defined the same soap url in the location options.

Eg:

$params = array('location' => {soapurl});
$client = new SoapClient({soapurl},$params);

Thanks for your time. :)

like image 173
Pradeep shyam Avatar answered Dec 28 '22 11:12

Pradeep shyam