Send pdf to whatsapp using php

I made a video explaining how to do Whatsapp automation using Javascript. Someone has asked this question about how to send a PDF to multiple contacts or groups using whatsapp-web.js in the comments of that video. Let’s find out!

Here’s the example source code to send a PDF to multiple contacts or groups using WhatsApp API

Important: If you abuse this API and send bulk messages to bulk contacts as spam then you might get banned by WhatsApp so be careful when you send any kind of bulk messages.

If you run this code then a PDF named “sample.pdf” from the project folder will be sent to the list of chats defined in the myChats array. Modify the myChats array and PDF attachment name and path as per your requirement.

const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");

const myChats = ["Contact1", "Contact2", "Group1"]; // Add a list of contact names or group names here

const client = new Client({
  authStrategy: new LocalAuth(),
});

client.on("qr", (qr) => {
  qrcode.generate(qr, { small: true });
});

client.on("ready", () => {
  console.log("Client is ready!");
  client.getChats().then((chats) => {
    myChats.forEach((chatName) => {
      const myChat = chats.find((chat) => chat.name === chatName);
      if(myChat){
        const attachmentPdf = MessageMedia.fromFilePath("./sample.pdf");
        client.sendMessage(myChat.id._serialized, attachmentPdf); 
      }
      else {
        console.log(`Chat ${chatName} not found`);
      }
    });
  });
});

client.initialize();

  • Author
  • Recent Posts

A CA- by education, self taught coder by passion, loves to explore new technologies and believes in learn by doing.

In this tutorial you will see How to Send and Receive Messages in WhatsApp using PHP.

Prerequisite :-

# Chat-API Token , Refer : https://www.phphive.info/255/get-whatsapp-password/

PHP Snippet for Sending Messages
Docs :
http://chat-api.phphive.info/docs#/message/sendTextMessageRequest

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?php

$chatApiToken="";// Get it from https://www.phphive.info/255/get-whatsapp-password/

$number="919999999999";// Number

$message="Hello :)"; // Message

$curl=curl_init();

curl_setopt_array($curl,array(

  CURLOPT_URL=>'http://chat-api.phphive.info/message/send/text',

  CURLOPT_RETURNTRANSFER =>true,

  CURLOPT_ENCODING=>'',

  CURLOPT_MAXREDIRS=>10,

  CURLOPT_TIMEOUT=> 0,

  CURLOPT_FOLLOWLOCATION=>true,

  CURLOPT_HTTP_VERSION=>CURL_HTTP_VERSION_1_1,

  CURLOPT_CUSTOMREQUEST=> 'POST',

  CURLOPT_POSTFIELDS=>json_encode(array("jid"=>$number."@s.whatsapp.net","message"=> $message)),

  CURLOPT_HTTPHEADER=>array(

    'Authorization: Bearer '.$chatApiToken,

    'Content-Type: application/json'

  ),

));

$response=curl_exec($curl);

curl_close($curl);

echo$response;

Sample Output :

{

   "error":0,

   "response":{

      "message":"Message sent successfully",

      "messageID":"string"

   }

}

For Receiving Messages

Refer : http://chat-api.phphive.info/docs#/device/messagesRequest

Q : Is There any Daily Limit for Using this API ?

Ans : No! There’s No Daily Limit. But Spamming is Obviously not allowed. We are keeping records of IP’s, if we find someone Spamming our Service then that IP will be Banned.

Chat-API Full Documentation available at http://chat-api.phphive.info/docs

Enjoy 🙂


Was this article/content helpful?

Thanks! Your feedback helps us to improve PHPHive.info


How do I send a PDF to WhatsApp?

To send your compressed PDF via WhatsApp, select the file, then the Share icon. Choose your receiver and tap on the Send button. Easy!

How do I send image on WhatsApp using PHP?

send-image-individual.php 'number' => '12025550108', // TODO: Specify the recipient's number (NOT the gateway number) here. ); $url = 'http://api.whatsmate.net/v3/whatsapp/single/image/message/' .

How can I send and receive messages on WhatsApp using PHP?

Programmable Messaging for WhatsApp and PHP Quickstart.
Sign up for Twilio and activate the Sandbox..
Set up your development environment to send and receive messages..
Opt-in to the Sandbox..
Send your first WhatsApp message..
Receive inbound WhatsApp messages..
Reply to incoming WhatsApp messages..