Creating a basic PHP API
5 posts
Page 1 of 1
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:
Full Code:
Code: Select all
Now that you know what we're looking at, let me take you through it.
<?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);
?>
Code: Select all
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.$ret = array();
Code: Select all
This checks that the IP is from your server. This isn't required but adds a bit of security.if(!$_SERVER['REMOTE_ADDR'] != "127.0.0.1"){
Code: Select all
Checks that there has been an action defined. If not, it'll return an error for us.if(isset($_POST['action']) && $_POST['action'] != ""){
Code: Select all
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.case "action1":
Code: Select all
Checks for input just like we checked for an action.if(isset($_POST['input'] && $_POST['input'] != "")){
Code: Select all
This will pipe what the user sent to us back at them.$ret['message'] = $_POST['input'];
Code: Select all
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.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;
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.
"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
VIP since: 6-10-2011
clanc789 wrote:I dont get it. What does it do?Do you know what an API is and what it's used for ?
"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.
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
Codex wrote: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 ).clanc789 wrote:I dont get it. What does it do?Do you know what an API is and what it's used for ?
"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.
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.
Practice makes perfect!
VIP since: 6-10-2011
VIP since: 6-10-2011
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)
Also, I might release a better code from an API that I'm coding now (might make it REST compatible)
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023