Cara menggunakan soap:envelope php

SoapUI comes with support for testing WSDL / SOAP based services. For enhanced functionality, try ReadyAPI for free.

  • Easy import of WSDLs and default request generation allows for ad-hoc testing and exploring of services
  • Support for commonly used standards like WS-Security, WS-Addressing, WS-ReliableMessaging, MTOM, etc allow for testing of advanced services and scenarios
  • Integrated WS-I compatibility testing tools allow you to validate both your contracts and messages for industry-standard compliance
  • The SOAP Request TestStep allows for extensive functional testing and validation of services through a variety of assertion and scripting possibilities
  • Load testing of SOAP/WSDL Services is supported as a natural extension to SoapUI functional tests
  • Service Simulations (“MockServices”) can be instantly created from your WSDL and run inside SoapUI for simulating both simple and complex client behavior
  • A graphical front end for generating code with the most popular web service development frameworks is provided, allowing you to easily compared frameworks and their artifacts
  • All functional tests, load tests and MockServices can easily be run both from inside SoapUI and via included command-line tools.
  • WSDL Coverage functionality gives you a unique insight into the coverage of your tests in relationship to the tested contracts; have you tested all elements? Attributes? Etc...
  • WSDL Refactoring allows you to automatically update your tests and simulations to be compliant with new versions of your WSDLs
  • Advanced editors and wizards in ReadyAPI make testing and exploring of services easy for non-technical users and testers.

Getting Started

Getting started with some ad-hoc testing of a SOAP service is straight forward; select the “New Project” option from the File menu, which will prompt the following dialog:

Paste the WSDL path http://www.dneonline.com/calculator.asmx?wsdl into the Initial WSDL/WADL field (the Project Name will be extracted from this) and press OK. SoapUI will work a bit and create the project with the imported WSDL available in the navigator. Go straight to the first “Request 1” request generated for the Add operation and double-click it, which opens the following window:

Now all you have to do is enter two integer value and press the green arrow on the top left to submit the request to the target service, which will return a nice response for you:

Cara menggunakan soap:envelope php

If you are using the pro version of SoapUI, or have a general dislike for XML syntax, you can use the Form view instead for the request and Overview for the response:

Cara menggunakan soap:envelope php

That’s it, you’ve done your first Ad-Hoc test of a SOAP Web Service, now dive into the details to get to grips with all the possibilities!

Beberapa waktu yang lalu pembahasan mengenai web service pernah saya tulis di blog tutorial ini. Dalam artikel tersebut dicontohkan secara sederhana bagaimana cara kerja web service yang digunakan untuk integrasi (pertukaran informasi) antara beberapa buah sistem. Akan tetapi dalam penerapannya, web service sendiri sudah ada protokol standard yang biasa digunakan yaitu SOAP (Simple Object Access Protocol) atau WSDL (Web Service Definition Language). SOAP dan WSDL, oleh wikipedia dijelaskan bahwa keduanya sama-sama menggunakan XML sebagai format pertukaran informasi/komunikasi antar sistem melalui HTTP atau SMTP.

Adapun struktur XML dalam SOAP selama proses transaksi antar sistem baik dalam proses call/request maupun response dalam web service, digambarkan pada gambar berikut ini:

Cara menggunakan soap:envelope php

Untuk mengimplementasikan web service dengan menggunakan SOAP saat ini adalah mudah, karena kita bisa menggunakan NuSOAP. NuSOAP, yang dibuat oleh NuSphere dan Dietrich Ayala ini, merupakan kumpulan class yang khusus digunakan untuk mengimplementasikan web server baik menggunakan protokol SOAP maupun WSDL. Namun dalam pembahasan kali ini sementara hanya akan dibahas khusus implementasi SOAP nya saja. Anda bisa mengunduh NuSOAP ini di sourceforge. Setelah Anda unduh, dan diekstrak filenya, Anda akan mendapatkan sebuah folder bernama /lib. Di dalam folder /lib inilah kumpulan-kumpulan class ini tersimpan dan kita tinggal memanfaatkannya saja.

OK, dalam artikel ini saya akan contohkan implementasi SOAP dengan NuSOAP pada studi kasus sederhana yaitu untuk operasi aritmatika. Adapun gambaran contohnya sebagai berikut. Di komputer A, dalam hal ini bertindak sebagai client akan dibuat sebuah script untuk menjumlahkan dua bilangan. Namun untuk mendapatkan hasil penjumlahannya, komputer A ini akan meminta bantuan komputer B. Dengan demikian komputer A ini nantinya akan melakukan call/request ke komputer B untuk menjumlahkan 2 bilangan, lalu komputer B yang bertindak sebagai server ini menghasilkan response berupa hasil penjumlahannya. Hasil response ini kemudian dibaca oleh komputer A, lalu ditampilkan hasilnya di komputer A.

Bagaimana cara mengimplementasikan kasus di atas? Ini dia caranya. Pertama, pastikan Anda sudah mendownload NuSOAP nya untuk mendapatkan folder /lib nya. Selanjutnya folder /lib ini sama-sama diletakkan di komputer A maupun komputer B. Nah.. selanjutnya, kita siapkan script PHP di komputer B yang nantinya berfungsi untuk menangkap call dari komputer A dan selanjutnya memberikan response. Ini dia scriptnya:

