Welcome to Abdul Malik Ikhsan's Blog

Zend Framework : Create Web Services using Zend_Soap

Posted in Tutorial PHP, Zend Framework by samsonasik on June 16, 2010

Web Services adalah software sistem design yang memungkinkan untuk INTEROPERABILITY aplikasi antara komputer satu dengan lainnya melalui jaringan via HTTP. Dengan web services, aplikasi di satu komputer memanggil method ( method-method ) di aplikasi lain, di komputer yang berbeda, dengan bahasa Pemrograman yang bisa berbeda. Misalkan kita di sisi server membuat web services menggunakan .NET, di sisi client kita tidak perlu mempunyai .NET. Nah, bagaimana komunikasinya ? Komunikasinya melalui sebuah Message Format, biasa disebut SOAP ( Simple Object Access Protocol ).
Web Service mengeluarkan output berformat XML yang disebuat WSDL ( Web Service Description Language).  Nah, file XML ini yang dikonsumsi oleh client dan didapatlah output.
Ok, let’s coding :
Saya akan mensimulasikan secara local. Pertama, kita siapkan class yang akan kita panggil, kita taruh di folder zfprojectnya\library :

<?php

class Soaps
{
 /**
 * Penambahan method
 *
 * @param Int $bilpertama
 * @param Int $bilkedua
 * @return Int
 */
 public function penambahan_data($bilpertama,$bilkedua)
 {
 $return = $bilpertama + $bilkedua;
 return  $return ;
 }

 /**
 * Pengurangan method
 *
 * @param Int $bilpertama
 * @param Int $bilkedua
 * @return Int
 */
 public function pengurangan_data($bilpertama,$bilkedua)
 {
 return $bilpertama - $bilkedua;
 }
}

Setelah itu, kita siapkan Controller kita :

<?php
//include file needed
require_once realpath(APPLICATION_PATH . '/../library/').'/Soaps.php';

class SoapwordpressController extends Zend_Controller_Action
{
    private $_WSDL_URI="http://192.168.1.3:10000/zftutwordpress/public/soapwordpress?wsdl";

    public function indexAction()
    {
        $this->getHelper('viewRenderer')->setNoRender();
        if(isset($_GET['wsdl'])) {
            //mengembalikan wsdl...
            $this->hadlereturnWSDL();
        } else {
            //menghandle SOAP request...
            $this->handleActualSOAP();
        }
        die;
    }

    private function hadlereturnWSDL() {
        $autodiscover = new Zend_Soap_AutoDiscover();
        $autodiscover->setClass('Soaps');
        $autodiscover->handle();
    }

    private function handleActualSOAP()
    {
        $soap = new Zend_Soap_Server($this->_WSDL_URI);
        $soap->setClass('Soaps');
        $soap->handle();
    }

    public function clientAction() {

        $client = new Zend_Soap_Client($this->_WSDL_URI);
        //isi variable...
        $tambah =  $client->penambahan_data(45,7);

        //coba tampilkan...
        echo $tambah;

        die;
    }
}

Ok, let’s check clientAction response 🙂
kalau berhasil, maka akan menampilkan angka 52 , karena kita memanggil function penambahan_data dengan input parameter 45 dan 7.

Referensi :
http://blog.ekini.net/2009/05/21/zend-framework-zend_soap-create-a-web-service/
http://makvin-it.com/
http://framework.zend.com

5 Responses

Subscribe to comments with RSS.

  1. Riki Risnandar said, on September 2, 2010 at 6:15 pm

    terima kasih atas tutorial web service nya di zend framework
    btw kok nama classnya ada wordpress-nya, lg bikin koneksi antara zf dengan wordpress?

  2. samsonasik said, on September 3, 2010 at 3:22 am

    hohoho :D, cuma ngelompokin project/file2 yg buat tutorial di wp aja 😀

  3. João Paulo said, on August 31, 2013 at 9:14 am

    samsonasik hello, I’m needing to make communication soap client and server, most within the same application, but in different modules, may seem strange more is needed, I followed your tutorial among thousands of other tutorials on the web more unfortunately not worked out, the more it seems to me that many people used as their example, then u can give me some tips how to communicate this web service in the same application but in different modules. Only one thing I’m Brazilian and do not speak English then I’m using a tool to translate text then it gets weird the translation u already know, already agredeço. The error that occurs is as follows (SOAP-ERROR: Parsing WSDL: Could not load from ‘http://127.0.0.1:8000/application/index?wsdl’: failed to load external entity “http://127.0. 0.1:8000 / application / index? wsdl “) has got to the point to debug the code to get in pure PHP without using soap client and the server ZF2


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: