Skip to main content

Webserver dengan SOAP


Webservice adalah suatu teknologi yg digunakan agar data kita bisa di baca di semua platform, baik java, vb.net, php dan android. Webservice berbasiskan xml. Karena skrg sudah ada librari untuk membuat webservice dgn php yaitu Nusoap, maka kita hrs mndownloadnya trlbh dahulu.
jgn lp pada setting php.ini soap_dll nya d disable.

disini saya mau kasih contoh simple bikin web service.
1. langkah pertama kita harus mempunyai program di sisi server yang nanti akan menghasilkan kluaran bahasa xml, bisa berupa wsdl maupun soap.

hellowsdl.php

<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello',                // method name
    array('name' => 'xsd:string'),        // input parameters
    array('return' => 'xsd:string'),    // output parameters
    'urn:hellowsdl',                    // namespace
    'urn:hellowsdl#hello',                // soapaction
    'rpc',                                // style
    'encoded',                            // use
    'Says hello to the caller'            // documentation
);
// Define the method as a PHP function
function hello($name) {
        return 'Hellooo, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>


pnjelasan

require_once('lib/nusoap.php'); ---> ini printah untuk manggil nusoap nya
// Create the server instance
$server = new soap_server(); --->ini fungsi untuk mmbuat webservice nya d server
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');   -->fungsi utk mmbuat wsdl



$server->register('hello',                //--> method name
    array('name' => 'xsd:string'),        //--> input parameters(variabel input  ex:nama.password)
    array('return' => 'xsd:string'),    // output parameters(type hasil/result yg d tampilkan webservice)
    'urn:hellowsdl',                    // namespace
    'urn:hellowsdl#hello',                // soapaction
    'rpc',                                // style
    'encoded',                            // use
    'Says hello to the caller'            // documentation
);


function hello($name) {   --->fungsi yg d jalankan ktika webservice d panggil
        return 'Hellooo, ' . $name;
}



lalu untuk mengaksesnya kita buat prgram client..

client.php

<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance

$wsdl="http://127.0.0.1/soa/soa/login_webserv/hellowsdl.php?wsdl";
true);
$client =new nusoap_client($wsdl,true);
// Check for an error
$err = $client->getError();
if ($err) {
    // Display the error
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    // At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
    echo '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h2>Error</h2><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h2>Result</h2><pre>';
        print_r($result);
    echo '</pre>';
    }
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

 localhost pd komp local d tulis 127.0.0.1 agar webservice dpt d panggil
$wsdl="http://127.0.0.1/soa/soa/login_webserv/hellowsdl.php?wsdl";--->ini adalah lokasi webservice


skrg kita coba bkin webservice pk database

server.php

<?

