Blog

Home

/

Blog

Blog Detail Page

PHP script to send messages with WhatsApp Business API

Admin

WhatsApp

0

Yes, it is possible to send messages using the WhatsApp Business API without any third-party API providers. Here is a step-by-step guide on how to do it:

Step 1: Create a WhatsApp Business Account To use the WhatsApp Business API, you need to create a WhatsApp Business Account. You can do this by downloading the WhatsApp Business app and setting up a business profile.

Step 2: Set up a server You need to set up a server to handle the API requests. WhatsApp provides a Docker container that you can use to set up your server. Here are the steps to set up a server:

  1. Download and install Docker.
  2. Download the WhatsApp Business API client from the Facebook Developer website.
  3. Extract the files and navigate to the “docker-compose.yml” file.
  4. Open the file and set the environment variables as described in the documentation.
  5. Run the following command in your terminal: “docker-compose up -d”. This will start the server.

Step 3: Obtain an authorization token To use the WhatsApp Business API, you need to obtain an authorization token from WhatsApp. Here are the steps to obtain a token:

  1. Register for the WhatsApp Business API using your business phone number and follow the verification process.
  2. Follow the steps on the Facebook Developer website to create a Facebook Business Manager account, a Facebook Page, and a WhatsApp Business Account.
  3. Generate a QR code for your WhatsApp Business Account.
  4. Use the WhatsApp Business app to scan the QR code.
  5. Follow the instructions on the Facebook Developer website to generate an authorization token.

Step 4: Send a message Here is an example code in PHP using cURL to send a message through the WhatsApp Business API:

$phone_number = "whatsapp_number"; // The phone number of the recipient in international format
$message_text = "Hello, this is a test message from the WhatsApp Business API!"; // The message text

$api_endpoint = 'https://graph.facebook.com/v18.0/<WHATSAPP_BUSINESS_ACCOUNT_ID>/messages';
// for more details https://developers.facebook.com/docs/whatsapp/business-management-api/get-started/

$api_token = "YOUR_API_TOKEN"; // Replace with your own API token
$data = [
    'phone' => $phone_number,
    'body' => $message_text
];
$ch = curl_init($api_endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "Authorization: Bearer $api_token"));
$result = curl_exec($ch);
curl_close($ch);

Replace whatsapp_number with the phone number of the recipient in international format (e.g. "+1234567890"). Replace YOUR_API_TOKEN with your actual API token obtained from WhatsApp.

In the example code, we are sending a message to the WhatsApp Business API using cURL. We set the appropriate headers and parameters to authenticate and send the message. The API endpoint should be replaced with your own server URL.

Note that the process of setting up and using the WhatsApp Business API can be complex and requires technical expertise. It is important to carefully read and follow WhatsApp’s policies and guidelines to avoid violating their terms of service.

Related Post

Hosting-related questions covering basic to advanced topics.
Admin 9 0
PHP script to send messages with WhatsApp Business API
Admin 8 0
How to Install WordPress in Control Panel
Admin 3 0
Why Resource Limit Is Reached
Admin 3 0
How To Use FTP ?
Admin 3 0

0 Comments

Leave a comment

Low cost hosting - Codingalso.com

Please Feel free to contact us.

We provide professional Website Design, Static Websites, Dynamic Websites, Android Applications and Hosting Services. with our experienced developers. 9 Years of working experience as a web designer & developer. Hire "website developers" us. and use the latest technologies for your websites & apps to grow your business.
CodingAlso

We working since 2013 in this field.

payment mods - CodingAlso

An MSME-registered company.

MSME - CodingAlso

Get In Touch

Address

Bahadinaghar, Raghunathganj, Murshidabad, West Bengal, Pin-742225, India

Email

support@codingalso.com

Phone

+91 8116312433

© CodingAlso.Com. All Rights Reserved.