Jump to content

How To Create A REST API


JustinK101

Recommended Posts

I need to create a REST API for an iPhone app. Let's say I have the following class:

 

class Basic {
	//Constructor
	function __construct() { }

	public function set_id($p_id) {
		$sql = "INSERT INTO basic (`id`) VALUES (" . $p_id . ")";
		mysql_query($sql) or die(mysql_error());
	}

	public function get_id($p_id) {
		$sql = "SELECT `id` FROM basic WHERE `id` = " . $p_id;
		$result = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_object($result);
		return($row->id);
	}

	public function delete_id($p_id) {
		$sql = "DELETE FROM basic WHERE `id` = " . $p_id;
		 mysql_query($sql) or die(mysql_error());
	}
}

 

Assuming I have that simple class above, how do I make this a REST API? Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/150991-how-to-create-a-rest-api/
Share on other sites

ohdang888,

 

Sorry if I was too general. I guess I just don't know where to start. I am decent at object oriented php, but not sure the framework and methods I need to create a REST API. Thanks for the assistance, greatly appreciated.

The above was just an example of what I want to do. Basically I need a REST interface, so a 3rd party iPhone developer can preform actions that need to be done.

 

As I understand, REST works like:

 

http://www.mydomain.com/api/get/98988

 

OR

 

http://www.mydomain.com/api/insert/

 

Basically how do I go about creating the PHP code to handle the requests.

 

As I understand, REST works like:

 

http://www.mydomain.com/api/get/98988

 

OR

 

http://www.mydomain.com/api/insert/

 

Those look like urls that are rewritten. Without rewrite those urls wcould look like:

 

www.mydomain.com/api/index.php?method=get&id=98988

 

It doesnt really have to do with anything under the hood. You don't even need to write OO code for this.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.