Jump to content

PHP simple way to create REST API

Joveice
Go to solution Solved by Hazy125,

The key is in your htaccess file. You probably have one auto generated by your server in your root directory. If you don't see it there, make sure you are searching for hidden files. And if its definitely not there, it is easy enough to create. Just create a file named ".htaccess"

 

In your htaccess file add the following text

RewriteRule ^api/ api.php

This sends the requests to the api directory to the api.php file where you can grab the URL and do all the PHP'y things

Hi, I'm trying to create a simple rest api for my personal use (just so I can interact with my server and add functions over time) all I want is to be able to use the url methods of doing this, I don't know OOP PHP, so I would like to stay away from any framework until I know that.

 

so I want to be able to interact with the api with mydomain.com/api/v1/{token}/stuff/{input}

 

how is this done? I know how to use POST but I don't know if this stuff are alike or what nor how to configure the nginx / apache to work with that type of url.

 

All help is appreciated.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

The key is in your htaccess file. You probably have one auto generated by your server in your root directory. If you don't see it there, make sure you are searching for hidden files. And if its definitely not there, it is easy enough to create. Just create a file named ".htaccess"

 

In your htaccess file add the following text

RewriteRule ^api/ api.php

This sends the requests to the api directory to the api.php file where you can grab the URL and do all the PHP'y things

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Hazy125 said:

The key is in your htaccess file. You probably have one auto generated by your server in your root directory. If you don't see it there, make sure you are searching for hidden files. And if its definitely not there, it is easy enough to create. Just create a file named ".htaccess"

 

In your htaccess file add the following text


RewriteRule ^api/ api.php

This sends the requests to the api directory to the api.php file where you can grab the URL and do all the PHP'y things

uh, thanks!

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

You have been doing web stuff for a while now i know a few tools that really helped me in a awesome way and im still learning a few of them, i did not know these tools till someone told me so hopefully it adds something to your hobby too.

 

Git with Git Flow

This is a ruling how to properly use git and with the git flow init command it will help you with making a master/develop en prefixes for feature and bugfixes branches.

 

Less/Sass

This will help you keep order in your CSS its writing in a other syntax and in the end you compile it to a minified CSS file with the next tool i recommend Gulp or Grunt.

your file will be smaller and all in one file which helps loading the page faster.

 

Gulp, Grunt or other building systems.

Will help you with things like compile multiple JS,CSS,LESS,SASS,SCSS,HTML and more into one file of like for LESS into CSS and alot of JS files in one JS file.

This will not only speed up you website but it gives also features to live refresh while developing.

 

Bootstrap mixins are also quite nice to use.

Those will help you write faster your CSS and not make ugly html with those tags you need to use for col-md-12 or something, with a bit of research im sure you figure out what im talking about.

 

I might advice you to take a look into something like Laravel or something i know you dont want to touch frameworks but since im new and did not even know about what a MVC was i really got blown away. and wished i always implemented something like a MVC. Laravel might give you some starting ideas for you to research to make your structure alot better.

 

Its always nice to have some knowledge of the software that actually runs your server like Nginx,Apache,MySQL etc. and maybe expand for own learning to make your application run with something like Redis or a caching system like Varnish. Easly run out system with Docker or Vagrant and Chef to make configuring services a heaven.

 

Just a few tips that might help with looking up some fun stuff.

Maybe you use some or found other things while googling for them.

 

Im sure alot of people with alot more experience can add somethings to it,

if they dont agree im sure i will get to hear that very shortly.

 

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

And like said above, consider using a framework link Lumen or Slim. They have both great features for a simple restful api... 

I would recommend, to use composer (I think you're not familiar with it, it's a Dependency Manager for PHP, really Important), so learn it.

Because you said, you don't have any OOP knowledge, use Slim. It's really straightforward and easy to use i would say. 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Cruorzy said:

You have been doing web stuff for a while now i know a few tools that really helped me in a awesome way and im still learning a few of them, i did not know these tools till someone told me so hopefully it adds something to your hobby too.

 

Git with Git Flow

This is a ruling how to properly use git and with the git flow init command it will help you with making a master/develop en prefixes for feature and bugfixes branches.

 

Less/Sass

This will help you keep order in your CSS its writing in a other syntax and in the end you compile it to a minified CSS file with the next tool i recommend Gulp or Grunt.

your file will be smaller and all in one file which helps loading the page faster.

 

Gulp, Grunt or other building systems.

Will help you with things like compile multiple JS,CSS,LESS,SASS,SCSS,HTML and more into one file of like for LESS into CSS and alot of JS files in one JS file.

This will not only speed up you website but it gives also features to live refresh while developing.

 

Bootstrap mixins are also quite nice to use.

Those will help you write faster your CSS and not make ugly html with those tags you need to use for col-md-12 or something, with a bit of research im sure you figure out what im talking about.

 

