php - SOAP PHP 错误解析 WSDL : failed to load external entity?

标签 php xml soap wsdl wamp

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

(SoapFault)[2] message which states: 'SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost/MyRegistration/login.xml' : failed to load external entity "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 服务器版本 2.2,以及 mySQL 5.5.24 和 PHP 5.3.13

最佳答案

Security issue: This answer disables security features and should not be used in production!

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

我的错误:

failed to load external entity

如何解决:

// 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);

关于php - SOAP PHP 错误解析 WSDL : failed to load external entity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875409/

相关文章:

php - 使用 materializecss 根据 mysqli 值更改表的行颜色

php - 为什么我的 Highcharts 堆积面积样条图看起来很有趣?

java - Launch4j 将 jar 转为 exe

java - 尝试与端点通信时无法识别 WSDL 类型

php - 使用 PHP : 在我的网页上上传/显示视频

php - 验证电子邮件已打开

xml - 使用翻译功能删除 XSLT 中的单词 'and'

php - 通过 php 处理 XML 目录

php - 从 php 访问 python 脚本中的函数

java - 调用WebService时SSL握手失败