メニュー

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?

Cを使用してSMSおよびMMS(日本未対応)を送信する

Today we'll use C, libcurl, and some POSIX APIs to send SMS and MMS messages using the Twilio REST API. Outside of a (at least mostly) POSIX-compliant build environment, you'll also need libcurl for the code in this guide.

はじめましょう!

Sign up for a Twilio Account (or Sign In)

Before exercising the Twilio API, you'll need to either create an account, or login to an existing account.

Don't have an account? Sign up for a free Twilio trial.

SMSを送信可能な電話番号を検索または購入する

This demo requires a Twilio phone number with SMS (and MMS) capabilities. You can either use a phone number that you already have, or purchase a new one with SMSes enabled.

SMS送信可能な電話番号は、以下のように電話番号一覧に表示されます:

SMS機能に対応したTwilio電話番号を確認する

And here's how you can search for a new number with SMS (or MMS, if you select it as well) capabilities:

SMS対応Twilio電話番号を購入する

C言語によるSMSの送信ガイドの前提条件

This guide targets POSIX Compliant environments, and was tested with clang-800.0.42.1 on Mac OSX 10.11, gcc 4.9.2 on Raspbian (kernel 4.4.34-v7+) and gcc 5.4.0 in Cygwin 2.877 on Windows 7 64-Bit. At a minimum, we'll need:

libcurl was available on the Mac already. On the Raspberry Pi, you can install a suitable version with:

sudo apt-get install libcurl4-openssl-dev

In Windows 7, I was able to install libcurl with Cygwin Setup:

Installing libcurl-devel and curl in Cygwin

c_smsの構築

Here's our example Makefile, which was usable in all three environments.

On most *NIX-type systems (including in Cygwin), building is probably as easy as:

git clone https://github.com/TwilioDevEd/twilio_c_sms.git
cd twilio_c_sms
make

You might need to make minor edits to the Makefile to build c_sms in your environment. Most commonly, you'll change the library search paths or the compiler.

Send an SMS or MMS Message with C

We've just built c_sms, which can be called from the command line and will attempt to send an SMS or MMS before exiting. twilio.c includes a function, twilio_send_message, which is easily adaptable to your own codebase (of course, you will need to add validation and safety before using it in production. For example, note that as is you can add more parameters through the variables!).

twilio_send_messageの動作の仕組みを見てみましょう。

  • First, we prepare our account credentials and HTTP Parameters to be consumed by the Twilio API.
  • Second, we need to prepare libcurl to execute a HTTP POST to the Twilio API. The curl_easy_setopt function calls instruct libcurl to post with the url and body we set, and use our Account and Auth Token details.
  • 3に、POSTを実行し下記の行で後始末をします:
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);​
    
  • 4番目に、libcurlまたはTwilio APIから何らかのエラーがないかどうかチェックします。

以下、Cによるtwilio_send_messageのコードになります:

        
        
        
        
        必要なHTTPパラメーターと認証をセットアップし、libcurlを準備し、Twilio REST APIにPOSTし、そして応答を確認します。

        Using libcurl to Send a Message with C and Twilio

        必要なHTTPパラメーターと認証をセットアップし、libcurlを準備し、Twilio REST APIにPOSTし、そして応答を確認します。

        c_smsを実行する

        Once c_sms is built, calling it from the command line is easy. Simply pass in your account credentials, from & to numbers, and a message body and you'll be lighting up a phone. Here's an example session:

        account_sid=ACXXXXXXXXXXXXXXXXXXXXXX
        auth_token=your_auth_token
        bin/c_sms -a $account_sid -s $auth_token -t "+18005551212" -f "+18005551213" -m "Hello, World!"
        # Optionally, use '-p http://some/path/to/img.jpg' to send an MMS.
        

        Do You See What I C? Time For Your Own Creation

        45+ years and billions of lines of code later, it's obvious that Dennis Ritchie designed a language to go the distance. Now Twilio can come with you on your C-journey with just a few helper libraries and your SMS and MMS use case.

        We hope this is a good starting point; please let us know what you build on Twitter!

        Paul Kamp Maylon Pedroso Kat King
        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.