How to create a REST service with PHP and Mysql and . htaccess

Asked

Viewed 6,765 times

1

I am developing a small project, where some information should be stored in a Mysql database that is hosted in a PHP hosting, I was wondering if there is a way to develop a REST service to insert the data in a Mysql database.

2 answers

4


A simplified way to do this, I recommend, is through Slim Framework, developed in PHP, focusing on the distribution of REST Apis.

Beginning (Source):

1) Curl -s https://getcomposer.org/installer | php

2) Create a file composer.json at the root of the project with the code below:

{
    "require": {
        "slim/slim": "2.*"
    }
}

3) Execute the command php composer.phar install

4) Include in your archive index.php the autoloader of the framework

<?php
require 'vendor/autoload.php';

Creating the "Hello World"

1) Install the application of the framework through the code:

$app = new \Slim\Slim();

2) Create the route:

$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

3) Run the application:

$app->run();

Access your project with the paths specified in $app->get. This is a very basic example of how Slim works, to get a better sense, I recommend reading the documentation.

  • Thanks Luis, I’ll check that option.

1

Zend, the company responsible for PHP and the famous Zend Framework, has developed a web service generator called Apigility.
I believe it is the easiest way and on Youtube there are several videos on the subject.

I particularly like Yii2. In this guide they show how easy it is to create a Restful webservice

  • 1

    Links may be good, but we recommend providing an example, as suggested at http://answall.com/help/how-to-answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.