My requirement is sending sms while place order button triggered.
1) Locally I have use one predefined sms gateway server
I receive message When I run http://localhost/send-sms.php, its not a magento way its fully core php.
2) I have set one event/observer when place order get trigger
I could try with copy the send-sms.php content to observer execute method, also copy the php library to the same Observer Class location but it is not work.
Observer class execute method
public function execute(\Magento\Framework\Event\Observer $observer)
{
require "twilio-php-master/Services/Twilio.php";
$AccountSid = "XXXXXXXXXXXXXXXXXXX";
$AuthToken = "XXXXXXXXXXXXXXXXXXXX";
$client = new Services_Twilio($AccountSid, $AuthToken);
$message = $client->account->messages->create(array(
"From" => "YYYYYYYYY",
"To" => "YYYYYYYYYY",
"Body" => "Test message!",
));
echo "Sent message {$message->sid}";
}
problem is magento get not recognize require URL. (first line of the execute method)
require 'twilio-php-master/Services/Twilio.php';
$client = new Services_Twilio($AccountSid, $AuthToken);
Services_Twilio class defined in Twilio.php, localy its work when I move to magento 2. I stuck with this two lines.
Where could I copy the twilio-php-master (php libreary)?
1 Answer 1
To my mind the right solution should be like this:
- Add
twilio/sdk(link on github and link on packagist) at your extension dependencies in thecomposer.json. - Create a model where you'll have the opportunity to write a code for sms sending (copy this part of code from the
send-sms.php). - Use this model in your observer in order to send sms.
PS: Do not forget to run setup:upgrade to load the twilio/sdk library
UPDATE:
In the Magento root directory you should execute the command (in the terminal):
composer require twilio/sdk
Then a composer adds the twilio library in the vendor folder and generates the autoloader for it:
twilio in the vendor directory
After that you can call the Services_Twilio class without any require (thanks to autoloader) like this:
$AccountSid = "XXXXXXXXXXXXXXXXXXX";
$AuthToken = "XXXXXXXXXXXXXXXXXXXX";
$client = new \Services_Twilio($AccountSid, $AuthToken);
$message = $client->account->messages->create(array(
"From" => "YYYYYYYYY",
"To" => "YYYYYYYYYY",
"Body" => "Test message!",
));
echo "Sent message {$message->sid}";
Note: do not forget to add "\" before the class name.
Result in debug:
As you can see, the Twilio class calling (reference) happens without any errors.
In addition, you can add dependence in the composer.json file of the module, like this:
{
"name": "vendor/module-module",
"description": "Sample Module",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/module-ui": "100.0.*",
"magento/magento-composer-installer": "*",
"twilio/sdk": "~4.10"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Vendor\\Module\\": ""
}
}
}
But it will work only at installation of the module through the composer.
-
thanks, I am working on your solution, I will be back to you.Bilal Usean– Bilal Usean2016年04月26日 15:35:02 +00:00Commented Apr 26, 2016 at 15:35
-
I try this, please explain bit more. I stuck with require keyword in php. I have updated my question.Bilal Usean– Bilal Usean2016年04月27日 04:41:08 +00:00Commented Apr 27, 2016 at 4:41
-
@BilalUsean I've updated my answerSiarhey Uchukhlebau– Siarhey Uchukhlebau2016年04月27日 07:31:46 +00:00Commented Apr 27, 2016 at 7:31
-
wow its working @Siarhey. I felt bad, I able to upvote you only one time :-) otherwise destroy upvote :-)Bilal Usean– Bilal Usean2016年04月27日 08:21:06 +00:00Commented Apr 27, 2016 at 8:21