メニュー

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

The Twilio PHP ヘルパーライブラリー

Twilio PHP SDKを使用すると、PHPアプリケーションから簡単にTwilioのAPIとやり取りできるようになります。 最新バージョンのTwilio PHP SDKはPackagistからご利用いただけます。 Twilio SDKにはPHPバージョン 5.3以降が必要です。 Twilio PHP SDKのバージョン4.xから、より新しいバージョン5.xへの移行をお考えの場合は、こちらのガイドをご参照ください。

Composerの使用したインストール

SDKのインストールには、Composerを使用する方法が推奨されます。 composer.jsonファイルにPHP SDKを追加できますが、そのためにはrequireコマンドを使用します。

composer require twilio/sdk

If you are using a framework like Laravel, the Twilio SDK may be automatically loaded for you and ready to use in your application. If you're using Composer in an environment that doesn't handle autoloading, you can require the autoload file from the "vendor" directory created by Composer if you used the install command above. Here is a basic example of using the SDK to send a text message.

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/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 = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$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
    '+15558675309',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => 'Hey Jenny! Good luck on the bar exam!'
    )
);

Composerを使わずにインストール

While we recommend using a package manager to track the dependencies in your application, it is possible to download and use the PHP SDK manually. You can download the full source of the PHP SDK from GitHub, and browse the repo if you would like. To use the SDK in your application, unzip the SDK download file in the same directory as your PHP code. In your code, you can then require the autoload file bundled with the SDK.

<?php
// 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/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 = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$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
    '+15558675309',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => "Hey Jenny! Good luck on the bar exam!"
    )
);

Using This Library

Authenticate Client

<?php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

Create a New Record

<?php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

$call = $client->calls->create(
    "+14155551212", "+14158675310",
    array("url" => "http://demo.twilio.com/docs/voice.xml")
);

echo $call->sid;

Get Existing Record

<?php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->calls("CA42ed11f93dc08b952027ffbc406d0868")->fetch();
echo $call->to;

Iterate Through Records

<?php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);

// Loop over the list of calls and echo a property for each one
foreach ($client->calls->read() as $call) {
    echo $call->direction;
}

追加ドキュメント

Once you're up and running with the PHP SDK, you'll find code samples supporting the latest version of the library in our REST API reference docs and in the docs for every Twilio product. In addition to the REST API reference, auto-generated PHP API documentation can be found here.

トラブルシューティング

On Windows, you may receive an error "SSL certificate problem: self signed certificate in certificate chain". Please refer to the Twilio Help Center for instructions to fix.

バージョン4.xのSDKを使用する

PHP SDKのもっとも最近のバージョンは、以前のTwilioアプリケーションで使用されている可能性のあるPHP SDKの前バージョンである4.xと互換性がありません。 旧バージョンは引き続き機能し、引き続きドキュメント全体にわたってサンプルコードをご利用いただけます。 このバージョンのインストールが必要な場合、下記コマンドで行うことができます。

composer require twilio/sdk:4.11.0

代替手段として、こちらからGitHub上の旧バージョンのSDKを直接ダウンロードすることができます。

お問い合わせ

We'd love to hear your feedback on the PHP SDK, and help you past any issues you may encounter. Feel free to drop us a line, and we'll make sure to get you sorted!

Rate this page:

ヘルプが必要ですか?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.