PHP code example of adrii / whatsapp-api

1. Go to this page and download the library: Download adrii/whatsapp-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adrii / whatsapp-api example snippets


use Adrii\Whatsapp\Whatsapp;

$graph_version    = "v15.0";
$phone_number_id  = "{phone_number_id}";
$access_token     = "{access_token}";
$recipient_id     = "{recipient_id}";

$ws = new Whatsapp($phone_number_id, $access_token, $graph_version);

$ws->webhook()->connect($_GET);

$ws->send_message()->text("Aloha 🍍", $recipient_id);

$ws->send_message()->template("hello_world", $recipient_id);

$component_header = array(
    "type" => "header",
    "parameters" => array(
        array(
            "type" => "image",
            "image" => array(
                "link" => "https://avatars.githubusercontent.com/u/29653964?v=4"
            )
        ),
    )
);

$component_body = array(
    "type" => "body",
    "parameters" => array(
        array(
            "type" => "text",
            "text" => "Adrii 🍍"
        )
    )
);

$component_button = array(
    "type"      => "button",
    "sub_type"  => "url",
    "index"     => 0,
    "parameters" => array(
        array(
            "type" => "text",
            "text" => "https://github.com/AdrianVillamayor/Whatsapp-Api"
        )
    )
);

$ws->send_message()->addComponent($component_header, $component_body, $component_button);

$response = $ws->send_message()->template("sample_purchase_feedback", $recipient_id);

$ws->send_message()->location("41.403191", "2.174840", "La Sagrada Família", "C/ De Mallorca, 401, 08013 Barcelona", $recipient_id);

    $contact = array(
        "addresses" => array(
            array(
                "city"          => "city name",
                "country"       => "country name",
                "country_code"  => "code",
                "state"         => "Contact's State",
                "street"        => "Contact's Street",
                "type"          => "Contact's Address Type",
                "zip"           => "Contact's Zip Code"
            )
        ),

        "birthday" => "14-02-1997",
        "emails" => array(
            array(
                "email" => "email",
                "type" => "HOME"
            ),
            array(
                "email" => "email",
                "type" => "WORK"
            )
        ),
        "name" => array(
            "formatted_name" => "formatted name value",
            "middle_name" => "last name value",
        ),
        "phones" => array(
            array(
                "phone" => "654034823",
                "type" => "MAIN"
            ),
            array(
                "phone" => "Phone number",
                "type" => "HOME"
            ),
            array(
                "phone" => "Phone number",
                "type" => "WORK"
            )
        ),
        "urls" => array(
            array(
                "url" => "some url",
                "type" => "WORK"
            )
        )
    );


$ws->send_message()->addContact($contact);

$response = $ws->send_message()->contact($recipient_id);


$ws->send_message()->addContact($contact_0, $contact_1, ...);

$ws->send_message()->media("image", "https://avatars.githubusercontent.com/u/29653964?v=4", $recipient_id);

$ws->send_message()->media("video", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/04/file_example_MP4_480_1_5MG.mp4", $recipient_id);

$ws->send_message()->media("document", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/10/file-sample_150kB.pdf", $recipient_id);

$ws->send_message()->media("audio", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/11/file_example_MP3_700KB.mp3", $recipient_id);

$ws->send_message()->media("sticker", "https://img-03.stickers.cloud/packs/210a9e68-b249-405f-8ea1-9af015ef074a/webp/c5b7bded-e0f0-4f79-86aa-ffd825aba680.webp", $recipient_id);

$ws->send_message()->media("image", "https://avatars.githubusercontent.com/u/29653964?v=4", $recipient_id, "individual", true, "your-image-caption-to-be-sent");

$ws->send_message()->media("video", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/04/file_example_MP4_480_1_5MG.mp4", $recipient_id, "individual", true, "your-video-caption-to-be-sent");

$ws->send_message()->media("document", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/10/file-sample_150kB.pdf", $recipient_id, "individual", true, null, "example_filename.pdf");

$button = [
        "header" => "Header",
        "body"   => "Body",
        "footer" => "Footer",
        "action" => [
            "buttons" => [
                [
                    "type" => "reply",
                    "reply" => [
                        "id" => "UNIQUE_BUTTON_ID_1",
                        "title" => "BUTTON_TITLE_1"
                    ]
                ],
                [
                    "type" => "reply",
                    "reply" => [
                        "id" => "UNIQUE_BUTTON_ID_2",
                        "title" => "BUTTON_TITLE_2"
                    ]
                ]
            ]
        ]
    ];

$ws->send_message()->interactive($button, $recipient_id, "button");


$list = [
    "header" => "Test Header",
    "body"   => "Test Body",
    "footer" => "Test Footer",
    "action" => [
        "button" => "BUTTON_TEXT",
        "sections" => [
            [
                "title" => "SECTION_1_TITLE",
                "rows" =>
                [
                    [
                        "id" => "SECTION_1_ROW_1_ID",
                        "title" => "SECTION_1_ROW_1_TITLE",
                        "description" => "SECTION_1_ROW_1_DESCRIPTION"
                    ],
                    [
                        "id" => "SECTION_1_ROW_2_ID",
                        "title" => "SECTION_1_ROW_2_TITLE",
                        "description" => "SECTION_1_ROW_2_DESCRIPTION"
                    ]
                ]
            ],
            [
                "title" => "SECTION_2_TITLE",
                "rows" => [
                    [
                        "id" => "SECTION_2_ROW_1_ID",
                        "title" => "SECTION_2_ROW_1_TITLE",
                        "description" => "SECTION_2_ROW_1_DESCRIPTION"
                    ],
                    [
                        "id" => "SECTION_2_ROW_2_ID",
                        "title" => "SECTION_2_ROW_2_TITLE",
                        "description" => "SECTION_2_ROW_2_DESCRIPTION"
                    ]
                ]
            ]
        ]
    ]
];

$ws->send_message()->interactive($list, $recipient_id, "list");