server.php

<?php

// mengincludekan file berisi class nusoap
require_once('lib/nusoap.php');
// instansiasi class soap untuk server
$server = new soap_server;
// meregistrasi 'method' untuk proses penjumlahan dengan nama 'jumlahkan'
$server->register('jumlahkan');

// detil isi method 'jumlahkan'
function jumlahkan($x, $y) {
    return $x + $y;
}

// memberikan response service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

Keterangan:
Setiap method yang dibuat, nama method harus diregisterkan dengan perintah

$server->register('nama method');

Langkah berikutnya, kita tulis script yang nantinya diletakkan di komputer A di sisi client.

client.php

<?php

require_once('lib/nusoap.php');

// dua bilangan yang akan dijumlahkan
$bil1 = 10;
$bil2 = 25;

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client('http://nomor_ip_komputerB/../server.php');
// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array('x' => $bil1, 'y' => $bil2));

echo "<p>Hasil penjumlahan ".$bil1." dan ".$bil2." adalah ".$result."</p>";

?>

Jika script client.php ini dijalankan di komputer client (dalam hal ini komputer A), maka akan menghasilkan output sbb:

Cara menggunakan soap:envelope php

Anda bisa mengimplementasikan NuSOAP ini dengan komputer di rumah (localhost) sebagai client dan server hosting. Jadi Anda upload script server.php nya ke hosting dan juga folder /lib nya. Sedangkan script client.php Anda simpan di localhost. Kemudian pada proses instansiasi

$client = new nusoap_client('http://nomor_ip_komputerB/../server.php');

Anda tinggal tentukan URL nya yang diarahkan ke script server.php di server hosting.

Kemudian bagaimana jika, dalam script server.php nya ingin dibuat lebih dari satu method, ya cukup buat saja beberapa method yang diinginkan dan jangan lupa registerkan. Contoh:

server.php

<?php

// mengincludekan file berisi class nusoap
require_once('lib/nusoap.php');
// instansiasi class soap untuk server
$server = new soap_server;
// meregistrasi 'method' untuk proses penjumlahan dengan nama 'jumlahkan' dan 'kurangi'
$server->register('jumlahkan');
$server->register('kurangi');

// detil isi method 'jumlahkan'
function jumlahkan($x, $y) {
    return $x + $y;
}

// detil isi method 'kurangi'
function kurangi($x, $y) {
    return $x - $y;
}


// memberikan response service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

dan berikut ini contoh script client.php nya.

client.php

<?php

require_once('lib/nusoap.php');

// dua bilangan yang akan dijumlahkan atau dikurangi
$bil1 = 10;
$bil2 = 25;

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client('http://nomor_ip_komputerB/../server.php');
// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array('x' => $bil1, 'y' => $bil2));
echo "<p>Hasil penjumlahan ".$bil1." dan ".$bil2." adalah ".$result."</p>";

// proses call method 'kurangi' di script server.php yang ada di komputer B
$result = $client->call('kurangi', array('x' => $bil1, 'y' => $bil2));
echo "<p>Hasil pengurangan ".$bil1." dan ".$bil2." adalah ".$result."</p>";

?>

Jika Anda penasaran bagaimana format XML yang dikirim ke server pada proses call/request untuk kasus di atas, Anda bisa tambahkan dengan perintah

$client->request;

Contoh:

client.php

<?php

require_once('lib/nusoap.php');

// dua bilangan yang akan dijumlahkan atau dikurangi
$bil1 = 10;
$bil2 = 25;

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client('http://rosihanari.net/nusoap/server.php');
// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array('x' => $bil1, 'y' => $bil2));

// menampilkan format XML dalam proses call/request
echo $client->request;

?>

Setelah Anda jalankan script client.php di atas, kemudian melihat sourcenya di browser maka Anda akan dapatkan format XML SOAP nya sbb:

Cara menggunakan soap:envelope php

Demikian juga apabila Anda ingin melihat format XML response yang dikirimkan oleh server, yaitu dengan menambahkan perintah:

$client->response;

Contoh:

client.php

<?php

require_once('lib/nusoap.php');

// dua bilangan yang akan dijumlahkan atau dikurangi
$bil1 = 10;
$bil2 = 25;

// instansiasi obyek untuk class nusoap client
$client = new nusoap_client('http://rosihanari.net/nusoap/server.php');
// proses call method 'jumlahkan' di script server.php yang ada di komputer B
$result = $client->call('jumlahkan', array('x' => $bil1, 'y' => $bil2));

// menampilkan format XML hasil response
echo $client->response;

?>

dan bentuk format XML response dari server adalah sbb:

Cara menggunakan soap:envelope php

Mudah dan menarik bukan SOAP nya? Insya Allah untuk artikel mendatang akan saya paparkan studi kasus implementasi SOAP dengan NuSOAP yang return value dari method nya berupada data array/multiple data. Biasanya kasus ini diterapkan untuk proses lookup data yang ada di server.