How to send SMS message from Wordpress, using Twilio? - php

I'm trying to use Twilio in a Wordpress app and it didn't work, but my same code works in a different site/server.
I added the twilio-php folder and some PHP code to call it inside the wordpress root. I also added code to include it in the existing Wordpress PHP code, and I can't figure it out where the problem is. Can you help?
<?php
require "twilio-php/Services/Twilio.php";
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Include the PHP Twilio library. You need to download the library from
// twilio.com/docs/libraries, and move it into the folder containing this
// file.
// Set our AccountSid and AuthToken from twilio.com/user/account
$AccountSid = "********************";
$AuthToken = "*********************";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
/* Your Twilio Number or Outgoing Caller ID */
$from = '**********';
// make an associative array of server admins. Feel free to change/add your
// own phone number and name here.
$people = array(
"*********" => "******",
"**********" => "*********",
);
// Iterate over all admins in the $people array. $to is the phone number,
// $name is the user's name
foreach ($people as $to => $name) {
// Send a new outgoing SMS */
$body = "Hello this is a test message";
$client->account->sms_messages->create($from, $to, $body);
echo "Sent message to $name";
}
?>

i check the server log and notice that curel is not installed in the server then i installed and reboot the server and now it works fine thank you for your great support...Kevin Burke thanx :D

Related

How to send SMS to a PHP array with Twilio API

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);
}

How to send sms messages using twilio and windows scheduler?

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"

How to send message from WhatsApp in PHP with WhatsAPI Official?

I'm trying to use the WhatsApi Official library to send a message via WhatsApp from a php file. I've moved in my Apache web server the library, in a folder call test, like this:
The file whatsapp.php is this one:
<?php
require_once './src/whatsprot.class.php';
$username = "1XXXXXXXXX";
$password = "password";
$w = new WhatsProt($username, "0", "My Nickname", true); //Name your application by replacing “WhatsApp Messaging”
$w->connect();
$w->loginWithPassword($password);
$target = '1xxxxxxxxx'; //Target Phone,reciever phone
$message = 'This is my messagge';
$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message
echo "Message Sent Successfully";
?>
I'm facing some problem with the library new WhatsProt(), which blocks all the code (may be sockets ?).
So my question is, how can I fix this problem ? If no, are there any other solution to send message from a pho script ?
You can use below script to send message from whatsapp in PHP.
https://github.com/venomous0x/WhatsAPI/tree/master/examples
Configure the source code in Apache and run examples/whatsapp.php file.
You have change below configurations.
//simple password to view this script
$config['webpassword'] = 'MakeUpPassword';
and
$config['YOURNAME'] = array(
'id' => 'e807f1fcf82d132f9bb018ca6738a19f',
'fromNumber' => '441234567890',
'nick' => "YOURNICKNAME",
'waPassword' => "EsdfsawS+/ffdskjsdhwebdgxbs=",
'email' => 'testemail#gmail.com',
'emailPassword' => 'gmailpassword'
);
It's working for me..
afaik you are probably better off currently writing an interface to a python project. E.g. have a microservice that does sending of messages for you in python, and you call them via some json request or similar
see this project, looks promising: https://github.com/tgalal/yowsup
it seems like the only viable option so far, as everything else was shut down or has a high probability to get your account banned
see discussion here:
https://stackoverflow.com/a/46635985/533426

QuickBooks PHP DevKit connection errors

