How to send a VCard SMS to a cellphone from a PHP website

SMS messages sent to cellphones can contain text and other content, such as ringtones, operator logos, pictures and network settings. One of the most popular content formats is the VCard SMS. A VCard SMS is basically a business card, that stores the name and contact information of a person. If a VCard SMS is received on a mobile phone it can be saved in the phone's addressbook with a single click. The VCard SMS format is supported by all major mobile phone manufacturers. In this guide you can find an example on how to send a VCard SMS to a mobile phone from a PHP website.

Prerequisites

In order to get this example working, you need to have a webserver with PHP support. You also need to have Ozeki NG SMS Gateway installed and configured. Ozeki NG can send SMS messages using a GSM phone or GSM modem attached to your computer with a phone to pc data cable or through an IP SMS connection, such as an SMPP, UCP, CIMD2 connection. The PHP enabled working webserver and Ozeki NG SMS Gateway does not have to be installed on the same computer.

How to send a VCard SMS from a PHP website?

The example you can find here contains an HTML file and a PHP script (Figure 1). It works the following way. The website visitor downloads the HTML file in his browser, he then fills in the HTML form then submits it by clicking OK. The HTML form is submitted to the PHP script. The PHP script creates an HTTP request, that can send the VCard SMS through the HTTP SMS API of Ozeki NG SMS Gateway. This architecture is simalar to the setup we use in the Send SMS from a PHP Website guide. That guide has a video tutorial that can help you get started.

sending sms from a website
Figure 1 - Send SMS from a website

The first step in setting up this system, is to Install the Ozeki NG SMS Gateway to your computer and to verify, that you can send SMS messages from the gateway manually. After your SMS gateway is functioning you can create the HTML Form and the PHP script.

Create the HTML Form for sending the VCard SMS

Please save the send_vcard_sms.html file into the WWW directory of your webserver. The send_vcard_sms.html file contains the HTML Form that can be used to post the details of the VCard business card. Here is the source code of the send_vcard_sms.html file:

C:\www\send_vcard_sms.html

<html>
<body>
<h1>My VCard SMS form</h1>
<form method=post action='send_vcard.php'>
<table border=0>
<tr>
     <td>Recipient phone number</td>
     <td><input type='text' name='recipient'></td>
</tr>
<tr>
    <td>Family Name: </td>
    <td><input type="text" name="FAMILYNAME" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Given Name: </td>
    <td><input type="text" name="GIVENNAME" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Phone (Home): </td>
    <td><input type="text" name="PHONEHOME" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Phone (Work): </td>
    <td><input type="text" name="PHONEWORK" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Mobile Phone (Personal): </td>
    <td><input type="text" name="MOBILEHOME" value="" SIZE="40"></td>
</tr>

<tr>
    <td>Mobile Phone (Work): </td>
    <td><input type="text" name="MOBILEWORK" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Fax: </td>
    <td><input type="text" name="FAX" value="" SIZE="40"></td>
</tr>
<tr>
    <td>E-Mail: </td>
    <td><input type="text" name="EMAIL" value="" SIZE="40"></td>
</tr>
<tr>
    <td>Homepage URL: </td>
    <td><input type="text" name="HPURL" value="" SIZE="40"></td>
</tr>
<tr>
    <TD align="left" valign="top">Birthday: </td>
    <td>
        <table>

            <tr>
                <td>
                    Year:
                </td>
                <td align="right">
                    <input type="text" name="BYEAR" value="1977" maxlength="4" SIZE="4">
                </td>
                <td>
                    Month:
                </td>

                <td align="right">
                    <input type="text" name="BMONTH" value="01" maxlength="2" SIZE="2">
                </td>
                <td>
                    Day:
                </td>
                <td align="right">
                    <input type="text" name="BDAY" value="16" maxlength="2" SIZE="2">
                </td>
            </tr>
        </table>
    </td>
