Pages

Powered by Blogger.

Thursday, January 22, 2015

Web Service dengan PHP NuSoap (Instagram)

Masalah
Bagaimana cara membangun aplikasi web service dengan menggunakan PHP NuSoap sebagai pustaka web service-nya pada berapa kali kata (yang diinputkan) disebutkan pada instagram?

Solusi
Solusi studi kasus akan di bagi menjadi 5 Tahapan, yaitu :
      1.      Merancang Arsitektur Sistem
      2.      Merancang dan Membuat Struktur Database/Tabel
      3.      Membuat Script Web Service Server (WSDL)
      4.      Membuat Script Web Service Client
      5.      Visualisasi dengan Instagram

Tahapan Solusi
      1.      Merancang Arsitektur Sistem

      























      2.      Merancang dan Membuat Stuktur Database/Tabel
      Database Engine : MySQL
      Database Navigator : PhpMyAdmin
      Struktur Database/Table
      Database Name : insta
      Table Name : info_penduduk_propinsi

            Struktur Tabel

Nama Field
Type Data
Length
Id
Integer
11
Nama
Varchar
255
Jumlah
Integer
11
    
            Script

Create database insta;

            Crate table search

CREATE TABLE IF NOT EXISTS `search` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(255) NOT NULL,
  `jumlah` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

            Insert table search

INSERT INTO `search` (`id`, `nama`, `jumlah`) VALUES
                (1, 'halima', 0),
                (2, 'retno', 0),
                (3, 'love', 0),
                     (4, 'fita', 0);


      3.      Membuat Script Web Service Server (WSDL)
      Kebutuhan :
-       AMP (Apache MySQL PHP )
-        Pustaka/Library PHP NuSoap
nusoap.php db_config.php ws_server.php
Script server ( server.php )
<?php

require_once('lib/nusoap.php');
$ns = "http://localhost/a/server.php";
$server = new soap_server;
$server->configureWSDL('Search Instagram_Halima_Retno_Fita', $ns);
$server->wsdl->schemaTargetNamespace = $ns;
$server->register('search', array('nama' => 'xsd:string'),
array('return'=>'xsd:string'), $ns);
function search($nama) {     //$nama hrs sm dgn nama field dtbase
if (!$nama) {
return new soap_fault('Client', '', 'Harus ada nilainya!', '');
}
if ($conn = mysql_connect("localhost", "root", "")) {
if ($db = mysql_select_db("insta")) {
$result = mysql_query("SELECT * FROM search WHERE nama = '$nama'");
$jumxx=mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$nama = $row["nama"];
$jumlah = $row["jumlah"];


}
} 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 "Nama belum terdaftar";
}
}
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])
                ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($POST_DATA);
exit();
?>

Hasil WSDL Web Service Server



ScreenShoot Web Service Server (WSDL)



      4.      Membuat Script Web Service Client
Kebutuhan :
1. AP (Apache PHP)
2. Pustaka/Library PHP NuSoap
Hasil SOAP Envelope Web Service Client ( Response dan Request ke Web Service Server )


       5.      Visualisasi dengan Instagram
Kebutuhan:
1. AP (Apache PHP)
2. Instagram API
3. Koneksi Internet (instagram.com)

Script Form(form.php)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="api.css">
<title>API Instagram</title>
</head>
<body background="api/bg.jpg">
<div align="center">
<div id="template">
     <div id="header"><img src="api/hd.png" height="150" width="950">
     </div>
     <div id="navigasi">
          <table align="center" width="800px">
              <tr align="center">
                   <td width="200px"><a href="index.html">Beranda </a></td>
                   <td width="200px"><a href="form.php">Search Tags </a></td>
              </tr>
          </table>
     </div>
     <div id="kiri">
          <div class="b">
              <br><br>
              <p align="center">Menu Utama
                   <ul>
                        <li><a href="form.php">Search Tags</a></li>
                   </ul>
                  
                   <br>
                   <p align="center">Nama Kelompok :</p>
                   <ul>
                        <li>Halimatus Sya'diyah</li>
                        <li>Retno Wahyu Wulandari</li>
                        <li>Nur Fitriyah Lestari</li>
                   </ul>
              </p> <br>
          </div>
     </div>
     <div id="isi">
                   <h1 align="center">Instagram Tag Search</h1>
                   <form method=POST action=client.php>
                   Search : <input type=text name=nama><br>
                   <input type=submit><br>
                   </form>
     </div>
         
     <div id="clear"></div>
     <div id="footer">
          <p align="center"><blink><b>Copyright@2014 - Halima, Retno, Fita </b></blink></p>
     </div>
     <div id="clear"></div>
</div>
</body>
</html>


ScreenShoot Instagram Web Service Client (form.php)


Script Client(client.php)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="api.css">
<title>API Instagram</title>
</head>
<body background="api/bg.jpg">
<div align="center">
<div id="template">
     <div id="header"><img src="api/hd.png" height="150" width="950">
     </div>
     <div id="navigasi">
          <table align="center" width="800px">
              <tr align="center">
                   <td width="200px"><a href="index.html">Beranda </a></td>
                   <td width="200px"><a href="form.php">Search Tags </a></td>
              </tr>
          </table>
     </div>
     <div id="kiri">
          <div class="b">
              <br><br>
              <p align="center">Menu Utama
                   <ul>
                        <li><a href="form.php">Search Tags</a></li>
                   </ul>
                  
                   <br>
                   <p align="center">Nama Kelompok :</p>
                    <ul>
                        <li>Halimatus Sya'diyah</li>
                        <li>Retno Wahyu Wulandari</li>
                        <li>Nur Fitriyah Lestari</li>
                   </ul>
              </p> <br>
          </div>
     </div>
     <div id="isi">
<?php
require_once('lib/nusoap.php');
?>

<?php
$nama="$_POST[nama]";
$wsdl="http://localhost/a/server.php?wsdl";

$client =new nusoap_client($wsdl,true);
// Call the SOAP method
$param = array('nama'=>$nama);

$result = $client->call('search', $param);
 if($result=="Nama belum terdaftar")
 {
     echo "$result";
 }else{
     echo "<p align='left'>"."ID: $result";
     echo "<br/>";
     echo "Nama : $nama";
     echo "<br/>";
     echo "Jumlah :";
     echo "<br/>";
}

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

?>
<br/>
<?php
if(!empty($_POST['nama'])){
          $json_url = 'https://api.instagram.com/v1/tags/'.$_POST['nama'].'/media/recent?access_token=322328738.467ede5.37be716059df45ceb644aad9bef24f19';
          $json = file_get_contents($json_url);
          $links = json_decode($json);
    
          foreach( $links->data as $pecah ) {
              echo '<a href = "'.$pecah->link.'" target="blank"><img src ="'.$pecah->images->thumbnail->url.'"/></a>';
             
             
         
     }
}
?>
</div>
         
     <div id="clear"></div>
     <div id="footer">
          <p align="center"><blink><b>Copyright@2014 - Halima, Retno, Fita </b></blink></p>
     </div>
     <div id="clear"></div>
</div>
</body>
</html>


ScreenShoot Instagram Web Service Client (client.php)



Selamat mencoba, semoga bermanfaat :)

 

Blogger news

Blogroll

About