May 2, 2016 7:40 am

Facebook Messenger API for Pages Using Curl PHP

In this article I’ll explain how to use Facebook Messenger with your business page using PHP, curl Messenger for business pages makes it easy to offer instant one-on-one customer service, it boost to your business.

Facebook Messenger API for Pages Using Curl PHP

[wpdm_file id=171]

Pre Requirements you must have Facebook App and page (Create a new Facebook App and Page or use existing ones.)

More details visit Messenger Doc gohrough the following simple steps

Step 1: Enable Messages on Your Page

Your page can accept and send messages only if you’ve enabled Messages go to your page settings in general settings click on messages and enable it.

Settings-> General->messages


Step 2: Setup Webhook

Before setup Webhook from Facebook we need to create a file as messenger_example.php (file name as your wish) and put the snippet of code for Webhook verification.

// this is weebhook verification
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'Place your verify_token here') {
echo $challenge;

Note : This file location domain must have “https”

Go to your app menu and Select “Webhooks” Click on New Subscription and select page from list of drop down.


After that Enter a Callback URL for a webhook, enter a Verify Token and select message_deliveries, messages, messaging_optins, and messaging_postbacks under Subscription Fields.

Your code should look for the Verify Token and respond with the challenge sent in the verification request. Click Verify and Save.


Step 3: Getting Page Access Token

Go to your app menu and Select “Messenger”, select a PAGE from drop down need to pass all permissions, then access-token will generate.


Step 4: Need to Subscribe the App to the Page

Using the Page Access Token generated in the above step, make the following call. This will subscribe your app to get updates for this specific Page.

Open your terminal and execute the following code

curl -ik -X POST "<token>"

It will return true for successful Subscribe.

Now we need to handle messages when a message is sent from page, receiving its related response means message, senderid, pageid, timestamp etc.

Complete code below.

$input = json_decode(file_get_contents('php://input'), true);

$senderid = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];

$page_access_token="" // palce your accesstoken
//API Url
$url = "$page_access_token";
//The JSON data of message (only text message).
$jsonData = '{
"text":"Hey! This is SRINU how can i help you"
//call to send message function


function send_message($jsonData,$url){

//Encode the array into JSON.
$jsonDataEncoded = $jsonData;

//Initiate cURL.
$ch = curl_init($url);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
$result = curl_exec($ch);



NOTE : Its working only for admin of Facebook App and Page,until Need to get App Review Permissions.

Author Srinu Chilukuri

I am an experienced PHP Developer I have experience in the following areas:
PHP, MySQL, Ajax, CakePHP and Facebook APIs. Website - Google+

Tutorial Categories:

7 responses to “Facebook Messenger API for Pages Using Curl PHP”

  1. suneel says:

    The way you have presented it’s superb I liked it. Thank You so much Srinu,

  2. Keith James Davis says:

    Do you put the “complete code” in your webhook file? Sorry, I’m new to this.

  3. Ritesh says:

    Can you help me?? I am struggling to pull the leads ads from facebook to the client CRM. So how should i do the facebook api. My client uses a .net framework for CRM. So can anyone help me. I am new to this field.

  4. vchapter247 says:

    Hi , i have following response
    {“error”:{“message”:”(#100) Parameter error: You cannot send messages to

    Any solution.

Leave a Reply

Your email address will not be published. Required fields are marked *