Sunday, December 18, 2005

XML-RPC Web Service with PHP and MySQL

I created a simple web service to query a database on a remote server.

The client sends a postcode to the server script which responds with the corresponding address in Japanese.

The server script looks up the address in a MySQL database and returns the value.

This kind of application would be useful for e-commerce.

When users are completing an online order filling in their address, it is convenient for the system to supply their address automatically, given their post code.

Here’s the code for the client script: postcode.php







XML-RPC: Convert Postcode to Address




XML-RPC Web Service example


This script uses the PEAR PHP extension XML-RPC to generate a remote-procedure call.

A postcode is sent to the remote server and the corresponding Japanese address is returned.




 





if($submit){
require_once './xml/RPC.php'; //This script uses the PEAR XML-RPC package

$input = $postcode;
$params = array(new XML_RPC_Value($input, 'string'));
$msg = new XML_RPC_Message('postcode', $params);

$cli = new XML_RPC_Client('/ajax/server.php', 'ajet.net');
// $cli->setDebug(1);
$resp = $cli->send($msg);

if (!$resp) {
echo 'Communication error: ' . $cli->errstr;
exit;
}

if (!$resp->faultCode()) {
$val = $resp->value();
// echo $input . ' address is ' . $val->scalarval();
$val = $val->scalarval();
$val = explode("|",$val); //convert string to an array
if (!empty($val[3])){ //check if valid data was returned
echo outputTable($val,$input);
}
else{
echo $val[0]; //echo error message
}
} else {
/*
* Display problems that have been gracefully caught and
* reported by the xmlrpc.php script.
*/
echo 'Fault Code: ' . $resp->faultCode() . "\n";
echo 'Fault Reason: ' . $resp->faultString() . "\n";
}
}

function outputTable($v,$i)
{
global $lang_ken;
global $lang_town;
global $lang_village;
global $lang_pc2addy;
global $lang_katakana;
global $lang_kanji;

$table="






























$lang_pc2addy
$i $lang_kanji $lang_katakana
$lang_ken $v[0] $v[3]
$lang_town $v[1] $v[4]
$lang_village $v[2] $v[5]
";

$table .= "
";
$table .= "$lang_kanji: ".$v[0].$v[1].$v[2]."
";
$table .= "$lang_katakana: ".$v[3].$v[4].$v[5];
$table .= "
";
return $table;
}
?>






and here’s the code for the server script: server.php


require_once './xml/Server.php';

//set and connect to the database
$db_hostname = 'localhost';
$db_username = 'username';
$db_password = 'password';
$db_database = 'database';
$db_table = 'tbl_postcode';

$conn = @mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database, $conn);

//set the UTF-8 connection
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

//XML-RPC server function
function getAddress($params) {
$param = $params->getParam(0);

// This error checking syntax was added in Release 1.3.0
if (!XML_RPC_Value::isValue($param)) {
return $param;
}
$val = $param->scalarval();
if($val){ // data was sent
$val = postcode2address($val);
$val = new XML_RPC_Value($val, 'string');
return new XML_RPC_Response($val);
}
else{
$val = "Please enter a postcode."; //no data was sent
$val = new XML_RPC_Value($val, 'string');
return new XML_RPC_Response($val);
}

}

//initialize the server
$server = new XML_RPC_Server(
array(
'postcode' =>
array(
'function' => 'getAddress'
)
)
);

function postcode2address($pc)
{
// returns Japanese address from postcode
$postcode=str_replace("-","",$pc); //remove the hyphen
$sql="select prefecture,town,village,prefecture_kat,town_kat,village_kat from tbl_postcode where postcode='$postcode' limit 1";
$result = mysql_query($sql); //run the query on the database
$record = mysql_num_rows($result);
if($record) //if a record is returned
{
$row = mysql_fetch_assoc($result);
//concatenate the japanese address
$kanji=$row['prefecture']."|".$row['town']."|".$row['village']."|";
$katakana=$row['prefecture_kat']."|".$row['town_kat']."|".$row['village_kat']."|";
return $kanji.$katakana;
//return $row;
}
else { $address="The postcode: '$postcode' returned 0 results."; return $address; }
}

?>

It was a good web service design experience!

Saturday, December 17, 2005

Introduction

This blog shall be a documentation of my Systems Design work.

This page is powered by Blogger. Isn't yours?