I might advice you to take a look into something like Laravel or something i know you dont want to touch frameworks but since im new and did not even know about what a MVC was i really got blown away. and wished i always implemented something like a MVC. Laravel might give you some starting ideas for you to research to make your structure alot better.

 

Its always nice to have some knowledge of the software that actually runs your server like Nginx,Apache,MySQL etc. and maybe expand for own learning to make your application run with something like Redis or a caching system like Varnish. Easly run out system with Docker or Vagrant and Chef to make configuring services a heaven.

 

Just a few tips that might help with looking up some fun stuff.

Maybe you use some or found other things while googling for them.

 

Im sure alot of people with alot more experience can add somethings to it,

if they dont agree im sure i will get to hear that very shortly.

 

Thanks for all the advice! I appreciate it alot.

 

14 minutes ago, leodaniel said:

And like said above, consider using a framework link Lumen or Slim. They have both great features for a simple restful api... 

I would recommend, to use composer (I think you're not familiar with it, it's a Dependency Manager for PHP, really Important), so learn it.

Because you said, you don't have any OOP knowledge, use Slim. It's really straightforward and easy to use i would say. 

Yes, I have been looking at Laravel but I was not looking to go into it before I knew more about PHP. And thats correct I have no clue of the composer but I have seen it everywhere when looking at tutorials.

Also thanks for the advice!

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Hazy125 said:

The key is in your htaccess file. You probably have one auto generated by your server in your root directory. If you don't see it there, make sure you are searching for hidden files. And if its definitely not there, it is easy enough to create. Just create a file named ".htaccess"

 

In your htaccess file add the following text


RewriteRule ^api/ api.php

This sends the requests to the api directory to the api.php file where you can grab the URL and do all the PHP'y things

Is this correct?

 

My folder structure is

Root

       --api

              --v1

                    --index.php

       --.htaccess

RewriteRule ^api/v1/ /api/v1/index.php

EDIT:

Yes this seems to be working!

Thanks everyone for all the advices and how I could do this, I can now move further with my project!

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Cruorzy said:

I might advice you to take a look into something like Laravel or something i know you dont want to touch frameworks but since im new and did not even know about what a MVC was i really got blown away. and wished i always implemented something like a MVC. Laravel might give you some starting ideas for you to research to make your structure alot better.

i have all the projects I have ever made, I suggest doing this, what you do is keep them and never look then in a year or so have a look and you can clearly see how fsr you've come along. 

 

I problem I have with laravel is whether you need a frame work for any given website. I wouldn't use it for a simple blog style website for example. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, vorticalbox said:

I problem I have with laravel is whether you need a frame work for any given website. I wouldn't use it for a simple blog style website for example. 

Absolutely true, but but you pick your project or work-out your idea and then choose what tools you going to use. Like you wouldnt use a Wordpress site for a social media kind of website.

 

So far i think Laravel is awesome for projects that require safety above everything, structure that can be understood fast. and from a small to medium project probably.

I might be wrong but wouldnt it be awesome if you already knew the hottest framework out there for PHP (Laravel) and the people you are going to work with also know Laravel. Really speed some things up :D

 

And i wish i was good in keeping my old projects but im the kind of guy that deletes everything always once im done, i Might take your advice and just drop everything on a bitbucket or something :)

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Cruorzy said:

Absolutely true, but but you pick your project or work-out your idea and then choose what tools you going to use. Like you wouldnt use a Wordpress site for a social media kind of website.

 

So far i think Laravel is awesome for projects that require safety above everything, structure that can be understood fast. and from a small to medium project probably.

I might be wrong but wouldnt it be awesome if you already knew the hottest framework out there for PHP (Laravel) and the people you are going to work with also know Laravel. Really speed some things up :D

 

And i wish i was good in keeping my old projects but im the kind of guy that deletes everything always once im done, i Might take your advice and just drop everything on a bitbucket or something :)

i have used a little laravel not much, security isn't what I would consider its best feature its a time saver, being a framework. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Okey, so the other way worked fine, but It took time as I knew and everyone said without a framework. So I started to look at this

https://docs.phalconphp.com/en/3.0.0/reference/tutorial-rest.html#creating-a-model

 

Tho, they do not say which files the examples corespond to. Only in the 2 first ones, and they create like 7 files.

 

Does anyone understand this and could explain?

 

They start by saying this is their files

my-rest-api/
    models/
        Robots.php
    index.php
    .htaccess

 

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

  • 8 months later...
5 hours ago, olidev said:

You can follow this tutorial for properly creating a rest api in PHP: https://www.cloudways.com/blog/custom-dashboard-using-woocommerce-php-rest-api/

This tutorial is basically about creating a custom dashboard using php rest api. You will get the idea of how rest api is created and how it can be used in your project.

I have created an API in laravel :)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×