3

使用 PHP 发送 SOAP 请求时遇到此错误SoapClient

Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host in /var/www/phpwebservice/soap-client.php:6
Stack trace:
#0 [internal function]: SoapClient->__doRequest('__call('getCatalogEntry', Array)
#2 /var/www/phpwebservice/soap-client.php(6): SoapClient->getCatalogEntry('catalog1')
#3 {main} thrown in /var/www/phpwebservice/soap-client.php on line 6

直接在/var/www/含义下移动文件时脚本工作正常:

http://localhost/soap-client.php

phpwebservice但是当我将它移动到一个名为该错误的子文件夹时。子文件夹是这样的没关系:

http://localhost/phpwebservice/soap-client.php

或者如果我为它制作了一个 VirtualHost:

http://phpwebservice/soap-client.php

的内容soap-client.php

    $client     = new SoapClient("catalog.wsdl");
    $catalogId      = 'catalog1';
    $response       = $client->getCatalogEntry($catalogId);
    echo $response;

肥皂-server.php:

    function getCatalogEntry($catalogId){
    if($catalogId == 'catalog1')
            return  "<html>
                <head>
                    <title>catalog</title>
                </head
                <body>
                <p> </p>
                    <table border=1>
                    <tr>
                        <th>catalogid</th>
                        <th>journal</th>
                        <th>section</th>
                        <th>edition</th>
                        <th>title</th>
                        <th>author</th>
                    </tr>
                    <tr>
                        <td>catalog1</td>
                        <td>ibm developerworks</td>
                        <td>xml</td>
                        <td>october 2005</td>
                        <td>jaxp validation</td>
                        <td>brett mclaughlin</td>
                    </tr>
                    </table>
                </body>
            </html>";
elseif($catalogId == 'catalog2')
    return  "<html>
                <head>
                    <title>catalog</title>
                </head>
                <body>
                    <p> </p>
                    <table border=1>
                        <tr>
                            <th>catalogid</th>
                            <th>journal</th>
                            <th>section</th>
                            <th>edition</th>
                            <th>title</th>
                            <th>author</th>
                        </tr>
                        <tr>
                            <td>catalog1</td>
                            <td>ibm developerworks</td>
                            <td>xml</td>
                            <td>july 2006</td>
                            <td>the java xpath api</td>
                            <td>elliotte harold</td>
                        </tr>
                    </table>
                </body>
            </html>";
    }
    ini_set("soap.wsdl_cache_enabled", "0");
    $server = new soapserver("catalog.wsdl");
    $server->addfunction("getCatalogEntry");
    $server->handle();

目录.wsdl:

   <?xml version ='1.0' encoding ='UTF-8' ?>
   <definitions name='Catalog'
   targetnamespace='http://phpwebservice/catalog'
   xmlns:tns='http://phpwebservice/catalog'
   xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
   xmlns:xsd='http://www.w3.org/2001/XMLSchema'
   xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
   xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
   xmlns='http://schemas.xmlsoap.org/wsdl/'>
   <message name='getCatalogRequest'>
    <part name='catalogId' type='xsd:string'/>
   </message>
   <message name='getCatalogResponse'>
    <part name='Result' type='xsd:string'/>
   </message>
   <portType name='CatalogPortType'>
    <operation name='getCatalogEntry'>
                <input message='tns:getCatalogRequest'/>
        <output message='tns:getCatalogResponse'/>
    </operation>
   </portType>
   <binding name='CatalogBinding' type='tns:CatalogPortType'>
    <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/>
    <operation name='getCatalogEntry'>
    <soap:operation soapAction='urn:phpwebservice-catalog#getCatalogEntry'/>
        <input>
            <soap:body use='encoded' namespace='urn:phpwebservice-catalog' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
        </input>
        <output>
                    <soap:body use='encoded' namespace='urn:phpwebservice-catalog' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
        </output>
    </operation>
   </binding>
   <service name='CatalogService'>
    <port name='CatalogPort' binding='CatalogBinding'>
        <soap:address location='http://phpwebservice/soap-server.php'/>
   </port>
   </service>
   </definitions>

谢谢。

4

3 回答 3

3

如果你在开发环境中,最好设置soap.wsdl_cache_enabled=0php.ini. 否则,您必须在对 WSDL 文件(通常位于定义的目录中soap.wsdl_cache_dir)进行任何更改后手动删除 WSDL 缓存

您还可以参考:SoapFault 异常:无法连接到主机

于 2012-12-31T06:53:19.687 回答
1

当我的选项中未指定connection_timeout时,我遇到了特定 SOAP 服务的此问题。SoapClient将超时设置为 1 为我解决了这个问题。

$wsdl = 'https://...';
$options [
  'trace' => true,
  'exceptions' => true,
  'connection_timeout' => 1
];
$client = new SoapClient($wsdl, $options);
于 2014-10-01T14:52:39.380 回答
0

没关系,我只是从 wsdl 文件中的每一行中删除 /catalog ,现在一切正常,thanx 无论如何。

于 2012-06-25T19:42:55.740 回答