require_once('lib/nusoap.php');
$ns = "http://localhost/";
$server = new soap_server;
$server->configureWSDL('login', $ns);
$server->wsdl->schemaTargetNamespace = $ns;
$server->register('login', array('username' => 'xsd:string'),
array('return'=>'xsd:string'), $ns);
$server->register('login', array('password' => 'xsd:string'),
array('return'=>'xsd:string'), $ns);
function login($username,$password) {     //$username,password hrs sm dgn nama field dtbase
if (!$username) {
return new soap_fault('Client', '', 'Harus ada nilainya!', '');
}
if ($conn = mysql_connect("localhost", "root", "")) {
if ($db = mysql_select_db("soa")) {
$passx=md5($password);
$result = mysql_query("SELECT * FROM customer WHERE
username = '$username' and password='$passx'");
$jumxx=mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$id = $row["id_customer"];
$username = $row["username"];
$nama = $row["nama"];
$alamat = $row["alamat"];
$email = $row["email"];
$telp = $row["telp"];


}
} else {
return new soap_fault('Database Server', '', 'Koneksi ke
database gagal!', '');
}
} else {
return new soap_fault('Database Server', '', 'Koneksi ke database
gagal!', '');
}

if($jumxx>0){

return "$id";


}else{

return "Login Salah";
}
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>




client.php

<?
require_once('lib/nusoap.php');

?>

<?php
$usernamex="$_POST[username]";
$passwordx="$_POST[password]";
$wsdl="http://127.0.0.1/soa/soa/login_webserv/server.php?wsdl";
//$client = new soapclient('http://localhost/web_service/baru/webs/nusoapprogwsdl/hellowsdl.php?wsdl', true);
$client =new nusoap_client($wsdl,true);
// Call the SOAP method
$param = array('username'=>$usernamex,'password'=>$passwordx);
//$result = $client->call('login', array('username' => $usernamex));
$result = $client->call('login', $param);
 if($result=="Login Salah")
 {
     echo "$result";
 }else{



echo "id: $result";

echo "login sukses";

}
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

?>


database nya:

-
-- Table structure for table `customer`
--

CREATE TABLE `customer` (
  `id_customer` int(5) NOT NULL auto_increment,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `nama` varchar(255) NOT NULL,
  `umur` int(3) NOT NULL,
  `alamat` text NOT NULL,
  `telp` varchar(15) NOT NULL,
  `email` varchar(50) NOT NULL,
  `tgl_lahir` varchar(15) NOT NULL,
  `kode_pos` varchar(10) NOT NULL,
  `nama_ibu_k` varchar(50) NOT NULL,
  `gender` varchar(15) NOT NULL,
  PRIMARY KEY  (`id_customer`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`id_customer`, `username`, `password`, `nama`, `umur`, `alamat`, `telp`, `email`, `tgl_lahir`, `kode_pos`, `nama_ibu_k`, `gender`) VALUES
(1, 'cust1', '91ec1f9324753048c0096d036a694f86', 'Customer 1', 25, 'Muara Bahari', '09289301', 'test@gmail.com', '', '', '', ''),
(3, 'sandi', '827ccb0eea8a706c4c34a16891f84e7b', 'ariessandi x', 29, 'jakarta', '987899', 'sandi@yahoo.com', '', '', '', ''),
(4, 'aries', '827ccb0eea8a706c4c34a16891f84e7b', 'aries s', 25, 'jakarta', '987980709', 'ss@ymail.com', '', '', '', '')

==============
login pk:
username: cust1
pass:customer

username:sandi
pass:12345


silahkan di coba, semoga mmbantu


UPDATE: Berhubung sekarang versi php udah 5.3 ke atas, berikut perubahannya:

buat yg masih error coba d update sedikit:
buat folder webservice di htdocs:

ex: c:\xampp\htdocs\webservice
buat folder nusoap95-->di isi sm file2 library nusoap yg agan download

c:\xampp\htdocs\webservice\nusoap95\lib  -->tmpat library di copy
buat file server sama letaknya dgn librari di folder nusoap95

c:\xampp\htdocs\webservice\nusoap95\server.php  -->ini tmpat buat service wsdl

codingnya:

server.php

<?
require_once('lib/nusoap.php');
$ns = "http://127.0.0.1/";
$server = new soap_server;
$server->configureWSDL('login', $ns);  //nm wsdl nya login
$server->wsdl->schemaTargetNamespace = $ns;
$server->register('login', array('username' => 'xsd:string'),       //service yg di sediakan login
array('return'=>'xsd:string'), $ns);
$server->register('login', array('password' => 'xsd:string'),
array('return'=>'xsd:string'), $ns);
function login($username,$password) {     //$username,$password hrs sm dgn nama field dtbase
if (!$username) {
return new soap_fault('Client', '', 'Harus ada nilainya!', '');
}
if ($conn = mysql_connect("127.0.0.1", "root", "")) {
if ($db = mysql_select_db("soa")) {
$passx=md5($password);
$result = mysql_query("SELECT * FROM customer WHERE
username = '$username' and password='$passx'");
$jumxx=mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$id = $row["id_customer"];
$username = $row["username"];
$nama = $row["nama"];
$alamat = $row["alamat"];
$email = $row["email"];
$telp = $row["telp"];


}
} else {
return new soap_fault('Database Server', '', 'Koneksi ke
database gagal!', '');
}
} else {
return new soap_fault('Database Server', '', 'Koneksi ke database
gagal!', '');
}

if($jumxx>0){

$dataz=array($id,$username,$nama,$alamat,$email,$telp);
return "$dataz[0] - $dataz[1] - $dataz[2]";


}else{

return "Login Salah";
}
}
if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);

exit();
?>


untuk yg http_raw_post_data nya error di tambahin aja-->
if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);


di coding baru (di atas tulisan ini)udah ane bnerin..error ini d krnakan agan pake php 5.3 ke atas...

JANGAN LUPA D D BROWSER-->  http://127.0.0.1/webservice/nusoap95/server.php?wsdl   ---->di tes jalan ga d browser, coz kalo ga jalan atau error brarti wsdl nya error




nah program client nya di taro di folder:
htdocs/webservice/client.php
client.php


<?
require_once('nusoap95/lib/nusoap.php');

?>

<?php
$usernamex="$_POST[username]";
$passwordx="$_POST[password]";
$wsdl="http://127.0.0.1/webservice/nusoap95/server.php?wsdl"; 
$client =new nusoap_client($wsdl,true);


$err = $client->getError();
if ($err) {
    // Display the error
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
    // At this point, you know the call that follows will fail
}else{

// Call the SOAP method
$param = array('username'=>$usernamex,'password'=>$passwordx);
//$result = $client->call('login', array('username' => $usernamex));
$result = $client->call('login', $param);

}

echo "$result <br>";
 if($result=="Login Salah")
 {
     echo "$result";
 }else{



echo "id: $result <br>";

echo "login sukses";

}
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

?>



buat jg program interface buat login nya:

from.php

<form method=POST action=client.php>
username<input type=text name=username><br>
password<input type=text name=password><br>
<input type=submit><br>
</form>

Buat yang mau coba jalanin di java coba install prgram soap sonar..
trs masukin alamat wsdl yang tlah kita buat

alamat wsdl yg kita buat: http://127.0.0.1/soa/soa/login_webserv/hellowsdl.php?wsdl

kalo jalan brarti dah bner...

nah smoga skrg dah ga ada yg error lg...

slamat mncoba

Comments

Popular posts from this blog

FPDF dengan CodeIgniter

Cetak Surat Keputusan Controller: <?php //File in controller named surat_keputusan.php defined('BASEPATH') OR exit('No direct script access allowed'); class Cetak_surat_keputusan extends CI_Controller { public function __construct()     {         parent::__construct();         $this->load->helper('url');         $this->load->database();                $this->db->select();         $this->db->from('surat.config_sk');                $query = $this->db->get();                 return $query->result();             } public function index() {      ...

Preview PDF di Modal Bootstrap dengan Ajax

//preview pdf in line function preview_surat_keputusan(no) { $('#form')[0].reset(); $('.form-group').removeClass('has-error'); $('.help-block').empty(); $.ajax({ url : "<?php echo site_url('surat_keputusan/ajax_preview/')?>/" + no, type: "GET", dataType: "JSON",     success: function(data)     {     //paramater yang akan ditampilkan di modal         $('[name="no"]').val(data.no);         $('[name="ns"]').val(data.ns);         $('[name="thts"]').val(data.thts);         $('[name="pdf"]').val("http://localhost/dosdm/document/sm/pdf/"+data.thts+"-SK-"+data.ns+".pdf");         var link_base =  "http://localhost/dosdm/document/sm/pdf/"+data.thts+"-SK-"+data.ns+".pdf" ;             $('#pdf_view').attr('src', link_base);      ...

Hack File .xlsb

For first you must create a backup copy of your Workbook!!! Then you have to rename the XLSB file with ZIP extension. Test.XLSB => Test.ZIP             Opening your ZIP file using a compression software (e.g. WinRar) I can see the content of the file, structured in folders Inside the folder xl you can find a binary file named vbaProject.bin. Extract it on your desktop and edit it using a text editor. In my case I used Notepad++. Using the Find function of your editor, you must search the text DPB And replace the DPB string with DPx Then save the vbaProject.bin and replace this file inside the .ZIP File, renaming then .ZIP file in XLSB. Reopening the XLSB file using Excel, you will get an error message: you have to answer Yes to this error message. Then  Save , Close and Reopen your XLSB file. Now, if you go to VBA Editor (ALT + F11), you ca...