Cara membuat webservice menggunakan nusoap php


Cara membuat webservice menggunakan nusoap php

       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

<?
session_start();
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"];

//$_SESSION[id]="$id";
//$_SESSION[namauser]="$username";
}
} 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

Sumber : disini
Cara membuat webservice menggunakan nusoap php Cara membuat webservice menggunakan nusoap php Reviewed by suadmin21 on Tuesday, January 15, 2013 Rating: 5

1 comment:

  1. Get a $200 Bonus at Harrah's Casino in Las Vegas
    The new Harrah's Casino is septcasino one https://aprcasino.com/pluscasino/ of the most well-known Las 우리카지노사이트 Vegas-style https://jancasino.com/review/merit-casino/ casino resorts. It features a full-service spa, a full-service aprcasino spa and

    ReplyDelete

Powered by Blogger.