Using the Twilio API, I've got my PHP functioning to send to one phone number, and can successfully send. We're looking to send to multiple numbers from one request and to do so, I've set up an array of numbers to iterate through, however, I keep getting a 500 error when I attempt to send the message by hitting the URL. Below is the file I'm working with.
Running PHP 7.2 on a Linux server. I'm running CentOS 7.7 and Apache 2.4.43 if that matters at all.
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXX';
$token = 'XXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = “This is a test of sending the text message to multiple phone numbers.”
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
);
I'm not super familiar with PHP as I'm mostly in marketing, but I'm deputizing as developer in these crazy times because I know just enough to be dangerous. I'm thinking it is something with the foreach section, as that's the only piece that has changed from the single send.
Any help is appreciated!
Figured it out thanks to the help from #LuisE! I went through and figured out where I was missing the semicolons after the array, the $bodyTxt, and the $message = $twilio->messages.
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXX';
$token = 'XXXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = 'This is a test of sending the text message to multiple phone numbers.';
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages;
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
Related
<?php
// Include the bundled autoload from the Twilio PHP Helper Library
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = '8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with Voice capabilities
$twilio_number = "+12244073476";
// Where to make a voice call (your cell phone?)
$to_number = '+22678733599';
$client = new Client($account_sid, $auth_token);
try {
$client->account->calls->create(
$to_number,
$twilio_number,
array(
// two url can't function because the first url will be erase by the value of the second
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
// echo "Started call : " . $client->sid;
} catch (Exception $e) {
echo "Error : " . $e->getMessage();
}
?>
When I start the call to the phone number from twilio number, it's okay. But it is impossible to make communication.
How can I make communication after answering the call?
Twilio developer evangelist here.
Your title says you want to connect a browser to a phone. However, your code only shows use of the REST API.
To create calls from a browser to a phone you need to use the Twilio Voice SDK. I would recommend that you go through the instructions to run the Voice SDK quick start application. That will explain how making a call from a browser to a phone works and give you an example application to run and investigate. The quick start application is fully documented.
Hi I just got approved on Twilio to use it's WhatsApp messaging service.
I have no problem when testing it in the sandbox, but I got trouble when I moved it into production environment.
Based on Twilio explanation, I have to start the conversation to WhatsApp customer using one of pre-approved templates. When the customer replied, we got 24 hours of window to send freeform messages.
I already did what's in the https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates but unfortunately the given example is actually for freeform message.
Here's the script:
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACxxxxxxxxxxxxxxxxxxxxx";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("whatsapp:+14155238886", // to
array(
"from" => "whatsapp:+15005550006",
"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 O12235234"
)
);
print($message->sid);
Can anyone please help me with PHP script on how to send the WhatsApp message using this pre-approved template?
Alright, maybe some of you got here trying to ask the similar question and here's what I got after contacting the Twilio Support:
My WhatsApp API works now.
There's nothing wrong with my code nor their code (what's in their documentation https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates), actually they're using the same code to send either template message or freeform message.
Their Template Submission API to WhatsApp contains bug that creates mismatch between what we actually had in Twilio and what WhatsApp actually received. So that's why the first message I sent (even though I used the pre-approved template) always treated as freeform message thus it undelivered.
Twilio WhatsApp API is still in beta service, means bugs are expected. While it's still in beta, they recommend that we need to create templates as simple as possible and avoid formatting like bold, italics, strikethrough, etc also new lines (\n) being used in templates.
Thats all I can share and I hope you don't have problem just like I did.
Cheers!
Below is our code with predefined templates
$number = "+919XXXXXXXXX";
$to = "whatsapp:" . $number;
$from = "whatsapp:+1YYYYYYYYYY";
$msg = "Un rendez-vous de {{1}} pour {{2}} avec {{3}} et prévu le {{4}} a été créé.";
$accountSid = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
$authToken = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
$twilioClient = new Client($accountSid, $authToken);
$msg_data = array("from" => $from, "body" => $msg);
try {
$message = $twilioClient->messages->create($to, $msg_data);
$response = $message->sid ? $message->sid : '';
error_log("Twilio msg response : " . print_r($response, true));
} catch (TwilioException $e) {
error_log('Could not send whatsapp notification to ' . $number);
error_log('Could not send whatsapp TwilioException' . $e->getMessage());
}
One suggestion check for white space while creating message string. Even for a single white space they reject it.
I would like to schedule when I send sms messages using Twilio. I currently can schedule when to run php scripts using Windows Scheduler, however, whenever I try to send messages using twilio, the script doesn't execute.
PHP script that sends sms text message via using twilio
<?php
// Required if your environment does not handle autoloading
include '../vendor/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+19894837813',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+19823837823',
// the body of the text message you'd like to send
'body' => 'Hey Jenny! Good luck on the bar exam!'
)
);
?>
VB script that references batch file
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\xampp\htdocs\Twilio\php\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
Batch file that calles php script
"C:\xampp\php\php.exe" -f "C:\xampp\htdocs\Twilio\php\index.php"
I'm having some problems with my Shopify 'orders/paid' webhook that I install onto the users store via the Shopify SDK (using a PHP wrapper: https://github.com/phpclassic/php-shopify). Anyways, I create the webhook like so:
$shop = $this->request->getVar('shop'); // Get Shopify store URL
// Get access token for store
$config = array('ShopUrl' => $shop, 'ApiKey' => 'xxx', 'SharedSecret' => 'xxx');
\PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::getAccessToken();
// Keep track of access_token for SDK calls and authenticate to store for webhook creation
$addToken = $this->Stores->addAccessToken($shop, $accessToken);
$config = array('AccessToken' => $accessToken, 'ShopUrl' => $shop);
$shopify = new \PHPShopify\ShopifySDK($config);
// Create orders/paid webhook
$orderPaidHook = array('topic' => 'orders/paid', 'address' => 'https://api.autoloapp.com/webhook/create', 'format' => 'json');
try {
$webhooks = $shopify->Webhook->post($orderPaidHook);
}
This code as far as I can tell works, using print_r($webhooks) gives me a valid Webhook that has (in theory) been installed on to the store. However, when an order is actually paid for on the store I DO NOT get a message.
Using the "Test Webhook" settings under "Notification" in the Shopify Admin my webhook works perfectly. Using PostMan to simulate a webhook call works perfectly.
The code to process the webhook looks like this:
$headers = getallheaders();
$topic = $headers['X-Shopify-Topic'];
$storeName = $headers['X-Shopify-Shop-Domain'];
if ($topic == 'orders/paid') {
// Do some stuff, insert row into database
}
Again, when I use PostMan or the built-in "Test Webhook" setting for Shopify the row in my database gets created just fine. When I do it with a real webhook nothing happens. I'm not sure why this is happening, as far as I know all the headers should be the same. Do I need to verify the webhook in some way?
For reference I'm using CodeIgniter 4.0, PHP 7.0, and the newest version of the Shopify SDK.
I found this note on Shopify's Docs: "Note that if you are using a Rack based framework such as Ruby on Rails or Sinatra the header you are looking for is HTTP_X_SHOPIFY_HMAC_SHA256". With my current set-up I should still be using X-Shopify-Shop-Domain instead of HTTP_X_Shopify_Shop_Domain correct? Any help greatly appreciated!
So I finally figured out that for some reason the 'orders/paid' webhook wouldn't work for me in "production environments" despite working fine in my test environments. Simply changing the topic to 'orders/create' resolved my problem.
So I've been testing out twilio on a free web hosting server and this is what I get:
Fatal error: Class 'Twilio\Rest\Api' not found in /home/u512189195/public_html/twilio-php-master/Twilio/Rest/Client.php on line 263
This is my code:
<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+15558675309',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+15017250604',
// the body of the text message you'd like to send
'body' => "Hey Jenny! Good luck on the bar exam!"
)
);
?>
I've also tried without the __DIR__ .
Obviously, I've replaced the variables with my own data.
I've uploaded the php master library which we get from twilio documentation website. - https://www.twilio.com/docs/libraries/php
You can install twilio package via composer
https://packagist.org/packages/twilio/sdk
then;
<?php
require __DIR__ . '/vendor/autoload.php';
...
use Twilio\Rest\Client;
...
You should check that the class exists in the library you downloaded.
Look for:
twilio-php-master/Twilio/Rest/Api.php
If it is missing (and that is what the error message you get implies) then you need to download a more current version of the helper library.
See the section on 'Using without Composer' here.