2014-11-24 141 views
1

我用這個tutorial中的symfony2編寫了一個web服務。與symfony的soap web服務

我的潰敗是:

_soap: 
path:  /soap 
defaults: { _controller: AcmeSoapBundle:Default:index} 

我的配置:

services: 
hello_service: 
    class: Acme\SoapBundle\Services\HelloService 
    arguments: ["@mailer"] 

我的服務:

namespace Acme\SoapBundle\Services; 

class HelloService 
{ 
    private $mailer; 

    public function __construct(\Swift_Mailer $mailer) 
    { 
     $this->mailer = $mailer; 
    } 

    public function hello($name) 
    { 

     $message = \Swift_Message::newInstance() 
      ->setTo('[email protected]') 
      ->setSubject('Hello Service') 
      ->setBody($name . ' says hi!'); 

     $this->mailer->send($message); 

     return 'Hello, '.$name; 
    } 
} 

我的控制器:

namespace Acme\SoapBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 

class DefaultController extends Controller 
{ 
    public function indexAction() 
    { 
     $server = new \SoapServer('hello.wsdl'); 
     $server->setObject($this->get('hello_service')); 

     $response = new Response(); 
     $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1'); 

     ob_start(); 
     $server->handle(); 
     $response->setContent(ob_get_clean()); 

     return $response; 
    } 
} 

hello.wsdl(網絡/ hello.wsdl)

<?xml version="1.0" encoding="ISO-8859-1"?> 
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:tns="urn:arnleadservicewsdl" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns="http://schemas.xmlsoap.org/wsdl/" 
    targetNamespace="urn:helloservicewsdl"> 

    <types> 
     <xsd:schema targetNamespace="urn:hellowsdl"> 
      <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> 
      <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" /> 
     </xsd:schema> 
    </types> 

    <message name="helloRequest"> 
     <part name="name" type="xsd:string" /> 
    </message> 

    <message name="helloResponse"> 
     <part name="return" type="xsd:string" /> 
    </message> 

    <portType name="hellowsdlPortType"> 
     <operation name="hello"> 
      <documentation>Hello World</documentation> 
      <input message="tns:helloRequest"/> 
      <output message="tns:helloResponse"/> 
     </operation> 
    </portType> 

    <binding name="hellowsdlBinding" type="tns:hellowsdlPortType"> 
     <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 
     <operation name="hello"> 
      <soap:operation soapAction="urn:arnleadservicewsdl#hello" style="rpc"/> 

      <input> 
       <soap:body use="encoded" namespace="urn:hellowsdl" 
        encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> 
      </input> 

      <output> 
       <soap:body use="encoded" namespace="urn:hellowsdl" 
        encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> 
      </output> 
     </operation> 
    </binding> 

    <service name="hellowsdl"> 
     <port name="hellowsdlPort" binding="tns:hellowsdlBinding"> 
      <soap:address location="http://example.com/app.php/soap" /> 
     </port> 
    </service> 
</definitions> 

但我看到波紋管錯誤:

XML Parsing Error: no element found 
Location: http://xxx.xxxx.xxx/unproject/web/app_dev.php/soap 
Line Number 1, Column 1: 

我怎樣才能解決這個問題呢? 感謝您的幫助

回答

0

我想你已經複製從該鏈接的WSDL文件的內容,但錯過了改變location部分,你應該改變<soap:address location="http://example.com/app.php/soap" />到您自己的網址,例如,http://xxx.xxxx.xxx/unproject/web/app_dev.php/soap ..

+0

感謝您的回覆,但它無法正常工作。 hamid 2014-11-25 06:34:42

+0

@hamid所以,改變它然後.. – xurshid29 2014-11-25 11:09:42

+0

@ xurchid29:正如我所說,我改變它但它不能正常工作!我怎麼解決這個問題 ? – hamid 2014-11-25 11:33:29

0

編輯這一行你WSDL到的URL在你所暴露SoapServer的soap:address location="http://example.com/app.php/soap"

同時更換:
targetNamespace="urn:helloservicewsdl
到的
targetNamespace="urn:arnleadservicewsdl"

也看到,在您的的php.ini
soap.wsdl_cache_enabled=0 & & soap.wsdl_cache_ttl=0

使這一變化爲我工作。