Skip to content

Instantly share code, notes, and snippets.

@shin1x1 shin1x1/gist:7006593
Last active Aug 30, 2016

Embed
What would you like to do?
2013/10/17 Twilio API 勉強会でのハンズオン内容です。

Composer インストール

$ mkdir yourdir
$ cd yourdir
$ curl -s http://getcomposer.org/installer | php

$ ls
composer.phar

インストール

$ php composer.phar require twilio/sdk dev-master

$ ls
composer.json composer.lock composer.phar vendor

サンプル(sample.php)

<?php
require_once __DIR__.'/vendor/autoload.php';

$xml = new Services_Twilio_Twiml();
$xml->say('Hello!');

header('Content-type: text/xml; charset=utf-8');
echo $xml;

http://example.com/yourdir/sample.php

電話をかける(inbound.php)

<?php
require_once __DIR__.'/vendor/autoload.php';

$xml = new Services_Twilio_Twiml();
$xml->say('こんにちは!', array('language' => 'ja-jp'));

header('Content-type: text/xml; charset=utf-8');
print $xml;

http://example.com/yourdir/inbound.php

簡易IVR(inbound.php)

<?php
require_once __DIR__.'/vendor/autoload.php';

$xml = new Services_Twilio_Twiml();

if (empty($_POST['Digits'])) {
  $digit = null;
} else {
  $digit = (integer)$_POST['Digits'];
}

if ($digit == 1) {
  $xml->say('1を押しました。', array('language' => 'ja-jp'));
} else {
  $xml->say('こんにちは!1 を押して下さい。', array('language' => 'ja-jp'));
}

$xml->gather(array('numDigits' => 1, 'timeout' => 30));

header('Content-type: text/xml; charset=utf-8');
echo $xml;

http://example.com/yourdir/inbound.php

電話をかける(outbound.php)

<?php
require_once __DIR__.'/vendor/autoload.php';

$sid = 'xxx';
$token = 'xxx';

$client = new Services_Twilio($sid, $token);

$ret = $client->account->calls->create(
    '+81-xx-xxxx-xxxx', // From number(Twilio)
    '+81-xx-xxxx-xxxx', // To number(Your phone number)
    'http://demo.twilio.com/docs/voice.xml'
);

var_dump($ret->sid);

http://example.com/yourdir/outbound.php

@bouzuya

This comment has been minimized.

Copy link

bouzuya commented Oct 18, 2013

ハンズオンを Ruby で書いていたので、そのコードを公開しておきます。
Ruby / Sinatra 力が低いので、コードが妥当かは知りません。

https://gist.github.com/bouzuya/7045058

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.