错误SoapFault异常:[HTTP]禁止

时间:2013-09-06 09:26:12

标签: php soap

请帮我解决这个问题。我只想测试这个示例Web服务。

错误:

  

3 add 4 = SoapFault异常:[HTTP]禁止..... soap-client-2.php:14堆栈跟踪:#0 [内部功能]:SoapClient-> _ doRequest(' _call('getResult',Array)#2 ............ / soap-client-2.php(14):SoapClient-> getResult('add',3, 4)#3 {main}

客户端:

<?php
phpinfo();
    // Désactivation du cache WSDL
    ini_set("soap.wsdl_cache_enabled", "0");  

    $options = array("trace" => true);

  $client = new SoapClient("operation.wsdl", $options); 
  try { 
    $operation = "add";
    $integer1  = 3;
    $integer2  = 4;
    echo "$integer1 $operation $integer2 = ";
    echo $client->getResult($operation, $integer1, $integer2); 
    print "<pre>\n"; 
      print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n"; 
      print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n"; 
      print "</pre>"; 
  } catch (SoapFault $exception) { 
    echo $exception;       
  } 
?>

服务器

<?php
/*
 * Fonction getResult sert à addition ou soustraire 2 entiers et retourne le résultat
 * @param $operation Type de l'opération (add/substract)
 * @param $integer1 Entier 1
 * @param $integer2 Entier 2
 * @return $result Résultat de l'opération
 */
function getResult($operation, $integer1, $integer2) {
    $result = 0;

    if (($operation != "add") && ($operation != "substract")) {
      throw new SoapFault("Server", "Veuillez utiliser une methode d'operation valable (add/substract).");
    } 
    if (!$integer1 || !$integer2) {
      throw new SoapFault("Server", "Veuillez indiquer 2 entiers.");
    } 
    if ($operation == "add") {
      $result = $integer1 + $integer2;
    }
    if ($operation == "substract") {
      $result = $integer1 -$integer2;
    }

    return $result; 
}  

// Désactivation du cache WSDL
ini_set("soap.wsdl_cache_enabled", "0");  


// Catch l'erreur si l'instanciation la classe SoapServer
// échoue, on retourne l'erreur
try { 
  $server = new SoapServer('operation.wsdl');
  // On ajoute la méthode "getResult" que le serveur va gérer
  $server->addFunction("getResult"); 
} catch (Exception $e) {
  echo 'erreur'.$e;
}

// Si l'appel provient d'une requête POST (Web Service)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // On lance le serveur SOAP
  $server->handle();
}
else {
  echo '<strong>This SOAP server can handle following functions : </strong>';
  echo '<ul>';
  foreach($server->getFunctions() as $func) {
    echo '<li>' , $func , '</li>';
  }
  echo '</ul>';
}
?>

WSDL

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='getResult' 
  targetNamespace='http://example.org/getResult' 
  xmlns:tns='http://example.org/getResult' 
  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='getResultRequest'> 
      <part name='operation' type='xsd:string'/>
      <part name='integer1' type='xsd:int'/>
      <part name='integer2' type='xsd:int'/>
    </message> 
    <message name='getResultResponse'> 
      <part name='result' type='xsd:string'/> 
    </message> 

    <portType name='getResultPortType'> 
      <operation name='getResult'> 
        <input message='tns:getResultRequest'/> 
        <output message='tns:getResultResponse'/> 
      </operation> 
    </portType> 

    <binding name='getResultBinding' type='tns:getResultPortType'> 
      <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
      <operation name='getResult'> 
        <soap:operation soapAction='urn:xmethods-delayed-quotes#getResult'/> 
        <input> 
          <soap:body use='encoded' namespace='urn:xmethods-delayed-calcul' 
            encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
        </input> 
        <output> 
          <soap:body use='encoded' namespace='urn:xmethods-delayed-calcul' 
            encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
        </output> 
      </operation> 
    </binding> 

    <service name='getResult'> 
      <port name='getResultPort' binding='getResultBinding'> 
        <soap:address location='http://............../soap-server-2.php'/> 
      </port> 
    </service> 
</definitions>

0 个答案:

没有答案