I'm a 27 year old software developer that mainly focuses on software development. As everyone i have taken a side in the Apple vs Android game. My choice is Android because of oh so many reasons :-) By contributing to the Android developers content online ( in the form of writing Android tutorials ), I hope to convince others to make the same decision and help them make nice content for the Android platform. Mark is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Tutorial: How to make a simple PHP webservice

04.28.2011
| 31373 views |
  • submit to reddit
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 :)

References
Published at DZone with permission of Mark Mooibroek, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ryan Developer replied on Thu, 2011/04/28 - 5:30pm

Editor: this does not belong on JavaZone

darryl west replied on Fri, 2011/04/29 - 11:12am

Not only does this not belong in JavaZone it's a horrible, but typical M$oft example of bad coding practices.

dev danke replied on Sun, 2012/02/05 - 4:51am

I use PHP because it's open and cross-platform.  Therefore, the last thing I'd want is to use Microsoft platform-specific code to access the DB.  At the very least, I'd use ODBC, so I could use iODBC on *nix systems. 

Also, I'd put my DB access code inside a class, which would be called by pages.  Then, I'd only need to change it in one place when I switched platforms. 

You always need to be wary about Microsoft advice.  Most of their suggestions try to lock you into their platform, and limit your future choices.  In most cases, Microsoft cannot compete technically with their FOSS competitors. To ensure their revenue stream, they rely on lock-in to proprietary products and technology.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.