2: Berichten verzenden
Construeer de client vanuit de URL en het geheim van je gateway, en roep vervolgens een method aan per berichttype.
$wa = %WhatsApp($url, $secret)
$wa->text('31612345678@c.us', 'Hello from Phlo')
Een contact is een WhatsApp-id zoals 31612345678@c.us (individu) of een ...@g.us (groep). De demo accepteert een gewoon nummer en voegt @c.us automatisch voor je toe.
2.1: The resource methods
| Method | Sends |
|---|---|
text($to, $text) | a text message |
image($to, $file, $text) | an image with optional caption |
document($to, $file, $text) | a file as a document |
audio($to, $file) / voice($to, $file) | audio / a voice note |
location($to, $lat, $lon, $text) | a location pin |
poll($to, $name, $options, $multi) | a poll |
reaction($msg, $emoji) | an emoji reaction |
read($chat) | marks a chat read |
status() / health() / qr() | gateway state and the login QR |
Media methods take a Phlo file object. An uploaded file arrives in the payload as a file already, so you can pass it straight through:
route both POST send image {
$wa = %WhatsApp($url, $secret)
$wa->image($this->contact(%payload->to), %payload->image, (string)%payload->caption)
}2.2: Verzendingen kunnen mislukken; verwacht het.
Elke oproep gaat via het netwerk naar de gateway, die offline of niet verbonden kan zijn. Wrap verzendt zodat een fout een duidelijke boodschap voor de gebruiker wordt in plaats van een foutpagina. De demo retourneert een toast met de fout van de gateway bij een mislukking en een succes-toast in andere gevallen.
Laatst bijgewerkt op 05-11-2026