PHP Whatsapp simple example code

Can someone provide a simple example of code using whatsapp php.

I need an example of code that can be used on my real server (simple shared hosting).

Maybe just a simple html form with a phone field and a meesage field that will send a whatsapp message to that number. and a function that will send every whatsapp message to a mail address...

I bought a number and i tried using the strart up guides, but couldn't get it. i know PHP, but very basic.

Thanks!

Tagged:

Answers

  • Hi,

    What you're asking for is not actually possible using WhatsApp because of the rules around unsolicited messages that Facebook enforce. WhatsApp only allow you to initiate a conversation outside of a 24 hour session via templates; you can learn more here (click PHP in the right hand side for sample code in PHP):

    https://www.twilio.com/docs/whatsapp/tutorial/send-whatsapp-notification-messages-templates

    This webinar is also a very good starting point for learning the basic concepts of how WhatsApp works from sandbox mode to templates:

    https://ahoy.twilio.com/devgen_webinar_gs_whatsapp_emea_1

    Good luck!

    Gary

  • Hi gary!

    Thank you for your answer.

    So i have add a new file on my server called "index.php" and add this code:

    <?php use Twilio\Rest\Client; // Find your Account SID and Auth Token at twilio.com/console // and set the environment variables. See http://twil.io/secure $sid = '1Ca6dd29855f33afjasd8f9j137413753s'; $token = '197e817cj8396dbb1a18afab5eebb1'; $twilio = new Client($sid, $token); $message = $twilio->messages ->create("whatsapp:+972778887526", // to [ "from" => "whatsapp:+12677759756", "body" => "Hi, Joe! Thanks for placing an order with us. We’ll let you know once your order has been processed and delivered. Your order number is O123234" ] ); print($message->sid); ?>

    (I have changed the numbers [sid,token,to,from] but i'm using the true ones on my code)

    Now if i go to that file's url on chrome i get http error 500.

    Does it suppose to work like that or am i missing something?

    Am i suppose to include this "autoload.php" file? if yes, where do i get it?

    Thanks!
    Ben

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support