Skip to content

Snippets / PHP

PHP

Send Twilio Text on App Event

D Donald McGuinn · 146 views · Updated 2 years ago
Getting notified of new posts, updated posts, new timeline posts and all of those important notifications is key to running your Voxel themed directory. Twilio is powerful and very inexpensive to send texts out for important information. In order to do so, follow these instructions:

Navigate to the backend of your site and go to Voxel > App Events.
Click on the notification you want to have sent via Twilio.
Open the accordion and click on “Advanced”.
Copy all of that information and paste it into the functions.php file of your Voxel Child Theme. If you do not have a child theme setup, visit this article to learn how.
Where it says “// your custom code…” paste the following code and get rid of the “// your custom code…”

Be sure to replace all of the necessary information where needed including Account SID, Auth Token, Twilio number with leading country code, and the destination number with country code. Feel free to customize the message under the destination number to further enhance the message.
// Twilio credentials
    $account_sid = 'YOUR_TWILIO_ACCOUNT_SID';
    $auth_token = 'YOUR_TWILIO_AUTH_TOKEN';
    $twilio_number = 'YOUR_TWILIO_PHONE_NUMBER';

    // The destination phone number and message content
    $to_number = 'DESTINATION_PHONE_NUMBER';
    $message_content = 'A post has been updated!';

    // Twilio API URL
    $url = 'https://api.twilio.com/2010-04-01/Accounts/' . $account_sid . '/Messages.json';

    // Setup request to send json via POST
    $data = array(
        'From' => $twilio_number,
        'To' => $to_number,
        'Body' => $message_content
    );
    $post = http_build_query($data);

    // Set up cURL
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $account_sid . ':' . $auth_token);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    // Send the request and get the response
    $response = curl_exec($ch);
    $error = curl_error($ch);
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // Close cURL session handle
    curl_close($ch);

    // Check for errors and process the response
    if ($error) {
        error_log('Curl error: ' . $error);
    } else {
        // Process the response as needed
        error_log('Twilio API response: ' . $response);
    }
PHP · 43 lines

Discussion

Ask a question or share how you used this.

Log in to join the discussion.

No comments yet — be the first.

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.