# Receive an incoming SMS message with PHP and Laravel Now that you know how to [send a message to yourself](/docs/sms/getting-started/php/send-sms), learn how to handle incoming SMS messages. Note: Before you can get started, you need the following already set up: - - [PHP](https://www.php.net/manual/en/install.php) 7.3.0 or later and a familiarity with how to create a new file. - cURL 7.61.0 or later installed. - The latest version of [Laravel](https://laravel.com/docs/5.8/installation) (and [Composer](https://getcomposer.org/)). - [ngrok](https://ngrok.com/download) and a familiarity with how to open a local tunnel. Steps: 1. [Set up your Laravel application](#set-up-your-laravel-application) 2. [Start your server](#start-your-server) 3. [Open a tunnel to your server](#open-a-tunnel-to-your-server) 4. [Set up your callback in the Sinch Build Dashboard](#set-up-your-callback-in-the-sinch-customer-dashboard) 5. [Modify your send file](#modify-your-send-file) ## Set up your Laravel application First, we'll perform a series of setup steps. If you already have Laravel and PHP setup, skip ahead to [open an ngrok tunnel to your server](#open-a-tunnel-to-your-server). 1. Create and then navigate to a new folder where you want to store your project. 2. Open a terminal or command prompt and execute the following command to install Laravel: ```shell composer global require laravel/installer ``` This will install Laravel globally and give you access to the Laravel commands. 3. Run the following command to create a new Laravel application: ```shell laravel new YOUR_app_name ``` This sets up your web server, installs, and configures all of the necessary dependencies. 4. Once your web server is created, change directories into the root application folder: ```shell cd YOUR_app_name ``` Note: Make sure that the `fileinfo` extension and `libcurl` are included/enabled in your PHP installation package. More recent versions automatically include these. ## Start your server To fire up the application, start the server with the following command: ```shell php artisan serve ``` If all goes according to plan, a URL with the port appears: `Starting Laravel development server: http://127.0.0.1:8000`. ## Open a tunnel to your server Since we are testing on our local machine, we need to open a tunnel using [ngrok](https://ngrok.com/docs). 1. In the same root file location, open a separate command prompt or terminal. 2. Run the following command: ```shell ngrok http 8000 ``` An ngrok session should start. 3. Copy the forwarding URL connecting to your localhost port. It looks similar to this: `https://1234-4567-8-90-12.ngrok.io` In the next step, we'll paste the ngrok URL into Sinch Build Dashboard callback function and tack on our endpoint. ## Set up your callback in the Sinch Build Dashboard The callback function, or webhook, is what allows incoming messages to be routed back to you. The SMS API will handle webhooks on the backend. So, all you need to do is setup the URL in the Sinch Build Dashboard for each respective service plan ID that you'll be using. Note: We are using ngrok for the purposes of testing on our local machine, but you can congifure any callback URL in the Sinch Build Dashboard. 1. In the [Sinch Build Dashboard](https://dashboard.sinch.com/sms/api/rest), navigate to SMS, then APIs. 2. Select your service plan ID, then scroll to Callback URL, and click **EDIT** to add your callback URL. 3. Paste in the callback URL that ngrok provided and add `/api/incomingSMS` to the end of the ngrok URL. This specifies that we want to utlize the API route (`/api`) in our Laravel app *and* which endpoint to call. Since we are using the SMS webhook, it is `/incomingSMS`. 4. While we're in the Sinch Build Dashboard, navigate to **Numbers**, then **Your virtual numbers**. Make a note of [your Sinch virtual number ](https://dashboard.sinch.com/numbers/your-numbers)as you'll use it to test for an incoming message. Callback handling complete. Check. ## Modify your send file Now that all the setup steps are complete, we will modify the code that enables receiving SMS messages. This code sets up the route for your web server and contains the logic to respond to incoming callbacks from the Sinch servers through your local tunnel. 1. Open the `api.php` file in your project folder (located in `\routes`). 2. Populate that file with the **api.php** code found on this page. We will not need any of the pre-existing code that is on the `api.php` file. ### Fill in your parameters 1. Modify the code sample with your own parameters as outlined below. | Parameter | Your Value | | --- | --- | | `YOUR_service_plan_id` | The service plan ID found on your [Sinch Build Dashboard](https://dashboard.sinch.com/sms/api/rest). | | `YOUR_API_token` | Your API token found in the [Sinch Build Dashboard](https://dashboard.sinch.com/sms/api/rest) with your corresponding Service Plan ID. | Double check that the region is correct on your base URL. Learn more about [regional options here](/docs/sms/api-reference#base-url). 2. Save the file. 3. Using a mobile phone, send an SMS message to your Sinch virtual number. ### Response You should receive an SMS back on your own device that says, "Would ya look at that? You did it! You sent: ." The ngrok tunnel will also display a `200 OK` status: ```shell HTTP Requests ------------- POST /api/incomingSMS 200 OK ``` And that's that! You've received an incoming SMS message using PHP through Laravel. Didn't work? No worries! It happens. Check the following: - Are you sending the message to [your free Sinch virtual test number](https://dashboard.sinch.com/numbers/your-numbers)? - Have you [verified your own mobile number](https://community.sinch.com/t5/Virtual-Numbers/How-can-I-add-a-verified-number-to-my-account/ta-p/8723) in the Sinch Build Dashboard? - Are the `fileinfo` and `libcurl` extensions included in your `php.ini` files? ## Additional resources Click [here to learn more about the batches endpoint](/docs/sms/api-reference/sms/batches).