0

Looking for a way to specify image path that's stored in local server in Whatsapp twilio api. I can send image via api using hosted image links but having difficulty specifying local file path. Can you help?

<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.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 = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$message = $twilio->messages
                  ->create("whatsapp:+15017122661", // to
                           [
                               "mediaUrl" => ["https://images.unsplash.com/photo-1545093149-618ce3bcf49d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=668&q=80"],
                               "from" => "whatsapp:+14155238886"
                           ]
                  );

print($message->sid);
?>

In short, I want to know whether i can attach local image file to the message using the mediaURL attribute.

2
  • Your question is for PHP, but I answered the same question for .NET here: stackoverflow.com/questions/74378937/…
    – Swimburger
    Commented Nov 14, 2022 at 22:20
  • I tried to answer your question, but a mod has removed the answer because it is the same as the .NET one. I hope it helps!
    – Swimburger
    Commented Nov 14, 2022 at 22:20

1 Answer 1

1

When Twilio processes your request to send an MMS or a WhatsApp Message with an mediaUrl, Twilio will retrieve the file from the Media URL that you provided. However, Twilio cannot access your localhost network.

First, make sure the media files are served by your web server.

Then, during development, you can tunnel your localhost network to the public internet using tools like ngrok. You'd run a command like this:

ngrok http https://localhost:5000

https://localhost:5000 should be replaced with the localhost URL to the web server serving your media locally.

The output will display the public Forwarding URL created by ngrok. Any HTTP request to this Forwarding URL will be forwarded to your application listening to https://localhost:5000. Use this public Forwarding URL with the path to your file, as your Media URL and Twilio should be able to retrieve it and sent it as an MMS.

When you deploy your application to a web server that is publicly accessible, you won't need a tunnel anymore, but can point to the public domain of where your media is served.

Note, if you don't want your media to be publicly accessible, you can secure your application by validating the HTTP request originates from Twilio.


Alternatively, you can upload your media to a storage service like AWS S3, Azure Storage, Google Cloud Storage, etc. Make sure the media is publicly accessible and then use the public URL of the uploaded media.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.