Over a million developers have joined DZone.

Tutorial: How to make a simple PHP webservice

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Is this post ill shed some light on how to make a simple PHP webservice. This tutorial is the first in a serie that will show how to get data from your webserver to your Android application. This script will learn you the following:

XML output with php

itzz XML

  • Connect to a database ( MSaccess in this example )
  • Query the database
  • Return XML
  • Explain how to use parameters

I have put alot of comments in the code to make it easy to understand :-)

//convert all the post data to php variables
foreach ($_POST as $key => $value) {
	$$key = addslashes(trim($value));
}
//check if a parameter exists - if not then we don't have to go to the database
if(isset($naam) || isset($plaats)):
	//open a msaccess connection
	$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
	$cstr='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Jet OLEDB:System Database=C:\test.mdw;User ID=username;Password=hardtocrack!;';
	$conn->open($cstr);
	//Quit on error
	if (!$conn){exit("Connection Failed: " . $conn);}
	//to prevent SQL injection
	$n = addslashes($naam);
	$p = addslashes($plaats);
	//build the query
	$sql='SELECT
			*
		FROM
			klanten
		WHERE
			naam Like "%' . $n . '%")
		AND plaats Like "%' . $p . '%");';
	$rs=$conn->execute($sql);
	//return an error in XML format if there is an error
	if (!$rs){	exit("<results status=\"error\"><message>Error in SQL</message></results>");}
	//loop thru the recordset and make up the results in XML format
	$num = 0;
	while (!$rs->EOF){
		  $id = $rs->Fields("id")->value;
		  $c = $rs->Fields("name")->value;
		  $n = $rs->Fields("plaats")->value;
		  $results .= "<result><orgid>$id</orgid><naam>$c</naam><plaats>$n</plaats></result>";
		  $rs->MoveNext();
		  $num++;
	}
	//Wrap the results in a XML parent
	$retstr = "<results status=\"success\" count=\"$num\">";
	$retstr .= $results;
	$retstr .= "</results>";
	$rs->Close();	$rs = null;
	$conn->Close(); $conn = null;
	//return the XML data!
	echo str_replace("&", "en" ,$retstr);
endif;

Pretty straight forward code, inho :-) You can use any database you want ofcourse, i just decided to put up MS Access because a mysql / php is soo standard.

Example output will be:

<results status="success" count="2">
	<result><orgid>1</orgid><naam>Test</naam><plaats>Groningen</plaats></result>
	<result><orgid>2</orgid><naam>p-xr</naam><plaats>Silicon Valley</plaats></result>
</results>

I hope you now are able to make a simple php webservice for yourself. In the next post we will explain how to make a http request from android and how we can secure this with a nice username/password :)

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Published at DZone with permission of Mark Mooibroek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}