How to send sms using CYTA (Cyprus) web sms api, with php

I have recently come across CYTA (Cyprus) web sms api. It has been around for a while from what I can understand. Don’t confuse this with Web Sms (previously cybee if I am not mistaken) which was around since forever. It uses a RESTful api, in which you can programmatically send an sms from your application or web application/website using your preferred programming language. Well, not really any language since the sample code has examples only for 3 programming languages, and all are Microsoft’s. By the way, as of this writing, and to my knowledge, this is the only source code available in php to accomplish this task. The documentation provides useful information, although I don’t find it to be fully documented.

So, time to dive in. First, you should know that you need a mobile phone number from cyta, if you want to use this service. Then, you need to sign up, if you don’t already have an account. After you sign in go here to view and manage your settings. You should take notice of your secret key, as you will be needing it later. Before moving on, I should also inform you that you are allowed 10 free sms per day. After that, your phone number, (which you used to sign up) will be charged 0.02 euro / sms. Each time you can sent up to 500 sms. Of course, you can only sent sms to phone numbers in Cyprus. International mobile numbers are not allowed.

Before viewing the code, you should read the documentation to have an idea how it works and what the server responses mean. So here is my php code.


   $url = "https://www.cyta.com.cy/cytamobilevodafone/dev/websmsapi/sendsms.aspx";
   $xml = file_get_contents("body.xml");

   $headers = array(
      "POST HTTP/1.1",
      "Content-type: application/xml; charset=\"utf-8\"",
      "Content-length: " . strlen($xml),
      "Connection: close",
   );

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
   curl_setopt($ch, CURLOPT_POSTFIELDS,  $xml);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   $data = curl_exec($ch);
   echo $data;
   curl_close($ch);

The code is pretty straight forward. We set cyta’s server url, read additional information from an xml file, and set the headers. Then, we use curl to send the request. Below is the xml file.


<?xml version="1.0" encoding="UTF-8"?>
 <websmsapi>
 	<version>1.0</version>
 	<username>YOUR_USERNAME</username>
 	<secretkey>YOUR_SECRETKEY</secretkey>
 	<recipients>
 		<count>1</count>
 		<mobiles>
 			<m>9XXXXXXX</m>
 		</mobiles>
 	</recipients>
 	<message>Hello from Cyta web sms api</message>
 	<language>en</language>
 </websmsapi>

A few things to notice here are obviously your username and secret key. You get these from your account page. The number of recipients must match the number of mobile phones listed. The server will respond with an xml indicating an error code or a success. You can view the xml in your browser’s developer tools console. Your browser tab will only show the message or code. For example the browser will show this.


0A5PGRBLYWQ

But if you dive to the developer console, in the elements tab (in chrome) you can view the source which if you expand it will be this.


<!--?xml version="1.0" encoding="UTF-8" ?-->
<html>
<head><meta name="chromesniffer" id="chromesniffer_meta" content="{}"><script type="text/javascript" src="chrome-extension://homgcnaoacgigpkkljjjekpignblkeae/detector.js"></script>
</head>
<body cz-shortcut-listen="true"><websmsapi><status>0</status><lot>A5PGRBLYWQ</lot></websmsapi>
<div id="window-resizer-tooltip"><a href="#" title="Edit settings"></a><span class="tooltipTitle">Window size: </span><span class="tooltipWidth" id="winWidth"></span> x <span class="tooltipHeight" id="winHeight"></span><br><span class="tooltipTitle">Viewport size: </span><span class="tooltipWidth" id="vpWidth"></span> x <span class="tooltipHeight" id="vpHeight"></span></div>
</body>
</html>

Here, a status of 0 means success! This is of course a proof of consept code. Normally you would wrap this up in a function and place it in your custom functions php file. Then you would include that in your main project and call the function. You would also had to automatically create the xml file with the appropriate phone numbers. The source code is up on github. Feel free to browse and use it in your own projects. If you have any questions or comments, please use the comments below. I hope this will be helpful to someone. I would also like to know if anyone has implemented this in another language or framework, so please let me know!

2 comments

  1. Tatiana · December 6, 2014

    Is there any chance to use it in Greece???We have a Cyta Mobile Phone and we have 1000 free sms per month.
    Thanks in advance
    Tatiana

    • Sotiris Karagiorgis · December 6, 2014

      To use this API, you need to be a registered member of http://www.cyta.com.cy. Once you have registered and been activated you will receive 10 free sms per day while the rest of the sms you sent will be charged to the (cypriot) mobile number you registered. The recipients must also use cypriot phone numbers, as international ones are prohibited. Therefore, you cannot use it with greek phone numbers.
      Thank you for your comment!