api – PHP Blog http://blog.dev-php.site Snippets and guides Wed, 21 Mar 2018 02:33:10 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.4 Slim 3 PHP Routes Return 404 Page Not Found http://blog.dev-php.site/slim-3-php-routes-return-404-page-not-found/ Wed, 21 Mar 2018 02:33:10 +0000 http://blog.dev-php.site/?p=36 Continue reading Slim 3 PHP Routes Return 404 Page Not Found]]> If your routes give you a 404 error when removing the localhost:8080/index.php/xxx

Ensure that your 000-default.conf or whatever you name it has AllowOverride All


    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/public

    ServerName "dev.local"
    ServerAlias "www.dev.local"

    
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    ErrorLog /var/www/html/logs/error.log
    CustomLog /var/www/html/logs/access.log combined

Ensure that a .htaccess file is in the same folder as the index.php, and the contents are


    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [qsappend,last]

Restart/reload apache apache

sudo service apache2 reload
service apache2 restart
service httpd reload
//For whatever flavor your running
]]>
Slim 3 API Part 1 Hello World http://blog.dev-php.site/slim-3-hello-world-api/ Tue, 13 Mar 2018 23:57:58 +0000 http://blog.dev-php.site/?p=16 Continue reading Slim 3 API Part 1 Hello World]]> Installation

1. Create your api folder
2. Make sure you download the composer.phar file into the folder.
3. Open a shell script, cd to your new api folder and type

composer require slim/slim "^3.0"
//or
php composer.phar require slim/slim "^3.0"

Basic Setup and Routing

Now you should only see your composer.lock, composer.json and a vendor directory. Create index.php and place the following code inside.

get('/', function ($request, $response) {
    return 'hello world';
});

$app->run();

Preview

Go back to the terminal and type

php -S localhost:8000

in the browser visit, http://localhost:8000/ you should see “hello world”

API calls with Slim

GET is used for reading and retrieving data.
POST is used for inserting data.
PUT is used for updating data.
DELETE is used for deleting data.

POST

get('/', function ($request, $response) {
    return 'hello world';
});

 $app->post('/postit', function ($request, $response) {
     $input = $request->getParsedBody();
     return $this->response->withJson($input);
 });

$app->run();

You can then create a form but we will simulate a form post by using curl.
on the command line run

curl -d "param1=value1¶m2=value2" -X POST http://localhost:8000/postit

You should see

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    64  100    37  100    27     45     33 --:--:-- --:--:-- --:--:--    78{"param1":"value1","param2":"value2"}
]]>