title: PHP Soap Fault "Could not connect to host"
body: <p>There are <a href="https://stackoverflow.com/questions/4318870/soapfault-exception-could-not-connect-to-host">some</a> <a href="https://stackoverflow.com/questions/11165142/soap-error-fatal-error-uncaught-soapfault-exception-http-could-not-connect-t?lq=1">questions</a> <a href="https://stackoverflow.com/questions/30410658/php-soapclient-soapfault-exception-could-not-connect-to-host">in</a> <a href="https://stackoverflow.com/questions/13009673/php-soap-could-not-connect-to-host">SO</a> with the same problem, but any of them helped me.</p>
  
  <p>Here is my code to call a Soap Web service:</p>
  
  <pre><code>$client = new SoapClient($wsdl, array(
                                 'soap_version' =&gt; SOAP_1_1,
                                 'trace' =&gt; 1,
                                 'exceptions'=&gt; true,
                                 'encoding' =&gt; 'UTF-8',
                                 'compression' =&gt; SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_DEFLATE,           
                                 'cache_wsdl' =&gt; WSDL_CACHE_NONE
                                  ))
  $params = new SoapVar($xmlString, XSD_ANYXML, null, null);
  try {
      $client-&gt;functionName($params);
  catch (SoapFault $f) {
      print_r ($f);
  }
  </code></pre>
  
  <p>This is (part of) the SoapFault Object:</p>
  
  <pre><code>SoapFault Object
  (
      [message:protected] =&gt; Could not connect to host
      [string:Exception:private] =&gt; 
      [code:protected] =&gt; 0
      [file:protected] =&gt; /home/user/Documentos/Web/Client.class.php //fileName
      [line:protected] =&gt; 49 //line where I have $client-&gt;functionName
      [trace:Exception:private] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [function] =&gt; __doRequest
                      [class] =&gt; SoapClient
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; &lt;?xml version="1.0" encoding="UTF-8"?&gt; // the XML
  
                              [1] =&gt; https://example.company.com/WSDLName.svc //name of WSDL
                              [2] =&gt; http://example.org/Pull/functionName //URL with name of the function
                              [3] =&gt; 1
                              [4] =&gt; 0
                          )
  
                  )
  
              [1] =&gt; Array
                  (
                      [file] =&gt; /home/user/Documentos/Web/Client.class.php //fileName
                      [line] =&gt; 49
                      [function] =&gt; __call
                      [class] =&gt; SoapClient
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; functionName
                              [1] =&gt; Array
                                  (
                                      [0] =&gt; SoapVar Object
                                          (
                                              [enc_type] =&gt; 147
                                              [enc_value] =&gt; //the XML
                                          )
  
                                  )
  
                          )
  
                  )
  </code></pre>
  
  <p>If I call <code>print_r($client-&gt;__getLastRequest());</code> and test the printed XML in <a href="http://www.soapui.org/" rel="nofollow noreferrer">SoapUI</a> all works like a charm.</p>
  
  <p>If I call <code>print_r(file_get_contents($wsdl));</code> I got the WSDL file.</p>
  
  <p>Trying <code>var_dump($client-&gt;__getLastRequestHeaders())</code>, getting <code>null</code>;</p>
  
  <p><strong>My attempts until now:</strong></p>
  
  <p>1) Putting a <code>ini_set('soap.wsdl_cache_enabled',0);</code> just before the line <code>$client = new SoapClient...</code>. Same error.</p>
  
  <p>2) Changes the call line to <code>$client-&gt;__soapCall('functionName',(array)$params)</code>. Same error. The only difference is the XML formatted with <code>&amp;lt;</code> and <code>&amp;#13;</code>.</p>
  
  <p>3) Change my soap options just to <code>array('soap_version' =&gt; SOAP_1_1,'trace' =&gt; 1)</code>. Same error.</p>
  
  <p>4) The wsdl URL is a http URL, so I believe that is not a SSL problem. However, in the SoapFault object, I receive the same URL with https, so I changed my $wsdl to the URL with https:// and receive the error <code>SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://example.company.com/WSDLName.svc?wsdl'</code>.</p>
  
  <p>As I said, all works fine in SoapUI.</p>
  
  <p>Finally, here is my server info:</p>
  
  <blockquote>
    <p>Ubuntu 14.04.3 LTS</p>
    
    <p>Apache 2.4.7</p>
    
    <p>PHP 5.5.9</p>
  </blockquote>
  
  <p>Someone has any idea on what I doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Following the comments below, here are more information:</p>
  
  <p>1) Both <code>http</code> and <code>https</code> URLs in the browser returns the WSDL document.</p>
  
  <p>2) If I ping the wsdl URL in terminal, I receive <code>ping: unknown host https://example.org/Pull/functionName</code>. The WSDL also has a method named Ping, defined as:</p>
  
  <pre><code>&lt;wsdl:operation name="Ping"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/IPull/Ping" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  </code></pre>
  
  <p>When I use it in PHP I got the same scenario: A Soap Fault "Could not connect to host", but the XML created works fine in SoapUI.</p>
  
  <p>3) The result of <code>var_dump($client)</code> is:</p>
  
  <pre><code>object(SoapClient)[42]
    public 'trace' =&gt; int 1
    public 'compression' =&gt; int 32
    public '_encoding' =&gt; string 'UTF-8' (length=5)
    public '_soap_version' =&gt; int 1
    public 'sdl' =&gt; resource(30, Unknown)
  </code></pre>
  
  <p><strong><em>Note</em></strong>: I got a workaround to deal with the problem that is definitively not the best option, but works. 
  I created the whole XML body (inclusing the soap envelope) using <code>DOMDocument</code> or <code>SimpleXML</code> and send it via <a href="http://php.net/manual/en/book.curl.php" rel="nofollow noreferrer"><code>cURL</code></a> to the URL <code>https://example.company.com/WSDLName.svc</code>.<br>
  It's ugly, but works to me for the moment while I'm waiting for any other solution.</p>