WhatsApp PHP

Client
in package

WhatsApp PHP client instance.

Tags
author

Gabriel Silva

license

MIT

Table of Contents

Properties

$authToken  : string|null
Authentication token if any.
$hostUrl  : string
Base URL of the API.
$instance  : Client|null
Client instance.
$timeout  : int
Request timeout.

Methods

checkLogin()  : bool
Checks if the client is logged in to WhatsApp.
consumeWebhook()  : Message|null
Consumes a message received webhook.
create()  : Client
Creates a Client instance.
getChats()  : array<string|int, Chat>
Get a list of available chats.
getContacts()  : array<string|int, Profile>
Gets a list of all contact profiles.
getInstance()  : Client
Gets the current Client instance.
getMessages()  : array<string|int, Message>
Gets the messages from a chat.
getProfile()  : Profile|null
Gets a user profile.
getUser()  : Profile|null
Gets the current connected user profile.
login()  : QRCode|bool
Performs a login to WhatsApp, if the client is not logged yet.
logout()  : bool
Disconnects from WhatsApp, if the client is already logged in.
searchMessages()  : array<string|int, Message>
Searches for a message.
sendLocation()  : bool
Sends a location pin.
sendMedia()  : bool
Sends a media message.
sendMessage()  : bool
Sends a text message.
sendSticker()  : bool
Sends a sticker.
sendVoice()  : bool
Sends a voice message.
setAuthToken()  : Client
Sets the API auth token.
setHostUrl()  : Client
Sets the base URL of the API.
setOffline()  : bool
Sends an Offline presence status to the client.
setOnline()  : bool
Sends an Online presence status to the client.
setTimeout()  : Client
Sets the request timeout.
__construct()  : mixed
Disable construction because of Singleton pattern.
request()  : array<string|int, mixed>|null
Perform an HTTP request.

Properties

$authToken

Authentication token if any.

private static string|null $authToken

$hostUrl

Base URL of the API.

private static string $hostUrl

$instance

Client instance.

private static Client|null $instance

$timeout

Request timeout.

private static int $timeout

Methods

checkLogin()

Checks if the client is logged in to WhatsApp.

public checkLogin() : bool
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on logged in, otherwise false.

consumeWebhook()

Consumes a message received webhook.

public consumeWebhook(string $body) : Message|null
Parameters
$body : string

Request body in raw format.

Return values
Message|null

Returns the message if valid.

create()

Creates a Client instance.

public static create(string $hostUrl[, string|null $authToken = null ][, int $timeout = 30 ]) : Client
Parameters
$hostUrl : string

Base URL of the API.

$authToken : string|null = null

(Optional) Authentication token if any.

$timeout : int = 30

(Optional) Request timeout in seconds.

Return values
Client

Returns the client instance.

getChats()

Get a list of available chats.

public getChats() : array<string|int, Chat>
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
array<string|int, Chat>

Returns a list of chats.

getContacts()

Gets a list of all contact profiles.

public getContacts() : array<string|int, Profile>
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
array<string|int, Profile>

Returns a list of profiles.

getInstance()

Gets the current Client instance.

public static getInstance() : Client
Tags
throws
Exception

Throws an exception if the Client instance was not created.

Return values
Client

Returns the client instance.

getMessages()

Gets the messages from a chat.

public getMessages(string $number[, int|null $limit = null ]) : array<string|int, Message>
Parameters
$number : string

Contact number to fetch messages.

$limit : int|null = null

(Optional) Maximum number of messages to fetch. Leave blank to get as many as possible.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
array<string|int, Message>

Returns a list of messages.

getProfile()

Gets a user profile.

public getProfile(string $number) : Profile|null
Parameters
$number : string

Phone number to fetch profile.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
Profile|null

Returns the profile if found.

getUser()

Gets the current connected user profile.

public getUser() : Profile|null
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
Profile|null

Returns the profile if found.

login()

Performs a login to WhatsApp, if the client is not logged yet.

public login() : QRCode|bool
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
QRCode|bool

Returns the QR Code, false if already logged in.

logout()

Disconnects from WhatsApp, if the client is already logged in.

public logout() : bool
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

searchMessages()

Searches for a message.

