The server then provides the API resource if the token is valid, or responds with a 401 Unauthorized status code if the token is missing, expired or invalid.īefore you begin, you’ll need a free Okta developer account. The server asks Okta for some metadata that allows it to verify tokens and validates the token (alternatively, it can just ask Okta to verify the token) The client sends the access token to the REST API server The client passes these credentials to Okta and obtains an access token The client application holds a Client ID and a Secret The flow is recommended for machine-to-machine authentication when the client is private and works like this: We’ll use Okta as our authorization server and we’ll implement the Client Credentials Flow. Note: when making PUT and POST requests, make sure to set the Body type to raw, then paste the payload in JSON format and set the content type to JSON (application/json). Then connect to 127.0.0.1:8000 with Postman and send http requests. First, go to the project directory and start the PHP server: You can test the API with a tool like Postman. exec ( $statement ) echo "Success! \n " } catch ( \ PDOException $e ) We’ll start by creating a /src directory and a simple composer.json file in the top directory with just one dependency (for now): the DotEnv library which will allow us to keep our Okta authentication details in a. Learn More About PHP, Secure REST APIs, and OAuth 2.0 Client Credentials FlowĬreate the PHP Project Skeleton for Your REST API.Build a Sample Client Application (Command Line Script) to Test the PHP REST API.Add Authentication to Your PHP REST API.
#SIMPLEIMAGE PHP HOW TO#
At the end of the post, I’ll show you how to build a test client application as well. The Client Credentials Flow is best suited for machine-to-machine communication where the client application is private (and can be trusted to hold a secret). There are different authentication flows in OAuth 2.0, depending on if the client application is public or private and if there is a user involved or the communication is machine-to-machine only. Okta is an API service that allows you to create, edit, and securely store user accounts and user account data, and connect them with one or more applications. We’ll make the API secure by using Okta as our authorization provider and implementing the Client Credentials Flow. In this article, I’ll show you how to build a simple REST API in PHP from scratch. You certainly don’t need a complex framework to build a simple but secure API though. They also make it easy to handle common issues like authentication/authorization, request validation, data transformation, pagination, filters, rate throttling, complex endpoints with sub-resources, and API documentation. They provide great tools to process requests and generate JSON responses with the correct HTTP status codes. Laravel/Lumen and Symfony’s API platform are the most often used examples in the PHP ecosystem. There are many great frameworks that can help you build REST APIs quickly. Most web applications these days are developed as single-page applications on the frontend, connected to backend APIs written in various languages.
REST APIs are the backbone of modern web development.