I will show steps of creating ZF based Web Service in Zend studio as below. Also, I put download link of whole ZS project at the end. We can create this simple demo project as below,
- Create a new ZF project in Zend Studio and name it as jiaWS.
- Add autoloaderNamespaces[] = "Jia_" in application.ini to allow Zend Autoloader load Jia_* classes.
- Creating a action class DummyController under directory /application/controllers.
- creating a function soapAction() in DummyController .
- creating a function wsdlAction() in DummyController.
- Creating a Jia_Hello class with sayHello() function under /library/Jia directory.
- Creating a Jia_DummyException class under /library/Jia directory for Soap fault processing.
Jia_Hello class.
/** * This class contains function which will be used by Web service caller. * All business logics will be implented or called in these functions. * * @author Yiyu Jia * */ class Jia_Hello { /** * This function simply return a String when it is called. * Note : the PHPDoc style comment below is very * important as Zend_Soap_AutoDiscover use it to generate WSDL. * * @return string */ public function sayHello(){ return "hello"; } }
DummyController calss
/** * This class includes two functions. soapAction() is point called by * Web service client. wsdlAction() generates WSDL when it is called. * * @author yiyu * */ class DummyController extends Zend_Controller_Action { /** * SOAP action named as soap. */ public function soapAction() { // disable layouts and renderers $this->getHelper ( 'viewRenderer' )->setNoRender ( true ); // initialize server and set URI $server = new Zend_Soap_Server('http://localhost/jiaWS/public/index.php/dummy/wsdl'); // set SOAP service class $server->setClass ( 'Jia_Hello' ); // register exceptions for generating SOAP faults $server->registerFaultException ( array ('Jia_DummyException' ) ); // handle request $server->handle (); } /** * function to generate WSDL. */ public function wsdlAction() { //You can add Zend_Auth code here if you do not want //everybody can access the WSDL file. // disable layouts and renderers $this->getHelper ( 'viewRenderer' )->setNoRender ( true ); // initilizing zend autodiscover object. $wsdl = new Zend_Soap_AutoDiscover (); // register SOAP service class $wsdl->setClass ( 'Jia_Hello' ); // set a SOAP action URI. here, SOAP action is 'soap' as defined above. $wsdl->setUri ( 'http://localhost/jiaWS/public/index.php/dummy/soap' ); // handle request $wsdl->handle (); } }
DummyClient.php
//This code is not a ZF MVC based //So, we need to load Zend libraries require_once 'Zend/Loader.php'; Zend_Loader::loadClass ( 'Zend_Soap_Client' ); //setting options past into Zend_Soap_Client. $options = array ('location' => 'http://localhost/jiaWS/public/index.php/dummy/soap', 'uri' => 'http://localhost/jiaWS/public/index.php/dummy/wsdl' ); try { //Initilizing client object $client = new Zend_Soap_Client ( null, $options ); //calling the Web service function. $result = $client->sayHello (); print_r ( $result ); } catch ( SoapFault $exp ) { //catching exception and print out. die ( 'ERROR: [' . $exp->faultcode . '] ' . $exp->faultstring ); } catch ( Exception $exp2 ) { die ( 'ERROR: ' . $exp2->getMessage () ); }
Source code as Zend Studio project can be downloaded click here
You can find a simple RESTful Web Service demo on my another post, creating RESTful service with Zend_Rest_Route and Zend_Rest_Controller.
when i run your sample on localhost this error occurs(after a long time):
ReplyDeleteERROR: [HTTP] Error Fetching http headers
but it works on a real host.
My own Web Service i wrote have the same problem.
how can i solve it?
I never see this error. I feel it is your local machine's configuration problem.
ReplyDeletei m getting this error :
ReplyDeleteERROR: [Sender] looks like we got no XML document
I feel that your server side configuration has problem if you use exactly my original code. Did you configure the URI rewrite engine properly?
ReplyDeleteBTW, I do not think PHP SOAP WS is a sophisticated solution for SOAP based WS solution. PHP is probably more suitable for creating RESTful WS. So, you can use Java to create SOAP WS if you have choice.
Here is a tutorial about how to create a Metro (JAX-WS) Web service in Netbeans 6.9.1 .
ReplyDeletehttp://yiyujia.blogspot.com/2011/02/simple-tutorial-about-creating-metro.html
Using Java to create SOAP based Web service if you can.
Hi Thanks for your tutorial , I’ve just downloaded the code and try to run it on my local host but nothing comes up,
ReplyDeletewould you please help me to sort it out
This XML file does not appear to have any style information associated with it. The document tree is shown below.
ReplyDelete