在 Codeigniter 中调用 Twiml 对象 - Twilio 服务
Call Twiml Object in Codeigniter - Twilio service
我正在将 twilio 服务集成到 codeigniter 中。但目前,我不知道如何调用 Twiml 对象。在原始 PHP 中,我可以这样编码:
use Twilio\Twiml;
$response = new Twiml();
但是在 codeigniter 中,我只能调用库 twilio 服务(我把它放在库文件夹中)而不能在控制器中使用 Twiml 对象。
$this->load->library( 'twilio_services' );
这是我下载 twilio 服务的地方:
https://github.com/t1gr0u/codeigniter-twilio/tree/master/libraries
我试着到处搜索,但找不到。任何人都可以帮助我。
另一种方法
下载Twilio PHP SDK并放入文件夹third_party
复制此文件并放入库文件夹中 Btwilio.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'third_party/twilio/Twilio/autoload.php' ;
use Twilio\Rest\Client;
use Twilio\TwiML ; // TwiML used
class Btwilio {
private $btwilio ;
public function __construct()
{
$AccountSid = "AC008e63cc03eec3be4b1cfe7ab80478a0";
$AuthToken = "82496aa3278d9cbebfc0f24ae1c1ba7f";
$this->btwilio = new Client($AccountSid, $AuthToken);
$this->btwiml = new Twiml();
}
public function sendsms()
{
$people = array(
"+919048XXXXXX" => "Rajeev"
);
// 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 = $this->btwilio->account->messages->create(
// the number we are sending to - Any phone number
$number,
array(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased
'from' => "+18559063122",
// the sms body
'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
)
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
}
public function sendtwiml()
{
// $this->btwiml = new Twiml(); use $this->btwiml to use Twiml services
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
//$response = new Twiml();
$this->btwiml->sms('The king stay the king.', ['from' => '+14105551234',
'to' => '+919048309695']);
print_r($this->btwiml) ;
}
}
/* End of file Btwilio.php */
/* Location: ./application/libraries/Btwilio.php */
像这样在你的控制器上调用这个库
$this->load->library('btwilio');
$this->btwilio->sendsms() ; // method created on Btwilio library
$this->btwilio->sendtwiml() ; // method of TwiML example
在此库中创建可从官方 Twilio 使用的自定义方法
你必须使用 $this->btwilio
因为我们已经实例化了这个
我正在将 twilio 服务集成到 codeigniter 中。但目前,我不知道如何调用 Twiml 对象。在原始 PHP 中,我可以这样编码:
use Twilio\Twiml;
$response = new Twiml();
但是在 codeigniter 中,我只能调用库 twilio 服务(我把它放在库文件夹中)而不能在控制器中使用 Twiml 对象。
$this->load->library( 'twilio_services' );
这是我下载 twilio 服务的地方: https://github.com/t1gr0u/codeigniter-twilio/tree/master/libraries
我试着到处搜索,但找不到。任何人都可以帮助我。
另一种方法
下载Twilio PHP SDK并放入文件夹third_party
复制此文件并放入库文件夹中 Btwilio.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'third_party/twilio/Twilio/autoload.php' ;
use Twilio\Rest\Client;
use Twilio\TwiML ; // TwiML used
class Btwilio {
private $btwilio ;
public function __construct()
{
$AccountSid = "AC008e63cc03eec3be4b1cfe7ab80478a0";
$AuthToken = "82496aa3278d9cbebfc0f24ae1c1ba7f";
$this->btwilio = new Client($AccountSid, $AuthToken);
$this->btwiml = new Twiml();
}
public function sendsms()
{
$people = array(
"+919048XXXXXX" => "Rajeev"
);
// 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 = $this->btwilio->account->messages->create(
// the number we are sending to - Any phone number
$number,
array(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased
'from' => "+18559063122",
// the sms body
'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
)
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
}
public function sendtwiml()
{
// $this->btwiml = new Twiml(); use $this->btwiml to use Twiml services
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
//$response = new Twiml();
$this->btwiml->sms('The king stay the king.', ['from' => '+14105551234',
'to' => '+919048309695']);
print_r($this->btwiml) ;
}
}
/* End of file Btwilio.php */
/* Location: ./application/libraries/Btwilio.php */
像这样在你的控制器上调用这个库
$this->load->library('btwilio');
$this->btwilio->sendsms() ; // method created on Btwilio library
$this->btwilio->sendtwiml() ; // method of TwiML example
在此库中创建可从官方 Twilio 使用的自定义方法
你必须使用 $this->btwilio
因为我们已经实例化了这个