解析WSDL的SOAP PHP错误:无法加载外部实体?

问题描述 投票:16回答:8

我正在尝试使用PHP和SOAP运行Web服务,但到目前为止,我所得到的只是:

(SoapFault)[2]消息表明:'SOAP-ERROR:解析WSDL:无法从'http://localhost/MyRegistration/login.xml'加载:无法加载外部实体“http://localhost/MyRegistration/login.xml

我已经尝试将localhost更改为127.0.0.1,但这没有任何区别。 login实际上是一个wsdl文件,但是如果我把login.wsdl放在SOAPClient构造函数中,它会说“'看起来好像我们没有XML文档'”。

这是我的SOAP客户端代码(register_client.php):

<?php
try
{
    $sClient = new SoapClient('http://127.0.0.1/MyRegistration/login.wsdl');    

    $param1 = $_POST["regname"];
    $param2 = $_POST["regpass1"];

    $response = $sClient->loginVerify($param1, $param2);    

    var_dump($response);
}
catch(SoapFault $e)
{
    var_dump($e);
}
?> 

这是login.wsdl文件:

<?xml version="1.0"?>
<definitions name="LoginVal" 
    targetNamespace="urn:LoginVal" 
    xmlns:tns="urn:LoginVal"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Login">
  <xsd:element name="getName" type="xsd:string" />
<xsd:element name="getPass" type="xsd:string" />
  <xsd:element name="LoginResponse" type="xsd:string" />          
</xsd:schema>           
  </types>

  <message name="loginVerify">
<part name="username" type="tns:getName" />
<part name="password" type="tns:getPass" />
  </message>

  <message name="doLoginResponse">
<part name="return" type="tns:LoginResponse" />
  </message>  

  <portType name="LoginPort">
    <operation name="loginVerify">
  <input message="tns:loginVerify" />
  <output message="tns:doLoginResponse" />
    </operation>
  </portType>

  <binding name="LoginBinding" type="tns:LoginPort">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
  <operation name="loginVerify">
    <soap:operation soapAction="urn:LoginAction" />
    <input>
      <soap:body use="encoded" namespace="urn:Login" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
    </input>
    <output>
      <soap:body use="encoded" namespace="urn:Login" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
    </output>
  </operation>
  </binding>

  <service name="LoginService">
    <port name="LoginPort" binding="tns:LoginBinding">
  <soap:address location="http://localhost/MyRegistration/register.php" />
    </port>
  </service>

</definitions>

而且我不确定这是否涉及,所以我提供了SOAP服务器register.php的代码:

<?php
if(!extension_loaded("soap"))
{
    dl("php_soap.dll");
}

ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("login.wsdl", array('uri'=>'http://127.0.0.1/MyRegistration'))

public function loginVerify($username, $password)
{
    if($_POST["regname"] && $_POST["regemail"] && $_POST["regpass1"] && $_POST["regpass2"] )
    {
        if($_POST["regpass1"] == $_POST["regpass2"])
        {
            $servername = "localhost";
            $username = "root";
            $password = "Hellfire";

            $conn = mysql_connect($servername,$username,"Hellfire")or die(mysql_error());

            mysql_select_db("soap",$conn);

            $sql = "insert into users (name,email,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regpass1]')";

            $result = mysql_query($sql,$conn) or die(mysql_error());

            return "You have registered sucessfully";

            //print "<a href='index.php'>go to login page</a>";
        }
        else return "passwords dont match";
    }
    else return "invalid data";
}

$server->AddFunction("loginVerify");
$server->handle();
?>

如果我提供不必要的信息,我很抱歉,但我是一个完全新手 - 如果有人能指出为什么要生成这个SOAP错误,我真的很感激,我能做些什么来纠正它。

我正在使用WAMP Server 2.2版,mySQL 5.5.24和PHP 5.3.13

php xml soap wsdl wamp
8个回答
27
投票

迁移到PHP 5.6.5后,soap 1.2不再起作用了。所以我通过添加可选的SSL参数解决了这个问题。

我的错误:

无法加载外部实体

怎么解决:

// options for ssl in php 5.6.5
$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,
    'trace' => 1,
    'exceptions' => 1,
    'connection_timeout' => 180,
    'stream_context' => stream_context_create($opts)
);

$wsdlUrl = $url . '?WSDL';
$oSoapClient = new SoapClient($wsdlUrl, $params);

6
投票

将此代码置于任何Soap调用之上:

libxml_disable_entity_loader(false);

3
投票

在register_client.php上,确保可以从正在执行代码的计算机访问已传递给SoapClient的URL。

$sClient = new SoapClient('http://127.0.0.1/MyRegistration/login.wsdl');    

如果127.0.0.0不起作用,您可以尝试使用某些网络IP地址并查看。

让我知道如果它仍然没有为你修复它,我确实尝试了你的例子和改变路径(在我的开发环境中使它正确)已经修复了相同的错误。

我很想知道它是否适合你。


3
投票

试试这个。适合我

$options = array(
    'cache_wsdl' => 0,
    'trace' => 1,
    'stream_context' => stream_context_create(array(
          'ssl' => array(
               'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
          )
    ))

$client = new SoapClient(url, $options);

2
投票

我有同样的问题。

这个php设置解决了我的问题:

allow_url_fopen -> 1

1
投票

使用php soapClient获得与https WSDL URL类似的响应

SoapFault异常:[WSDL] SOAP-ERROR:解析WSDL:无法从...加载

服务器从PHP 5.5.9-1ubuntu4.21 >> PHP 5.5.9-1ubuntu4.23更新后,我的客户端机器osx 10.12.6 / PHP 5.6.30出了问题,但可以建立MS Web服务客户端连接没有问题。

当我尝试加载WSDL时,Apache2的server_access.log没有显示任何条目,因此我添加了'cache_wsdl' => WSDL_CACHE_NONE以防止客户端wsdl缓存,但仍然没有条目。最后我试图加载wsdl每CURL -i检查HEADERS但所有似乎都没关系..

只有libxml_get_last_error()提供了一些见解> SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

所以我在调用中添加了一些ssl选项:

$contextOptions = array(
    'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    ));

$sslContext = stream_context_create($contextOptions);

$params =  array(
    'trace' => 1,
    'exceptions' => true,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'stream_context' => $sslContext
    );

try {
    $proxy = new SoapClient( $wsdl_url, $params );
} catch (SoapFault $proxy) {
    var_dump(libxml_get_last_error());
    var_dump($proxy);
}

在我的情况下,'allow_self_signed' => true做了伎俩!


0
投票

如果有人有同样的问题,一种可能的解决方案是设置bindto流上下文配置参数(假设您从11.22.33.44连接到55.66.77.88):

$context = [
    'socket' => [
        'bindto' => '55.66.77.88'
    ]
];

$options = [
    'soapVersion' => SOAP_1_1,
    'stream_context' => stream_context_create($context)
];

$client = new Client('11.22.33.44', $options);

0
投票

尝试使用SoapClient时遇到了类似的问题。一切都工作正常,但在生产中,有时在页面刷新,我会得到“SoapFault异常:[WSDL] SOAP-ERROR:解析WSDL:无法从...加载”错误。

我正在使用params:

new \SoapClient($WSDL, array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true, "exception" => 0)); 

删除所有为我工作的params:

new \SoapClient($WSDL); 
© www.soinside.com 2019 - 2024. All rights reserved.