2012-10-13 76 views
13

我試圖運行使用PHP & SOAP Web服務,但所有我得到到目前爲止是這樣的:SOAP PHP故障解析WSDL:無法加載外部實體?

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

我試圖改變本地主機爲127.0.0.1,但沒什麼區別。登錄實際上是一個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 Fault,以及我能做些什麼來糾正它,我真的很感激它。

我使用WAMP Server版本2.2,使用MySQL 5.5.24和5.3.13 PHP

回答

3

在register_client.php確保已傳遞給SoapClient的網址是來自你執行機器訪問代碼。

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

如果127.0.0.0不起作用,您可以嘗試使用某些網絡IP地址並查看。

讓我知道如果它仍然不能解決你的問題,我嘗試了你的例子和改變路徑(使它適合我的開發環境)已經修復了我的錯誤。

我很想知道它是否不能解決它。

1

我有同樣的問題。

這個PHP設置解決了我的問題:

allow_url_fopen -> 1 
19

移植到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); 
+1

我想補充的答案,這是由OpenSSL的變化5.6.x http://php.net/manual/en/migration56.openssl.php –

+4

而不是禁用SSL同行動機驗證,您應該嘗試解決導致驗證失敗的問題。例如,通過明確地將路徑設置爲包含必要的根證書來驗證的cafile文件。答案到了問題的核心,有時這可能是唯一可用的解決方案,所以+1。 – deceze

+1

像添加「openssl.cafile = C:/path/to/sslCertificates/cacert.pem」到php.ini :-)(如curl.cainfo!) –

5

將這個代碼中所有SOAP調用:

libxml_disable_entity_loader(false); 
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); 
1

試試這個。對我的作品

$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); 
相關問題