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

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

messenger_enable

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.

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.

webhook_setup

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.

webhook_setup2

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.

messenger_pageaccesstoken

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

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.

messenger_response

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:
  • The way you have presented it’s superb I liked it. Thank You so much Srinu,

  • tn

    Thanks

  • Keith James Davis

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

    • Every thing is in source code please download it.

  • Ritesh

    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.