Integrating Wbiztool’s Send Message API with PHP: A Step-by-Step Guide

Introduction

Wbiztool offers a powerful REST API for integrating WhatsApp messaging capabilities into PHP applications. In this guide, we’ll walk through the process of sending WhatsApp messages using Wbiztool’s Send Message API and PHP.

Prerequisites

Before getting started, make sure you have the following:

  1. A Wbiztool account with an active subscription
  2. Your Wbiztool API credentials (client_id, api_key, and whatsapp_client)
  3. A PHP development environment set up

Step 1: Prepare the API Request

To send a WhatsApp message using the Wbiztool API, you’ll need to make a POST request to the following endpoint:

https://wbiztool.com/api/v1/send_msg/

The request should include the following required fields:

  • client_id (integer): Your Wbiztool client ID
  • api_key (string): Your Wbiztool API key
  • whatsapp_client (integer): Your WhatsApp Client ID (found on the Whatsapp Setting page)
  • msg_type (integer): Message type (0 for text, 1 for image, 2 for file)
  • msg (string): The message content
  • phone (string): Recipient’s WhatsApp number with country code (e.g., “919632066772”)
  • country_code (string): Recipient’s country code (e.g., “91” for India, “1” for USA)

Additionally, depending on the msg_type, you may need to include:

  • img_url (string): Image URL for image messages (msg_type=1)
  • file_url (string): File URL for file messages (msg_type=2)
  • file_name (string): File name for file messages (msg_type=2)
  • file (file): Actual file for file messages (alternative to file_url)

Step 2: Make the API Request with PHP

Now, let’s see how to make the API request using PHP. Here’s an example of sending a simple text message:

<?php

$data = [
    'client_id' => client_id, 
    'api_key' => 'api_key',
    'whatsapp_client' => 'your_whatsapp_client_id',
    'msg_type' => 0,
    'msg' => 'Hi, This is a *test* message from _Wbiztool.com_',  
    'phone' => '9632066772',
    'country_code' => '91'
];

$url = 'https://wbiztool.com/api/v1/send_msg/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

In this code snippet:

  1. We define the request data as an associative array ($data), including all the required fields.
  2. We set the API endpoint URL ($url).
  3. Using cURL, we initialize a request to the URL, set the data fields, and specify that we want the response returned as a string.
  4. We execute the request and store the response in the $response variable.
  5. Finally, we echo the response, which will contain the msg_id if the message was submitted successfully.

Sending Image and File Messages

To send an image or file message, you’ll need to adjust the msg_type and include additional fields:

  • For image messages (msg_type=1), include the img_url field with a valid image URL.
  • For file messages (msg_type=2), you can either provide the file_url and file_name fields or upload the file directly using the file field.

Here’s an example of sending a file message by uploading the file:

<?php

$data = [
    'client_id' => client_id,
    'api_key' => 'api_key', 
    'whatsapp_client' => 'your_whatsapp_client_id',
    'msg_type' => 2,
    'msg' => 'Please find the attached file.',
    'phone' => '9632066772',
    'country_code' => '91'
];

$url = 'https://wbiztool.com/api/v1/send_msg/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$file_path = '/path/to/file';
$cfile = new CURLFile($file_path, mime_content_type($file_path), basename($file_path));
$data['file'] = $cfile;

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

The main difference here is that we create a CURLFile object with the file path and add it to the $data array using the file key before executing the request.

Handling the API Response

The Wbiztool API will respond with a JSON object containing the following fields:

  • status (integer): 1 for success, 0 for failure
  • message (string): A descriptive message about the request status
  • msg_id (integer): The unique ID of the sent message (on success)

You can parse the JSON response and handle it accordingly in your application:

$response_data = json_decode($response, true);

if ($response_data['status'] === 1) {
    $msg_id = $response_data['msg_id'];
    // Message sent successfully, do something with the msg_id
} else {
    $error_message = $response_data['message'];
    // Handle the error case
}

Conclusion

Integrating Wbiztool’s Send Message API with your PHP application is straightforward. By making an HTTP POST request to the API endpoint with the required fields, you can easily send text, image, and file messages to WhatsApp users. The API response will provide the necessary information to handle any messages sent and potential errors.

For more details on the API fields, response format, and advanced usage, refer to the official Wbiztool API documentation.

Leave a Comment

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

Scroll to Top