简体   繁体   中英

I am looking for some simple PHP code to send Twilio SMS message (without a framework required)

OK, I know I am a noob but I have gotten by with PHP for a while by just writing code in a text editor. With Twilio samples everything is prefaced with...

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';

like they show here.. https://www.twilio.com/docs/sms/send-messages

do I need this framework? I just want to send a single SMS message (or maybe loop through a bunch)

Something to get you started. Replace the ... things.

<?php

$twilio_account_sid = "AC...";
$twilio_auth_token = "2d...";
$twilio_phone_number = "+1...";

$payload = [
    'From' => $twilio_phone_number,
    'To' => '+1...',
    'Body' => 'This is the body of the message'
];

$url = 'https://api.twilio.com/2010-04-01/Accounts/' . $twilio_account_sid . '/Messages.json';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $twilio_account_sid . ':' . $twilio_auth_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));

$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close ($ch);

var_dump( $status );
var_dump( $response );

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM