Creating a basic PHP API

5 posts Page 1 of 1
Contributors
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

Creating a basic PHP API
rocky4126
This tutorial will show you how to create a basic API in PHP. This is probably not the best way of making an API, it should at the very least have flood protection on it.

Full Code:
Code: Select all
<?php
	$ret = array();
	if(!$_SERVER['REMOTE_ADDR'] != "127.0.0.1"){
		$ret['error'] = "Not calling from valid IP address";
		$ret['success'] = false;
	}else{
		if(isset($_POST['action']) && $_POST['action'] != ""){
			switch(strtolower($_POST['request'])){
				case "action1":
					if(isset($_POST['input'] && $_POST['input'] != "")){
						$ret['message'] = $_POST['input'];
						$ret['success'] = true;
					}else{
						$ret['error'] = "Missing parameter: input.";
						$ret['success'] = false;
					}
					break;
				case "parseurl":
					if(isset($_POST['input'] && $_POST['input'] != "")){
						$ret['message'] = urlencode($_POST['input']);
						$ret['success'] = true;
					}else{
						$ret['error'] = "Missing parameter: input.";
						$ret['success'] = false;
					}
					break;
				default:
					$ret['error'] = "No action named \"{$_POST['request']}\".";
					$ret['success'] = false;
					break;
			}
		}else{
			$ret['error'] = "Missing parameter: action.";
			$ret['success'] = false;
		}
	}
	echo json_encode($ret);
?>
Now that you know what we're looking at, let me take you through it.
Code: Select all
$ret = array();
Defines our array that we're going to insert values into. The valid values we'll be inserting are: message,error and success. If success = false then error will be used, otherwise, we'll use message and set success to true.
Code: Select all
if(!$_SERVER['REMOTE_ADDR'] != "127.0.0.1"){
This checks that the IP is from your server. This isn't required but adds a bit of security.
Code: Select all
if(isset($_POST['action']) && $_POST['action'] != ""){
Checks that there has been an action defined. If not, it'll return an error for us.
Code: Select all
case "action1":
If the action is equal to Action1 or ACTION1 or action1 or AcTiOn1 (aka, it doesn't matter on the case) then it'll come to this case.
Code: Select all
if(isset($_POST['input'] && $_POST['input'] != "")){
Checks for input just like we checked for an action.
Code: Select all
$ret['message'] = $_POST['input'];
This will pipe what the user sent to us back at them.
Code: Select all
case "parseurl":
	if(isset($_POST['input'] && $_POST['input'] != "")){
		$ret['message'] = urlencode($_POST['input']);
		$ret['success'] = true;
	}else{
		$ret['error'] = "Missing parameter: input.";
		$ret['success'] = false;
	}
	break;
This is a demonstration usage of the API. Someone sends the action parseurl with a non-encoded string as input and it will run urlencode(); on the input and return it.
Image
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Creating a basic PHP API
clanc789
I dont get it. What does it do?

"This is a demonstration usage of the API. Someone sends the action parseurl with a non-encoded string as input and it will run urlencode(); on the input and return it."

I dont get that. Please explain more?

Thank you.
Practice makes perfect!

VIP since: 6-10-2011
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: Creating a basic PHP API
Codex
clanc789 wrote:
I dont get it. What does it do?

"This is a demonstration usage of the API. Someone sends the action parseurl with a non-encoded string as input and it will run urlencode(); on the input and return it."

I dont get that. Please explain more?

Thank you.
Do you know what an API is and what it's used for ?

Well, you could use the website through an API and lets say you wanted for example to post a message, then you would make httpwebrequest in vb.net (can also be done through javascript/php/etc.) with your POST values etc.
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Creating a basic PHP API
clanc789
Codex wrote:
clanc789 wrote:
I dont get it. What does it do?

"This is a demonstration usage of the API. Someone sends the action parseurl with a non-encoded string as input and it will run urlencode(); on the input and return it."

I dont get that. Please explain more?

Thank you.
Do you know what an API is and what it's used for ?

Well, you could use the website through an API and lets say you wanted for example to post a message, then you would make httpwebrequest in vb.net (can also be done through javascript/php/etc.) with your POST values etc.
I know how to use an API and that the function is but i dont get this code (i have NEVER done anything with PHP in my life, thats probaly why :D).
Practice makes perfect!

VIP since: 6-10-2011
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

Re: Creating a basic PHP API
rocky4126
I don't know how to explain it better :O

Also, I might release a better code from an API that I'm coding now (might make it REST compatible)
Image
5 posts Page 1 of 1
Return to “Help & Support”