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.
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 "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<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 = "https://graph.facebook.com/v2.6/me/messages?access_token=$page_access_token"; //The JSON data of message (only text message). $jsonData = '{ "recipient":{ "id":"'.$senderid.'" }, "message":{ "text":"Hey! This is SRINU how can i help you" } }'; //call to send message function send_message($jsonData,$url); 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.
Tutorial Categories:
The way you have presented it’s superb I liked it. Thank You so much Srinu,
Thanks
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.
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.
Hi
Hi , i have following response
{“error”:{“message”:”(#100) Parameter error: You cannot send messages to
this
id”,”type”:”OAuthException”,”code”:100,”fbtrace_id”:”BQw9yV0SdPM”}}
Any solution.