PHP Soap Fault:无法连接到主机

问题描述:

有许多类似的问题。但是我尝试时没有得到解决方案。找到我的代码如下:PHP Soap Fault:无法连接到主机

肥皂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代码:

$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" 

请注意,我的肥皂URL是HTTPS。我尝试了很多来自堆栈溢出的其他解决方案,但都没有工作。我希望任何人告诉我为什么我会得到这个错误,以及我在请求中做了什么错误。

另一个代码尝试,但仍然是相同的:

$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 

当我用了SoapUI尝试它给了我一个答复。

最后,我得到了解决,

我发现这个问题。我试图连接的主机正在重定向到另一个域名。出于某种原因,PHP 5.6不会自动携带该位置。所以我在位置选项中定义了相同的soap url。

如:

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

感谢您的时间。 :)