PHP Getting Started

Back

zang-php


This PHP library is an open source tool built to simplify interaction with the Zang telephony platform. Zang makes adding voice and SMS to applications fun and easy.

For this libraries full documentation visit: http://zang.github.com/zang-cloud/zang-php

For more information about Zang visit: https://zang.io/products/cloud or http://docs.zang.io/aspx/docs


Installation


Via Pear

At the moment we don't support the PEAR package but will in the near future!

PHP 5.2+ Required (5.3+ recommended)

Via GitHub clone

Access terminal and run the following code:

  $ cd ~
  $ git clone https://github.com/zang-cloud/zang-php
  $ cd zang-php

Via Download

Step 1

Download the .zip file.

Step 2

Once the .zip download is complete, extract it and get started with the examples below.


Usage


REST

Zang REST API documentation

Send SMS Example

# First we must import the actual Zang library
require_once '../connectors/Sms.php';

# If an error occurs, Zang_Exception will be raised. Due to this,
# it's a good idea to always do try/catch blocks while querying ZangAPI
try {
    # Now what we need to do is instantiate the library
    # If Credentials are set in /configuration/application.config.php application 
    # is going to use this thata. If is nessesary credentals can be overriden as is shown below
    $sms = Sms::getInstance();

    # This is the best approach to setting multiple options recursively
    # Take note that you cannot set non-existing options
    # Credentials can be set in /configuration/application.config.php and be used 
    # as is shown below or could be set to some custom value
    $sms -> setOptions(array(
        "account_sid"   => $_ENV["ACCOUNT_SID"],
        "auth_token"    => $_ENV["AUTH_TOKEN"],
    ));

    # NOTICE: The code below will send a new SMS message.

    # Zang_Helpers::filter_e164 is a internal wrapper helper to help you work with phone numbers and their formatting
    # For more information about E.164, please visit: http://en.wikipedia.org/wiki/E.164
    $sentSms = $sms -> sendSms(array(
        'From'          => '(XXX) XXX-XXXX',
        'To'            => '(XXX) XXX-XXXX',
        'Body'          => "This is a test message.",
        'AllowMultiple' => "False"
    ));

    # If you wish to get back the full response object/array then use:
    echo "<pre>";
    print_r($sentSms->getResponse());
    echo "</pre>";
    # OR
    echo $sentSms
    
    # If you wish to get SID then use
    echo $sentSms->sid


} catch (ZangException $e){
    echo "<pre>";
    print_r( "Exception message: " . $e -> getMessage() . "<br>");
    print_r( "Exception code: " . $e -> getCode() . "<br>");
    print_r(  $e -> getTrace() );
    echo "</pre>";
}

InboundXML

InboundXML is an XML dialect which enables you to control phone call flow. For more information please visit the Zang InboundXML documentation

Example
<?php

# First we must import the actual Zang library
require_once "../library/ZangApi/InboundXML.php";

# If an error occurs, Zang_Exception will be raised. Due to this,
# it's a good idea to always do try/catch blocks while querying ZangAPI
try {
    # Now what we need to do is instantiate the library
    $inboundXml = new Zang_InboundXML();

    $inboundXml -> say( "Welcome to Zang. This is a sample InboundXML document.", array(
        "voice" => "male"
    ));

    # If you wish to get back validated Inbound XML as string then use:
    echo $inboundXml;


} catch (Exception $e){
    echo $e->getMessage();
    echo "<br><pre>";
    print_r( $e->getTrace() );
    echo "</pre>";
}

will render

<?xml version="1.0"?>

<Response><Say voice="male">Welcome to Zang. This is a sample InboundXML document.</Say></Response>

Just host that PHP file somewhere, buy a phone number in your Zang Account Dashboard and assign the URL of that PHP page to your new number. Whenever you dial that number, the InboundXML this page generates will be executed and you'll hear our text-to-speech engine say welcome.


Pending updates to this document

Subscribe for Updates