【问题标题】:Call Twiml Object in Codeigniter - Twilio service在 Codeigniter 中调用 Twiml 对象 - Twilio 服务
【发布时间】:2017-12-07 04:09:30
【问题描述】:

我正在将 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

我尝试到处搜索,但找不到。任何人都可以帮助我。

【问题讨论】:

标签: php codeigniter twilio twilio-twiml


【解决方案1】:

另一种方法

下载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 使用的自定义方法

Link is Here

你必须使用$this-&gt;btwilio,因为我们已经实例化了这个

【讨论】:

  • 我想知道我是否可以使用 Twiml。但你的方法是一个很好的方法。谢谢
  • 请检查更新后的代码。我已经实现了 TwiML 服务。
  • 谢谢。这是我需要的!
猜你喜欢
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多