PHP code example of xaamin / whatsapi

1. Go to this page and download the library: Download xaamin/whatsapi 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/ */

    

xaamin / whatsapi example snippets


    'Whatsapi' => 'Xaamin\Whatsapi\Facades\Laravel\Whatsapi',
    'WhatsapiTool' => 'Xaamin\Whatsapi\Facades\Laravel\Registration',

    // Include the composer autoload file
    Xaamin\Whatsapi\Facades\Native\Whatsapi;
    use Xaamin\Whatsapi\Facades\Native\Registration;

    // Or, if you want you can add a class alias
    // class_alias('Xaamin\Whatsapi\Facades\Native\Whatsapi', 'Whatsapi');
    // class_alias('Xaamin\Whatsapi\Facades\Native\Registration', 'Registration');

    // Of course, you can use the Config class from your favorite Framework.
    $config = __DIR__ . 'config/whatsapi.php';

    Whatsapi::setConfig($config);

    # Codeigniter 3 example
    
    use CI_Session;
    use Xaamin\Whatsapi\Sessions\SessionInterface;

    class SessionManager implements SessionInterface{

        # The key used in the Session.
        protected $key = 'itnovado_whatsapi';

        # Session object.
        protected $session;

        public function __construct(CI_Session $session)
        {
            $this->session = $session;
        }

        public function getKey()
        {
            return $this->key;
        }

        public function put($value)
        {
            $this->session->set_userdata($this->getKey(), $value);
        }

        public function pull()
        {
            $data = $this->session->userdata($this->getKey());

            $this->session->unset_userdata($this->getKey());

            return $data;
        }
    }

    // Get some resources
    $ci =& get_instance();
    $ci->load->driver('session');
    
    $sessionManager = new SessionManager($ci->session);

    // Override the default session implementation
    Whatsapi::setSessionManager($sessionManager);

    php artisan vendor:publish --provider="Xaamin\Whatsapi\WhatsapiServiceProvider" --tag="config"

    $number = '5219511552222'; # Number with country code
    $code = '132456'; # Replace with received code  

    $response = WhatsapiTool::registerCode($number, $code);


    $messages = $messages = Whatsapi::getNewMessages();

    if($messages)
    {
        foreach($messages as $message)
        {
            ...
        }
    }

    $result = Whatsapi::syncContacts(['5219512222222', '5219512222223']);
    
    foreach ($result->existing as $number => $account)
    {
        ... 
    }

    foreach ($result->nonExisting as $number)
    {
        ...
    }