</tr>
<tr>
    <td>Note: </td>
    <td><input type="text" name="NOTE" value="" SIZE="40"></td>
</tr>
<tr>
    <td> </td><td><input type=submit VALUE="Send"></td>
</tr>
</table>
</form>
</body>
</html>

Please save this HTML file into the wwwroot directory of your webserver. After you have saved it you can verify it by opening it in a webbrowser.

Create the PHP script to process the HTML Form of VCard SMS

As you can see the form target in this HTML form is the send_vard.php file. The send_vard.php file is a php script, that will accept the form data sent from the send_vard_sms.html HTML page and will submit the VCard SMS message through an HTTP request to Ozeki NG SMS Gateway.

The sourcecode of the PHP SMS Vcard script is here:

C:\www\send_vard.php

<?php

########################################################
# Login information for the SMS Gateway
########################################################

$ozeki_user = "admin";
$ozeki_password = "abc123";
$ozeki_url = "http://127.0.0.1:9501/api?";

########################################################
# Functions used to send the SMS message
########################################################
function httpRequest($url){
    $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    preg_match($pattern,$url,$args);
    $in = "";
    $fp = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    if (!$fp) {
       return("$errstr ($errno)");
    } else {
        $out = "GET /$args[3] HTTP/1.1\r\n";
        $out .= "Host: $args[1]:$args[2]\r\n";
        $out .= "User-agent: Ozeki PHP client\r\n";
        $out .= "Accept: */*\r\n";
        $out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
           $in.=fgets($fp, 128);
        }
    }
    fclose($fp);
    return($in);
}

function ozekiSend($phone, $messagetype, $messagedata, $debug=false){
      global $ozeki_user,$ozeki_password,$ozeki_url;

      $url = 'username='.$ozeki_user;
      $url.= '&password='.$ozeki_password;
      $url.= '&action=sendmessage';
      $url.= '&messagetype='.$messagetype;
      $url.= '&recipient='.urlencode($phone);
      $url.= '&messagedata='.urlencode($messagedata);

      $urltouse =  $ozeki_url.$url;
      if ($debug) { echo "Request: &lzbr>$urltouse&lzbr>&lzbr>"; }

      //Open the URL to send the message
      $response = httpRequest($urltouse);
      if ($debug) {
           echo "Response: &lzbr>&lzpre>".
           str_replace(array("&lz",">"),array("<",">"),$response).
           "&lz/pre>&lzbr>"; }

      return($response);
}

########################################################
# GET data from sendsms.html
########################################################

$phonenum = $_POST['recipient'];
$messagetype = "SMS:VCARD";
$messagedata =
"BEGIN:VCARD\r\n".
"VERSION:2.1\r\n".
"N:".$_POST['FAMILYNAME'].";".$_POST['GIVENNAME']."\r\n".
"TEL;VOICE;HOME:".$_POST['PHONEHOME']."\r\n".
"TEL;VOICE;WORK:".$_POST['PHONEWORK']."\r\n".
"TEL;CELL:".$_POST['MOBILEHOME']."\r\n".
"TEL;CELL;WORK:".$_POST['MOBILEWORK']."\r\n".
"TEL;FAX:".$_POST['FAX']."\r\n".
"EMAIL:".$_POST['EMAIL']."\r\n".
"URL:".$_POST['HPURL']."\r\n".
"BDAY:".$_POST['BYEAR'].$_POST['BMONTH'].$_POST['BDAY']."\r\n".
"NOTE:".$_POST['NOTE']."\r\n".
"END:VCARD\r\n";

$debug = true;

ozekiSend($phonenum,$messagetype,$messagedata,$debug);

?>

Once you have both: send_vcard_sms.html and send_vard.php saved into the wwwroot directory of your webserver, you can submit an SMS to the gateway. On a successful send the SMS gateway will return the message reference number, that can be used to track the message in the log file.

Using a similar approach you can send other message types. Please check the mobile SMS message type formats page to get the message type names and the appropriate syntax.

More information