I've been trying to sync QB-Web Connector but I keep receiving errors:
The first was: 'Fatal error: Class 'QuickBooks_Loader' not found', which I fixed by adding including the file location at the top of the page: include_once("$_SERVER[DOCUMENT_ROOT]/qb/QuickBooks/Loader.php");
Now I am getting the following error from the web-connector:
QBWC1012: Authentication failed due to following error message. Client
found response content type of 'text/html', but expected 'text/xml'.
The request failed with an empty response. See QWCLog for more
details. Remember to turn logging on.
I checked all the files to be sure that the content type is text/xml, still no dice. Please help.
<?php
/**
* Example of generating QuickBooks *.QWC files
*
* #author Keith Palmer <keith#consolibyte.com>
*
* #package QuickBooks
* #subpackage Documentation
*/
// Error reporting...
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
/**
* Require the utilities class
*/
require_once '../QuickBooks.php';
$name = 'My QuickBooks SOAP Server'; // A name for your server (make it whatever you want)
$descrip = 'An example QuickBooks SOAP Server'; // A description of your server
$appurl = 'https://www.domain.com/qb/QuickBooks/SOAP/Server.php'; // This *must* be httpS:// (path to your QuickBooks SOAP server)
$appsupport = 'https://www.domain.com'; // This *must* be httpS:// and the domain name must match the domain name above
$username = ''; // This is the username you stored in the 'quickbooks_user' table by using QuickBooks_Utilities::createUser()
$fileid = '57F3B9B6-86F1-4FCC-B1FF-966DE1813D20'; // Just make this up, but make sure it keeps that format
$ownerid = '57F3B9B6-86F1-4FCC-B1FF-166DE1813D20'; // Just make this up, but make sure it keeps that format
$qbtype = QUICKBOOKS_TYPE_QBFS; // You can leave this as-is unless you're using QuickBooks POS
$readonly = false; // No, we want to write data to QuickBooks
$run_every_n_seconds = 600; // Run every 600 seconds (10 minutes)
// Generate the XML file
$QWC = new QuickBooks_WebConnector_QWC($name, $descrip, $appurl, $appsupport, $username, $fileid, $ownerid, $qbtype, $readonly, $run_every_n_seconds);
$xml = $QWC->generate();
// Send as a file download
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="my-quickbooks-wc-file.qwc"');
print($xml);
exit;
The URL you're using:
$appurl = 'https://www.domain.com/qb/QuickBooks/SOAP/Server.php';
Is not the right URL.
If you reference the quick start guide you'll note that it has to point the Web Connector to this script:
docs/example_web_connector.php
That is the script that should be in your AppURL. The example script is an actual SOAP endpoint which will allow connections from the Web Connector. You've instead pointed the Web Connector to one of the library files which does nothing on it's own, and which you shouldn't need to touch or reference anywhere.
Incidentally, this is also the reason you've had to add that include_once line as well - you won't need that if you have the Web Connector pointed to the right place.

Fatal error: Class 'Services_Twilio' not found

I'm trying to send a text message when a form is submitted using Twilio's test account however I'm receiving the following error: 'Fatal error: Class 'Services_Twilio' not found'
My code is as follows:
} else{
//PHP native
mail( $to_guest, $subject, $html_text, $header_guest);
// Outlet Admin notification email
mail( $to_admin, $subject, $notification_text, $header_admin);
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require ('http://xzenweb.co.uk/reservation/web/twilio-php/Services/Twilio.php');
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "";
$AuthToken = "";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+44463463241" => "Andrew Charlton",
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"YYY-YYY-YYYY",
// the number we are sending to - Any phone number
+44463463241,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas!"
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
}
I've left my account ID and authtoken out of the above.
What is the problem?
so you use require.
According to the documentation and the one of include the require opens a file and evals the code behind it.
what it does in the way you use it, is the following: fetch the file from http://xzenweb.co.uk/reservation/web/twilio-php/Services/Twilio.php (whitch returns nothing at all) and evals id (evals nothing).
This is why your require fails.
If you want to require/include that piece of coe, you should download the sourcecode from http://xzenweb.co.uk/reservation/web/twilio-php/Services/Twilio.php and put it in the environment of your code.
Bit if i understand Twilio right, they are used as an rest webservice.
Accodring to the documentaton http://www.twilio.com/docs/api/rest/sending-messages
you want to download the rest-client-code (like described here https://www.twilio.com/docs/php/install) and then it all come to you ;-)

Categories