public searchMessages(string $query[, string|null $number = null ][, int|null $limit = null ][, int|null $page = null ]) : array<string|int, Message>
Parameters
$query : string

Query string to search.

$number : string|null = null

(Optional) Specific phone number to search in.

$limit : int|null = null

(Optional) Maximum number of messages to fetch.

$page : int|null = null

(Optional) Results page number.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
array<string|int, Message>

Returns a list of messages.

sendLocation()

Sends a location pin.

public sendLocation(string $number, int $latitude, int $longitude[, string|null $address = null ][, string|null $url = null ][, string|null $replyTo = null ]) : bool
Parameters
$number : string

Phone number to send location.

$latitude : int

Latitude coordinates.

$longitude : int

Longitude coordinates.

$address : string|null = null

(Optional) Address name to include in the message.

$url : string|null = null

(Optional) URL to include in the message.

$replyTo : string|null = null

(Optional) Another message ID to reply to.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

sendMedia()

Sends a media message.

public sendMedia(string $number, string $file[, string|null $message = null ][, bool $viewOnce = false ][, bool $asDocument = false ][, bool $asVoice = false ][, bool $asGif = false ][, bool $asSticker = false ][, string|null $replyTo = null ]) : bool
Parameters
$number : string

Phone number to send media.

$file : string

File location path or remote URL.

$message : string|null = null

(Optional) Caption to send with the media.

$viewOnce : bool = false

(Optional) Send the media as view once.

$asDocument : bool = false

(Optional) Send the media as a document.

$asVoice : bool = false

(Optional) Send audio media as a voice.

$asGif : bool = false

(Optional) Send video media as a GIF.

$asSticker : bool = false

(Optional) Send image media as a sticker.

$replyTo : string|null = null

(Optional) Another message ID to reply to.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

sendMessage()

Sends a text message.

public sendMessage(string $number, string $message[, string|null $replyTo = null ]) : bool
Parameters
$number : string

Phone number to send message.

$message : string

Message body.

$replyTo : string|null = null

(Optional) Another message ID to reply to.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

sendSticker()

Sends a sticker.

public sendSticker(string $number, string $file[, string|null $replyTo = null ]) : bool
Parameters
$number : string

Phone number to send the sticker.

$file : string

Sticker image file location path or remote URL.

$replyTo : string|null = null

(Optional) Another message ID to reply to.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

sendVoice()

Sends a voice message.

public sendVoice(string $number, string $file[, bool $viewOnce = false ][, string|null $replyTo = null ]) : bool
Parameters
$number : string

Phone number to send the voice message.

$file : string

Audio file location path or remote URL.

$viewOnce : bool = false

(Optional) Send the audio as view once.

$replyTo : string|null = null

(Optional) Another message ID to reply to.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

setAuthToken()

Sets the API auth token.

public setAuthToken(string|null $authToken) : Client
Parameters
$authToken : string|null

Authentication token if any.

Return values
Client

Returns the client instance.

setHostUrl()

Sets the base URL of the API.

public setHostUrl(string $hostUrl) : Client
Parameters
$hostUrl : string

URL.

Return values
Client

Returns the client instance.

setOffline()

Sends an Offline presence status to the client.

public setOffline() : bool
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

setOnline()

Sends an Online presence status to the client.

public setOnline() : bool
Tags
throws
RequestException

Throws an exception if the request fails.

Return values
bool

Returns true on success, false on failure.

setTimeout()

Sets the request timeout.

public setTimeout(int $timeout) : Client
Parameters
$timeout : int

(Optional) Request timeout in seconds.

Return values
Client

Returns the client instance.

__construct()

Disable construction because of Singleton pattern.

private __construct() : mixed

request()

Perform an HTTP request.

private request(string $url[, string $method = 'GET' ][, array<string|int, mixed> $data = [] ][, string $file = null ]) : array<string|int, mixed>|null
Parameters
$url : string

URL path to request.

$method : string = 'GET'

(Optional) HTTP method.

$data : array<string|int, mixed> = []

(Optional) Associative array with the request body.

$file : string = null

(Optional) Uploadable file location or URL.

Tags
throws
RequestException

Throws an exception if the request fails.

Return values
array<string|int, mixed>|null

Returns the response if valid.


        
On this page

Search results