PHP dan Web Services

PHP.

PHP: Hypertext Preprocessor, yang merupakan open source web scripting language. Diciptakan pada th 1994 dengan syntax yg diturunkan dari C, Java dan Perl. PHP merupakan scripting language yg powerful dan cukup mudah untuk dipelajari.
PHP biasanya digunakan untuk content membuat aplikasi web based, seperti cms, forum, blog, wiki maupun crm. Selain itu PHP juga telah banyak digunakan oleh perusahaan-perusahaan terkemuka, seperti Yahoo, AMD, SIEMENS, FIAT, NORTEL, HP, dll.
Kelebihannya antara lain.
– Digunakan sebagian besar website di dunia
– Dukungan komunitas dan dokumentasi yg banyak
– Code bisa jalan di UNIX, OSX dan Windows
– Backed commercially by Zend
– Sintaks yg cukup mudah

Example.

<?php

//Assign value
$x=123;
$y=221;

//Add x to y;
$sum=$x+$y;

//Print the result
echo $sum;

?>

Membuat Fungsi

<?php
//Add x to y
function sum($x,$y) -->deklarasi fungsi
{
return $x+$y; -->nilai balikan
}

//Print the result
echo sum(123,221); -->pemanggilan fungsi

?>

Membuat Class

<?php
class Calculator{  -->deklarasi class
public function sum($x,$y) -->public method
{
return $x+$y;
}
}

//Instantiate a calculator
$calc= new Calculator;

//Print the result
echo $calc->sum(123,221); -->pemanggilan method

?>

Web Services.

-Interoperable interactions
-Calls are made over a network
-Lots of different standards
-Popular website use them

Requirements.

  • PHP 5.2.2
  • SOAP
  • simplexml
  • DOM
  • JSON


SOAP.

Simple Object Access Protocol. Biasanya merupakan sebuah HTTP POST request. Pemanggilannya di enkapsulasi menggunakan XML, sedangan responsenya adalah XML document. Namun, PHP dapat menangani semuanya.

Support for SOAP 1.1, 1.2 and WSDL 1.1
Handles simple and complex types
Easy implementation
Output is quite bulky

Server implementation.

<?php
class Calculator{
public function sum($x,$y)
{
return $x+$y;
}
}

$options=array(
'uri'=>'http://example.com/exampleWebServer' -->namespace
);

$soapServer= new SoapServer(null, $options); --> start Server

$soapServer->setClass('Calculator'); -->assign class

$soapServer->handle(); -->handle incoming calls

?>


Client implementation.

<?php

$options=array(
'uri'=>'http://example.com/exampleWebService',   -->namespace
'location'=>'http://example.com/'          -->endpoint
);

$soapClient= new SoapClient(null, $options);

echo $soapClient->sum(123,221);      -->display result

?>

Output.

<?xml version="1.0" encoding="UTF-8"?>;
<SOAP-ENV:Envelope
<xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
<xmlns:ns1="http://example.com/exampleWebService"  -->namespace
<xmlns:xsd="http://www.w3.org/2001/XMLSchema"
<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
<SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>
<ns1:sumResponse>
<return xsi:type"xsd:int">344</return> -->result
</ns1:sumResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

REST.

Representational State Transfer. Biasanya sebuah HTTP GET request. Pemanggilan langsung dari GET parameter. Response merupaka XML document. Response dibuat dengan DOM. Interpreter response dengan simplexml.

Interaction needs to be implemented
XML schema must be know beforehand
Complex types not handled natively
Output is usually RSS

Server implementation.

<?php
class Calculator{
public function sum($x,$y)
{
return $x+$y;
}
}

$calc= new Calculator;
$result=$calc->sum($_GET['x'],$_GET['y']);  -->handle incoming call
$dom=new DOMDocument;  -->create XML Document

$root=$dom->createElement('result',null);
$dom->appendChild($root);

$value=$dom->createElement('value',$result);  -->add element
$root->appendChild($value);

echo $dom->saveXML();  -->output

?>

Client implementation.

<?php
$restURL='http://example.com/calculator/sum/';
$x=123;
$y=221;

$restUrl .='?x=' . $x . '&y=' . $y;  -->arguments
$xml=simplexml_load_file($restURL);  -->load XML doc

echo $xml->value;  -->output
?>

Output.

<?xml version="1.0"?>
<result>
<value>344</value>
</result>

JSON.

Javasript Object Notation.

  • REST approach
  • Except response is not XML
  • used on the client side
  • Create response with json_encode()
  • interpret response in javascript
  • Handles simple and complex types
  • Calls can be made with AJAX
  • Can also be used on the server side

Server implementation.

<?php
class Calculator{
public function sum($x,$y)
{
return $x+$y;
}
}

$calc= new Calculator;
$value=$calc->sum($_GET['x'],$_GET['y']);  -->handle incoming call
$result=array('value'=>$value);

echo 'var result=' . json_encode($result);  -->output JSON string

?>

Client implementation.

<html>
<head>
<script type="text/javascript" src="http://example.com/calculator/sum/json/?x=123&amp;y=221">
</script>
</head>

<body>
<script type="text/javascript">
var json=eval(result);
document.write(json.value);
</script>
</body>
</html>

Output.

var result={"value":344}

Resources.

PHP Manual : http://www.php.net
SOAP : http://www.w3.org/TR/soap
REST : http://tinyurl.com/akhc7
JSON : http://www.json.org

2 thoughts on “PHP dan Web Services

    • kalo baca dari codenya sepertinya g pake nusoap.
      setauku, klo kita bikin pake nusoap, harus ada library nusoap yg dipanggil.
      Bener g Rif???

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s