Quickstarters
Feature Overview
How to Build a Backend for PHP?
40 min
introduction in this tutorial, you’ll learn how to build a backend for php using back4app to power a dynamic web application on the server side we will focus on integrating key back4app services, such as database management, cloud code functions, rest and graphql apis, user authentication, and real time queries by following these steps, you’ll create a secure, scalable, and robust backend that you can use in your php projects we’ll also look at why back4app accelerates backend development compared to building everything from scratch, so you can save time and effort you’ll discover how to implement advanced security, schedule tasks with cloud jobs, and connect external integrations with webhooks, all while relying on back4app’s infrastructure by the end, you’ll be ready to grow this basic server side backend into a complete production setup, or integrate third party apis and custom logic for real world use prerequisites to complete this tutorial, you will need a back4app account and a new back4app project getting started with back4app https //www back4app com/docs/get started/new parse app if you do not have an account, create one for free and follow the guide to prepare your new project basic php development environment you can set this up locally with php installed or use a hosting environment that supports php official php documentation https //www php net/docs php curl support or a way to send http requests most php installations include curl by default installing curl (official docs) https //curl se/docs/install html familiarity with web application fundamentals understanding http, server side scripting, and basic database concepts will be helpful ensure you have these prerequisites in place before diving into the steps below having your back4app project set up and a php environment ready will make it easier to follow along step 1 – setting up back4app project create a new project the first step in building your server side backend on back4app is creating a new project if you have not already done so, follow these steps log in to your back4app account click the “new app” button in your back4app dashboard give your app a name (for example, “php backend tutorial”) once created, your new back4app project appears in the dashboard this project will be the foundation for the upcoming configuration steps connect to back4app via rest api since we are working with php without a parse specific sdk, we will rely on the back4app rest and graphql apis for communication you will need the following credentials from your back4app dashboard application id rest api key parse server url (usually https //parseapi back4app com ) you can find these by navigating to your app’s app settings or security & keys section in php, you can perform http requests using curl or other libraries below is a short example of sending a post request to back4app $url = 'https //parseapi back4app com/classes/todo'; $data = array( 'title' => 'buy groceries', 'iscompleted' => false ); $payload = json encode($data); $ch = curl init($url); curl setopt($ch, curlopt post, 1); curl setopt($ch, curlopt postfields, $payload); curl setopt($ch, curlopt httpheader, array( 'x parse application id your application id', 'x parse rest api key your rest api key', 'content type application/json' )); curl setopt($ch, curlopt returntransfer, true); $result = curl exec($ch); curl close($ch); echo $result; // print api response this snippet demonstrates how to connect your php application to your back4app backend modify the your application id , your rest api key , and other parameters to match your own keys by doing so, your server side code can read and write data in your new project step 2 – setting up the database creating a data model with your back4app project ready, it’s time to structure your database a data model defines how your web application’s data is organized for example, you might store tasks or blog posts in classes go to the “database” section in your back4app dashboard create a new class (e g , “todo”) with fields such as title (string) and iscompleted (boolean) back4app allows you to create columns for various data types, including string , number , boolean , pointer , relation , file , and others you can also let the schema be created automatically when you first save an object from your php script creating a data model using the ai agent back4app’s ai agent can automate data model creation open the ai agent from the dashboard provide a description like “please create a new todo app at back4app with a complete class schema ” let the ai agent create the database schema for you this feature can save you time and keep your server side app consistent reading and writing data (rest api) to save a new object in your todo class using rest, you can send a post request curl x post \\ h "x parse application id your application id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"title" "buy groceries", "iscompleted" false}' \\ https //parseapi back4app com/classes/todo in php, you would do something similar with curl for instance $url = 'https //parseapi back4app com/classes/todo'; $data = array('title' => 'buy groceries', 'iscompleted' => false); // (same curl setup as before) to query all existing todo items curl x get \\ h "x parse application id your application id" \\ h "x parse rest api key your rest api key" \\ https //parseapi back4app com/classes/todo reading and writing data (graphql api) back4app also supports graphql you can insert or retrieve data by sending graphql queries or mutations to https //parseapi back4app com/graphql insert (mutation) mutation { createtodo(input { fields { title "clean the house" iscompleted false } }) { todo { objectid title iscompleted } } } query query { todos { edges { node { objectid title iscompleted } } } } working with live queries (optional) if you need real time updates in your web application, consider live queries enable live queries in your back4app dashboard, then use a websocket approach from your php environment (or a separate client) to subscribe to changes while more common in javascript clients, you can set up separate sockets in php if needed for details, see the back4app live queries docs https //www back4app com/docs/javascript/live queries step 3 – applying security with acls and clps brief overview acls (access control lists) and clps (class level permissions) protect your data by controlling who can read or write objects this secures your backend from unauthorized access step by step set class level permissions (clps) in the database dashboard you can restrict public read/write or require authentication configure acls on each object if you need fine grained control an acl can specify read/write access for a specific user or role for more details, see app security guidelines https //www back4app com/docs/security/parse security step 4 – writing cloud code functions why cloud code cloud code allows you to run server side javascript for tasks like business logic, triggers, or validations, without setting up your own server this way, you can keep certain code hidden, perform data transformations, and more example function a simple cloud code function that calculates text length // main js parse cloud define('calculatetextlength', async (request) => { const { text } = request params; if (!text) { throw new error('no text provided'); } return { length text length }; }); this function can be invoked from your php script using rest curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"text" "hello back4app"}' \\ https //parseapi back4app com/functions/calculatetextlength deployment you can deploy cloud code via the back4app cli https //www back4app com/docs/local development/parse cli or the back4app dashboard npm modules if you need extra libraries, install them with npm and import them in your cloud code this is helpful for advanced server side integrations step 5 – configuring authentication enable authentication by default, your back4app project has user authentication via the parse user class you can control whether users must verify their email or only log in via username/password sign up, log in, log out (rest) create a user curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"username" "alice","password" "secret123","email" "alice\@example com"}' \\ https //parseapi back4app com/users log in an existing user curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ g \\ \ data urlencode 'username=alice' \\ \ data urlencode 'password=secret123' \\ https //parseapi back4app com/login social login social logins (google, apple, facebook) are possible by configuring oauth flows for instructions, refer to the social login docs https //www back4app com/docs/platform/sign in with apple step 6 – handling file storage setting up file storage you can upload files through rest curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type image/jpeg" \\ \ data binary "@/path/to/myimage jpg" \\ https //parseapi back4app com/files/myimage jpg the json response contains a file url you can store this url in your classes to reference the uploaded file example { "name" "myimage jpg", "url" "https //myapp back4app io/files/myimage jpg" } security considerations you can allow file uploads only from authenticated users or from certain roles configure file upload rules in the server settings step 7 – email verification and password reset overview email verification ensures only valid email addresses are used password resets allow users to recover their accounts securely dashboard configuration in back4app dashboard , go to app settings > email enable email verification and set up your desired templates enable password reset so users can recover accounts implementation once enabled, user sign ups trigger a verification email you can also prompt password resets using curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"email" "alice\@example com"}' \\ https //parseapi back4app com/requestpasswordreset step 8 – scheduling tasks with cloud jobs what cloud jobs do cloud jobs let you automate tasks like removing old data or sending regular emails you can write them in your main js and schedule them on the back4app dashboard example // main js parse cloud job('cleanupoldtodos', async (request) => { const todo = parse object extend('todo'); const query = new parse query(todo); const now = new date(); const thirty days = 30 24 60 60 1000; const cutoff = new date(now thirty days); query lessthan('createdat', cutoff); try { const oldtodos = await query find({ usemasterkey true }); await parse object destroyall(oldtodos, { usemasterkey true }); return `deleted ${oldtodos length} old todos `; } catch (err) { throw new error('error during cleanup ' + err message); } }); deploy this code to back4app schedule the job in the dashboard under app settings > server settings > background jobs step 9 – integrating webhooks definition webhooks let you send http requests to other services when events occur, such as a new record being created in your back4app classes this is helpful for external integrations, including payment platforms, email marketing tools, or slack notifications configuration open the webhooks panel in your back4app dashboard > more > webhooks add a new webhook with the endpoint url where you want to send event data choose the triggers (e g , object creation, update, or deletion) you can also define webhooks in cloud code triggers, making http requests with node js modules like axios step 10 – exploring the back4app admin panel the back4app admin app is a model centric interface that allows non technical users or administrators to manage data without writing code it offers a more intuitive data handling experience than the standard parse dashboard where to find it enable admin app by going to app dashboard > more > admin app click “enable admin app” and configure your admin credentials choose a subdomain to access the admin app log in with your newly created admin credentials to start managing database records, user accounts, roles, and more conclusion in this tutorial, you learned how to build a server side backend for your php web application using back4app you set up a secure database, leveraged real time features, defined custom logic in cloud code, and explored user authentication, file storage, and scheduled jobs you also saw how webhooks and the admin panel can integrate with external services and simplify data management with this foundation in place, you have a flexible and scalable backend that you can extend as needed for your next php project continue exploring advanced techniques, custom roles, or third party apis to make your application even more powerful and dynamic next steps refine your production ready php application by adding caching, load balancing, or advanced security add role based access control or unique authentication flows, if your user base requires it consult official back4app docs to learn more about performance tuning, logs, and analytics try additional tutorials and use the techniques learned here to build real world solutions, from e commerce to social platforms