<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Text Marketer - Email SMS, Bulk SMS Gateway, SMS Marketing Blog &#187; PHP Classes</title>
	<atom:link href="http://www.textmarketer.co.uk/blog/category/php-5-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.textmarketer.co.uk/blog</link>
	<description>SMS Gateway</description>
	<lastBuildDate>Tue, 07 Feb 2012 09:50:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Multithreading PHP for sending SMS</title>
		<link>http://www.textmarketer.co.uk/blog/2011/03/technical/multi-threading-php-for-sending-sms/</link>
		<comments>http://www.textmarketer.co.uk/blog/2011/03/technical/multi-threading-php-for-sending-sms/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 11:16:12 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.textmarketer.co.uk/blog/?p=2239</guid>
		<description><![CDATA[On occasion we need to control a PHP script from a single script but allow it to execute several PHP instances. PHP it&#8217;s self doesn&#8217;t have any native ability to multithread, which I remember as being a bit of an irritant when I started using the language, but you can &#8230; <a href="http://www.textmarketer.co.uk/blog/2011/03/technical/multi-threading-php-for-sending-sms/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><!-- 		@page { margin: 2cm } 		P { margin-bottom: 0.21cm } 		A:link { so-language: zxx } -->On occasion we need to control a PHP script from a single script but allow it to execute several PHP instances.</p>
<p>PHP it&#8217;s self doesn&#8217;t have any native ability to <strong>multithread</strong>, which I remember as being a bit of an irritant when I started using the language,  but you can use a couple of tricks to achieve a similar result.</p>
<p>&nbsp;</p>
<p><strong>Consider this scenario</strong></p>
<p>Our SMS Gateway interface (or API) is accessed via an <a href="http://www.textmarketer.co.uk/blog/2010/03/business-sms/rest-sms-api-specification-document/" target="_blank">RESTful web service  (HTTP POST)</a> or our <a href="http://www.textmarketer.co.uk/blog/2009/05/sms-gateway/text-marketer-apigateway-spec/" target="_blank">simple HTTP GET api</a>, these services offer a simple way to integrate with us, however there are draw backs the main one being the speed at which you can send data to the interface in a serial fashion.</p>
<p>Imagine the following:  Lets assume you want to  send 10,000 text messages as fast as  possible to the api, your method would do something like the following:</p>
<ul>
<li>construct data (your fully formed HTTP request)</li>
<li>open port</li>
<li>write data</li>
<li>read response</li>
<li>close port</li>
<li>repeat 10,000 times</li>
</ul>
<p>&nbsp;</p>
<p>With PHP this process is carried out in a linear fashion in the context of a loop, now let&#8217;s look at the numbers:</p>
<p>Internet latency to the api is an assumed 10ms, which would put you somewhere in the UK</p>
<p>The &#8216;system&#8217; that is the speed at which we can read and write back to your script is .5 ms</p>
<p>Total process time is 105000 ms or 105 seconds .. but wouldn&#8217;t it be nice to go quicker??</p>
<p>&nbsp;</p>
<p>Now this is where in parallel or multi threading would come in handy, if we could kick say 5 of these processes off at once then we could send the same amount of texts in 105 / 5  which is 21 seconds and a vast improvement.</p>
<p>&nbsp;</p>
<p>So how can this be done?</p>
<p>&nbsp;</p>
<p>Say hello to the Curl Extension <a href="http://uk3.php.net/curl">http://uk3.php.net/curl</a> this isn&#8217;t necessarily included with your  vanilla build of PHP so you&#8217;ll need to compile it in. The Curl extension offers many extras for the PHP developer one of which will will use to do our multithreading.</p>
<p>&nbsp;</p>
<p>It is usually best to explain the idea by example, here &#8216;s a class called DoMultiThread that shows the basic idea</p>
<p>&nbsp;</p>
<p><span style="color: #ff0000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;">&lt;?php</span></span></span></p>
<p><span style="font-family: Monaco, monospace; font-size: xx-small;"><span style="color: #0000ff;">class </span><span style="color: #000000;">DoMultiThread {</span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">//// i&#8217;m not going to simulate a 10,000 volume send but you can get the idea!</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">//  some dummy data an array of 5 names and numbers for constructing unique  messages</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">private </span><span style="color: #660000;">$names </span><span style="color: #000000;">= </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #008200;">&#8220;name&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;peter&#8221;</span><span style="color: #000000;">, </span><span style="color: #008200;">&#8220;number&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;077122345566&#8243; </span><span style="color: #000000;">), </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #008200;">&#8220;name&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;fred&#8221;</span><span style="color: #000000;">, </span><span style="color: #008200;">&#8220;number&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;077122345567&#8243; </span><span style="color: #000000;">), </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #008200;">&#8220;name&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;susan&#8221;</span><span style="color: #000000;">, </span><span style="color: #008200;">&#8220;number&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;077122345568&#8243; </span><span style="color: #000000;">), </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #008200;">&#8220;name&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;jim&#8221;</span><span style="color: #000000;">, </span><span style="color: #008200;">&#8220;number&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;077122345569&#8243; </span><span style="color: #000000;">), </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(</span><span style="color: #008200;">&#8220;name&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;katie&#8221;</span><span style="color: #000000;">, </span><span style="color: #008200;">&#8220;number&#8221; </span><span style="color: #000000;">=&gt; </span><span style="color: #008200;">&#8220;077122345510&#8243; </span><span style="color: #000000;">) );</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">function </span><span style="color: #000000;">__contruct() {</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">/// function sets up and sends 5 simultaneous threads</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">/// returns an array of the 5 outputs</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$output </span><span style="color: #000000;">= </span><span style="color: #660000;">$this</span><span style="color: #000000;">-&gt;execute ();</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> }</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">protected function </span><span style="color: #000000;">execute() {</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$mh </span><span style="color: #000000;">= curl_multi_init ();</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$handles </span><span style="color: #000000;">= </span><span style="color: #0000ff;">array </span><span style="color: #000000;">(); </span><span style="color: #808080;">/// will hold all our handle instances</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="color: #808080; font-family: Monaco, monospace; font-size: xx-small;">/// create our 5 threads, i wouldn&#8217;t advise trying many more</span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace; font-size: xx-small;"><span style="color: #0000ff;">foreach </span><span style="color: #000000;">( </span><span style="color: #660000;">$names </span><span style="color: #0000ff;">as </span><span style="color: #660000;">$item </span><span style="color: #000000;">) {</span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// create a new single curl handle</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$curl_handle </span><span style="color: #000000;">= curl_init ();</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// our unique transaction / message</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$url </span><span style="color: #000000;">= </span><span style="color: #008200;">&#8220;http://www.textmarketer.biz/gateway/?username=user&amp;password=pass&amp;message=Hi &#8221; </span><span style="color: #000000;">. </span><span style="color: #660000;">$item </span><span style="color: #000000;">[</span><span style="color: #008200;">'name'</span><span style="color: #000000;">] . </span><span style="color: #008200;">&#8220;&amp;orig=test&amp;number=&#8221; </span><span style="color: #000000;">. </span><span style="color: #660000;">$item </span><span style="color: #000000;">[</span><span style="color: #008200;">'number'</span><span style="color: #000000;">];</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// setting several options like url, timeout, returntransfer</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_setopt ( </span><span style="color: #660000;">$curl_handle</span><span style="color: #000000;">, CURLOPT_URL, </span><span style="color: #660000;">$url </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_setopt ( </span><span style="color: #660000;">$curl_handle</span><span style="color: #000000;">, CURLOPT_HEADER, </span><span style="color: #ff0000;">0 </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_setopt ( </span><span style="color: #660000;">$curl_handle</span><span style="color: #000000;">, CURLOPT_RETURNTRANSFER, </span><span style="color: #0000ff;">true </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_setopt ( </span><span style="color: #660000;">$curl_handle</span><span style="color: #000000;">, CURLOPT_TIMEOUT, </span><span style="color: #ff0000;">30 </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// add to the curl multi handle, this allows us to do the multi threading</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_multi_add_handle ( </span><span style="color: #660000;">$mh</span><span style="color: #000000;">, </span><span style="color: #660000;">$curl_handle </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// we will need to access the indvidual handles later</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$handles </span><span style="color: #000000;">[] = </span><span style="color: #660000;">$curl_handle</span><span style="color: #000000;">;</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> }</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// execute the multi handle</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$running </span><span style="color: #000000;">= null;</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">do </span><span style="color: #000000;">{</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_multi_exec ( </span><span style="color: #660000;">$mh</span><span style="color: #000000;">, </span><span style="color: #660000;">$running </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// it&#8217;s a good idea to reduce load, here it&#8217;s 1 micro second</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> usleep ( </span><span style="color: #ff0000;">10000 </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> } </span><span style="color: #0000ff;">while </span><span style="color: #000000;">( </span><span style="color: #660000;">$running </span><span style="color: #000000;">&gt; </span><span style="color: #ff0000;">0 </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// get the content of the transaction, this will contain (in our case) things like the transaction id, credits remaining etc</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #660000;">$i </span><span style="color: #000000;">= </span><span style="color: #ff0000;">0</span><span style="color: #000000;">; </span><span style="color: #660000;">$i </span><span style="color: #000000;">&lt; count ( </span><span style="color: #660000;">$handles </span><span style="color: #000000;">); </span><span style="color: #660000;">$i </span><span style="color: #000000;">++) {</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// get the content of the handle</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #660000;">$output </span><span style="color: #000000;">[] = curl_multi_getcontent ( </span><span style="color: #660000;">$handles </span><span style="color: #000000;">[</span><span style="color: #660000;">$i</span><span style="color: #000000;">] );</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// remove the handle from the multi handle</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_multi_remove_handle ( </span><span style="color: #660000;">$mh</span><span style="color: #000000;">, </span><span style="color: #660000;">$handles </span><span style="color: #000000;">[</span><span style="color: #660000;">$i</span><span style="color: #000000;">] );</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> }</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #808080;">// close the multi curl handle to free system resources</span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> curl_multi_close ( </span><span style="color: #660000;">$mh </span><span style="color: #000000;">);</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"><span style="color: #000000;"> </span><span style="color: #0000ff;">return </span><span style="color: #660000;">$output</span><span style="color: #000000;">;</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> </span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;"> }</span></span></span></p>
<p><span style="color: #000000;"><span style="font-family: Monaco, monospace;"><span style="font-size: xx-small;">}</span></span></span></p>
<p><span style="color: #ff0000; font-family: Monaco, monospace; font-size: xx-small;">?&gt;</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=2239&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2011/03/technical/multi-threading-php-for-sending-sms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REST SMS API Code Examples</title>
		<link>http://www.textmarketer.co.uk/blog/2010/03/dev/rest-sms-api-code-examples/</link>
		<comments>http://www.textmarketer.co.uk/blog/2010/03/dev/rest-sms-api-code-examples/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 10:56:58 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[All SMS Gateway Documentation]]></category>
		<category><![CDATA[Development Blog]]></category>
		<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[sms api]]></category>
		<category><![CDATA[support]]></category>

		<guid isPermaLink="false">http://87.106.109.73/blog/?p=1157</guid>
		<description><![CDATA[This blog post has been superseded with our website documentation In our REST API specification document we saw, in general terms, how to make a request to our REST SMS API and how to deal with the response. Here we provide you with some more concrete examples that you can copy &#38; paste! &#8230; <a href="http://www.textmarketer.co.uk/blog/2010/03/dev/rest-sms-api-code-examples/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.textmarketer.co.uk/developers/restful-api.htm">This blog post has been superseded with our website documentation</a></p>
<p>In our <a title="REST SMS API specification" href="/blog/2010/03/business-sms/rest-sms-api-specification-document/" target="_self">REST API specification document</a> we saw, in general terms, how to make a request to our REST SMS API and how to deal with the response. Here we provide you with some more concrete examples that you can copy &amp; paste!</p>
<p>NOTE: You will want to add additional error checking to the examples below.</p>
<p>In each of the languages used here we create a request, send it, receive the response, and parse its contents.</p>
<p><strong>PHP</strong></p>
<p>Example for getting the number of credits:</p>
<pre>$url = 'http://www.textmarketer.biz/services/rest/credits';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&amp;password=$password";

// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo  = curl_getinfo($curlHandle);
curl_close($curlHandle);

// deal with the response
if ($responseInfo['http_code']==200)
{
	$xml_obj = simplexml_load_string($responseBody);
	$credits = (int) $xml_obj-&gt;credits;
	// do something with the result
	echo $credits;

} else {
	// handle the error
	var_dump($responseBody);
}</pre>
<p>Example for getting the list of delivery reports:</p>
<pre>$url = 'http://www.textmarketer.biz/services/rest/deliveryReports';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&amp;password=$password";

// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo  = curl_getinfo($curlHandle);
curl_close($curlHandle);

// deal with the response
if ($responseInfo['http_code']==200)
{
	$xml_obj = simplexml_load_string($responseBody);
	$atts = $xml_obj-&gt;reports-&gt;attributes();
	$num_reports = (int) $atts-&gt;quantity;

	if ($num_reports &gt; 0)
	{
		echo "$num_reports reports\n";
		$reports = array();
		foreach ($xml_obj-&gt;reports-&gt;report as $xml_report)
		{
			$atts = $xml_report-&gt;attributes();
			$name = (string) $atts-&gt;name;
			$updated = (string) $atts-&gt;last_updated;
			$reports[] = array('name'=&gt;$name, 'modified'=&gt;$updated);
		}
		// do something with the report details
		var_dump($reports);
	} else {
		echo 'No reports available';
	}
} else {
	// handle the error
	var_dump($responseBody);
}</pre>
<p>Example for getting the details of a particular delivery report:</p>
<pre>// TODO the URL will change according to the delivery report to retrieve...
$url = 'http://www.textmarketer.biz/services/rest/deliveryReport/GatewayAPI_09-02-10';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!
$url = "$url?username=$username&amp;password=$password";

// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($curlHandle);
$responseInfo  = curl_getinfo($curlHandle);
curl_close($curlHandle);

// deal with the response
if ($responseInfo['http_code']==200)
{
	$xml_obj = simplexml_load_string($responseBody);

	$reportRows = array();
	foreach ($xml_obj-&gt;report-&gt;reportrow as $xml_reportRow)
	{
		$atts = $xml_reportRow-&gt;attributes();
		$mobile = (string) $atts-&gt;mobile_number;
		$updated = (string) $atts-&gt;last_updated;
		$msgID = (int) $atts-&gt;message_id;
		$status = (string) $atts-&gt;status;
		$reports[] = array('mobile'=&gt;$mobile, 'modified'=&gt;$updated, 'messageID'=&gt;$msgID, 'status'=&gt;$status);
	}
	// do something with the report details
	var_dump($reports);

} else {
	// handle the error
	var_dump($responseBody);
}</pre>
<p>Example for sending an SMS:</p>
<pre>$url = 'http://www.textmarketer.biz/services/rest/sms';
$username = 'myAPIusername'; // CHANGE THIS!!!
$password = 'myAPIpassword'; // CHANGE THIS!!!

$data = array('message'=&gt;'hello','mobile_number'=&gt;'447777777777', 'originator'=&gt;'me',
	'username'=&gt;$username, 'password'=&gt;$password);
$data = http_build_query($data, '', '&amp;');
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlHandle, CURLOPT_POST, 1);
$responseBody = curl_exec($curlHandle);
$responseInfo  = curl_getinfo($curlHandle);
curl_close($curlHandle);

// deal with the response
if ($responseInfo['http_code']==200)
{
	$xml_obj = simplexml_load_string($responseBody);
	// do something with the result
	echo "Message ID: $xml_obj-&gt;message_id\n";
	echo "Credits used: $xml_obj-&gt;credits_used\n";

	var_dump($responseBody);

} else {
	// handle the error
	var_dump($responseInfo);
	var_dump($responseBody);
}</pre>
<p>Example for transferring credits:</p>
<pre>
<div>&lt;?php</div>
<div>/**</div>
<div> * POST request on the 'credits' resource (credit transfer)</div>
<div> */</div>
<div>$url = '<a href="http://www.textmarketer.biz/services/rest/credits" target="_blank">http://www.textmarketer.biz/services/rest/credits</a>';</div>
<div>// for testing:</div>
<div>//$url = '<a href="http://sandbox.textmarketer.biz/services/rest/credits" target="_blank">http://sandbox.textmarketer.biz/services/rest/credits</a>';</div>
<div>$username = 'myAPIusername'; // CHANGE THIS!!! - the username of the account you're transferring FROM</div>
<div>$password = 'myAPIpassword'; // CHANGE THIS!!!</div>
<div>$targetAccountNumber = 0; // CHANGE THIS!!</div>
<div></div>
<div>$data = array('quantity'=&gt;'1','target'=&gt;$targetAccountNumber,</div>
<div><span>	</span>'username'=&gt;$username, 'password'=&gt;$password);</div>
<div>$data = http_build_query($data, '', '&amp;');</div>
<div>// we're using the curl library to make the request</div>
<div>$curlHandle = curl_init();</div>
<div>curl_setopt($curlHandle, CURLOPT_URL, $url);</div>
<div>curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);</div>
<div>curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);</div>
<div>curl_setopt($curlHandle, CURLOPT_POST, 1);</div>
<div>$responseBody = curl_exec($curlHandle);</div>
<div>$responseInfo  = curl_getinfo($curlHandle);</div>
<div>curl_close($curlHandle);</div>
<div></div>
<div>// deal with the response</div>
<div>if ($responseInfo['http_code']==200)</div>
<div>{</div>
<div> // success - you may want to read in the return data here to see how many credits were  left on each account</div>
<div></div>
<div>} else {</div>
<div><span>	</span>// handle the error here</div>
<div><span>	</span>// var_dump($responseInfo);</div>
<div><span>	</span>// var_dump($responseBody);</div>
<div>}</div>

?&gt;</pre>
<p>For more details on creating <a title="REST requests in PHP" href="http://www.gen-x-design.com/archives/making-restful-requests-in-php/" target="_blank">REST requests in PHP, see here</a>.</p>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=1157&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2010/03/dev/rest-sms-api-code-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using our short code and gateway APIs to create a game</title>
		<link>http://www.textmarketer.co.uk/blog/2009/07/sms-gateway/using-our-short-code-and-gateway-apis-to-create-a-game/</link>
		<comments>http://www.textmarketer.co.uk/blog/2009/07/sms-gateway/using-our-short-code-and-gateway-apis-to-create-a-game/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 15:10:29 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[All SMS Gateway Documentation]]></category>
		<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://87.106.109.73/blog/?p=391</guid>
		<description><![CDATA[The Concept To create an interactive game using Text Messaging and to show how easy it is to create interactive experiences using your Text Marketer Account. The example uses both our short code API (to receive the customer responses) and then our  gateway API to send out the &#8216;answer&#8217;. The &#8230; <a href="http://www.textmarketer.co.uk/blog/2009/07/sms-gateway/using-our-short-code-and-gateway-apis-to-create-a-game/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Concept</h2>
<p>To create an interactive game using Text Messaging and to show how easy it is to create interactive experiences using your Text Marketer Account. The example uses both our <a title="shortcode api" href="http://www.textmarketer.co.uk/blog/2009/05/sms-gateway/text-marketer-short-code-api-options/">short code API</a> (to receive the customer responses) and then our  <a title="api spec" href="http://www.textmarketer.co.uk/blog/2009/05/sms-gateway/text-marketer-apigateway-spec/">gateway API</a> to send out the &#8216;answer&#8217;.</p>
<h2>The Game</h2>
<p><span style="font-weight: normal; font-size: 13px;"><img class="size-full wp-image-392 " title="sweets in a jar" src="http://www.textmarketer.co.uk/blog/wp-content/uploads/2009/07/clasp_jar_novelty.jpg" alt="sweets in a jar" width="186" height="220" /></span></p>
<p>Guess the number of sweets in a jar. A simple game where the participant texts in the number of sweets they think is in the jar and then receives a message indicating if they are high,  low , close or have correctly guessed to the number of sweets. <a title="game classes" href="http://www.textmarketer.biz/downloads/game_classes.txt">Download all the required classes</a>.</p>
<h2>Mechanics</h2>
<p style="text-align: center;"><img class="size-full wp-image-407 aligncenter" title="api-flow" src="http://www.textmarketer.co.uk/blog/wp-content/uploads/2009/07/api-flow.jpg" alt="api-flow" width="497" height="367" /></p>
<p>Fig 1 shows the how the basic flow of the systems works. The game is initiated by an incoming text message (as shown in orange) to our 88802 shortcode. The text message contains a keyword  in this case <strong>sweets</strong> and the data you which you need to parse.</p>
<p>Once the text message has been received in your account you can enable a trigger to be sent to your own script residing on your own server. The trigger is a GET request with the parameters added to it as per our short code documentation.</p>
<p>Your application/script can then use the data to geenrate a response. In our case we find out whether the participant has guessed the correct number and we send back an apporprate response using the Text Marketer API  Gateway see documentation for details.</p>
<h2>Creating the game and code examples</h2>
<p>The Guess number of sweets in a jar game and all the code you need can be <a title="game classes" href="http://www.textmarketer.biz/downloads/game_classes.txt" target="_blank">downloaded here</a>. It&#8217;s written in PHP 5.</p>
<p>Once you have created yourself a<a title="free sms account" href="http://www.textmarketer.co.uk/free-bulk-sms-software.htm" target="_blank"> free account</a> you need to request a <a title="free sms short code keyword" href="http://www.textmarketer.co.uk/free-autoresponse-keyword.htm" target="_blank">free shortcode keyword</a> from Text Marketer.</p>
<p>Next <a title="login to fee SMS account" href="http://www.textmarketer.co.uk/campaign/index.php" target="_blank">login into your accoun</a>t and set the url your wish to use for accessing your script. To access the set up screen, select from the menu &#8220;Incoming SMS&#8221; =&gt; &#8220;Change Reply Settings&#8221; then click &#8220;Configure&#8221; next to the keyword. You&#8217;ll then be able to paste the URL into the API URL window. Then press submit.</p>
<p>&nbsp;</p>
<p>The game is broken the code down into 4 classes.</p>
<table border="1">
<tbody>
<tr>
<td><strong>Class Name</strong></td>
<td><strong>Purpose</strong></td>
</tr>
<tr>
<td>SweetsInAJar</td>
<td>The entry point class</td>
</tr>
<tr>
<td>ExtractData</td>
<td>Parse the incoming data &amp; extract the result</td>
</tr>
<tr>
<td>MessageLogic</td>
<td>Game logic and associated response</td>
</tr>
<tr>
<td><a title="sms send class" href="http://www.textmarketer.co.uk/blog/2009/06/technical/php-5-simple-sms-send-class/">SendSMSXML</a></td>
<td>The SMS send class</td>
</tr>
</tbody>
</table>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, fantasy; line-height: 19px; white-space: normal; font-size: 13px;">c</span>lass SweetsInAJar</pre>
<pre>{</pre>
<pre>    function __construct()</pre>
<pre>    {</pre>
<pre>    $sms = new SendSMSXML("myUsername","myPassword");</pre>
<pre>    $theData = new ExtractData("sweets",$_GET['message'],$_GET['number']);</pre>
<pre>    $logic = new MessageLogic();</pre>
<pre>    $logic-&gt;setClosenessRangeInPercent(15);</pre>
<pre>    if($theData-&gt;isValid()){</pre>
<pre>       $sms-&gt;send($theData-&gt;getNumber(),$logic-&gt;getMessage($theData-&gt;getData()),"GuessGame");</pre>
<pre>    }</pre>
<pre>    else</pre>
<pre>    {</pre>
<pre>       $sms-&gt;send($theData-&gt;getNumber(),$logic-&gt;getFailedMessage(),"GuessGame");</pre>
<pre>     }</pre>
<pre>   }</pre>
<pre>}</pre>
<p><strong>Explanation</strong><br />
This is our main class that calls all the relevant objects and class together. Simply the logic works like this:</p>
<ol>
<li>Set up all the objects you need.</li>
<li>Set the closeness to the number of sweets you will need to get before the &#8220;your are close&#8221; message will get sent .</li>
<li>If there is data we can use from the incoming message,  select the correct response and send it.</li>
<li>Otherwise select the &#8220;invalid format message&#8221; and send that.</li>
</ol>
<pre><span>class </span>ExtractData

{
   private <span>$theData</span><span>;</span>
<span>   private </span>$theMobileNumber<span>;</span>
   private <span>$keyword</span><span>;</span>
   private <span>$valid</span><span>=</span>false;
<span><span>	</span></span></pre>
<pre><span>   function </span><span>__construct(</span>$keyword<span>,</span>$message<span>,</span>$number<span>)</span>
   {
<span>      $this</span>-&gt;keyword = <span>$keyword</span>;
<span>      $this</span>-&gt;valid = <span>$this</span>-&gt;getMessageInfo(<span>$message</span>);
<span>      $this</span>-&gt;theMobileNumber = <span>$number</span>;
    }

    public function <span>isValid()</span></pre>
<pre><span>    {<span>		</span></span></pre>
<pre><span><span>       return </span><span>$this</span>-&gt;valid;</span></pre>
<pre><span>     }</span></pre>
<pre><span>     public function <span>getData()</span></span></pre>
<pre><span><span>     {</span></span></pre>
<pre><span><span><span>       return </span><span>$this</span>-&gt;theData;</span></span></pre>
<pre>     }

      public function <span>getNumber()</span>
      {
<span>        return </span><span>$this</span>-&gt;theMobileNumber;
      }

<span>      private function </span>getMessageInfo(<span>$m</span>)
      {
         //remove the keyword and set the result
<span><span>	 </span></span>//remove extra spaces
<span>         $m</span>=trim(<span>$m</span>);</pre>
<pre><span>         $m</span>=str_ireplace(<span>$this</span>-&gt;keyword,<span>""</span>,<span>$m</span>); <span>/// remove keyword</span>
<span>         $m</span><span>=ltrim(</span><span>$m</span><span>); </span>// remove any spaces</pre>
<pre><span>         if</span>(is_numeric(<span>$m</span>)) {</pre>
<pre><span>              $this</span>-&gt;theData = <span>$m</span>;
<span>              return true</span><span>; </span>// is it data?
          }</pre>
<pre>           else return false<span>;</span>
      }

}</pre>
<p><strong>Explanation</strong><br />
This simple class allows us to extract the data we need from the short code response. The response contains the enitre message so we need to remove the keyword and any spaces then check to see if the result is a number.  If it is then the object is &#8220;valid&#8221; else it&#8217;s not valid and we can use this state to send the &#8220;invalid format message&#8221; as described above.</p>
<pre><span>class </span>MessageLogic

{</pre>
<pre><span>    private static </span><span>$lowGuess </span><span>=</span>"Bad Luck! There are more sweets in the jar than you think, try again."<span>;</span>
<span>    private static </span><span>$highGuess</span><span>=</span>"Bad Luck! There are less sweets in the jar than you think, try again."<span>;</span>
<span>    private static </span><span>$correctGuess</span><span>=</span>"Well done! You have guessed the correct amount."<span>;</span>
<span>    private static </span><span>$closeGuess</span><span>=</span>"Close Guess! you are within {value}% of the correct answer."<span>;</span>
<span>    private static </span><span>$badData</span><span>=</span>"We couldn't understand your answer please text in the word SWEETS and your answer, for example SWEETS 300"<span>;</span>
<span>    private </span>$numSweets<span>=</span><span>5678</span><span>; </span><span>//default</span>
<span>    private </span><span>$closenessRange</span><span>=</span><span>5</span><span>;  </span>//5 percent as default 

<span>	</span>
<span>    public function </span>setNumberOfSweets(<span>$value</span>)</pre>
<pre>    {
<span>         $this</span>-&gt;numSweets=<span>$value</span>;
    }

<span>    public function </span>setClosenessRangeInPercent(<span>$value</span>) <span>//in percent</span>
    {
<span>         $this</span>-&gt;closenessRange=<span>$value</span>;
    }

<span>    public function </span>getFailedMessage()</pre>
<pre>    {
         return self<span>::</span><span>$badData</span><span>;</span>
    }

    public  function <span>getMessage(</span><span>$value</span><span>)</span>
    {
         /// some very simple rules
<span>         if</span><span>(</span>$value<span>==</span>$this<span>-&gt;numSweets) </span><span>return self</span><span>::</span>$correctGuess<span>;</span>
<span>         $isClose </span>= round(((abs(<span>$this</span>-&gt;numSweets - <span>$value</span>)) / <span>$this</span>-&gt;numSweets) * <span>100</span>); <span>// calculate how close the number is</span>
<span>         if</span>(<span>$isClose </span>&lt; <span>$this</span>-&gt;closenessRange) <span>return </span>str_replace(<span>"{value}"</span>,<span>$this</span>-&gt;closenessRange,<span>self</span>::<span>$closeGuess</span>);    <span>// if with in the closeness range return close messsage</span>
         else<span>{</span>
<span>              if</span>(<span>$value </span>&gt; <span>$this</span>-&gt;numSweets) <span>return self</span>::<span>$highGuess</span>;
              else return self<span>::</span><span>$lowGuess</span><span>;</span>
         }
     }
}</pre>
<p><strong>Explanation</strong></p>
<p>This is the game logic class, the getMessage function requires the integer that we extracted using the ExtractData class.  There are then some simple rules that select the appropriate message as the returned value.</p>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=391&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2009/07/sms-gateway/using-our-short-code-and-gateway-apis-to-create-a-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5 Simple SMS send Class</title>
		<link>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-simple-sms-send-class/</link>
		<comments>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-simple-sms-send-class/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 14:12:12 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[All SMS Gateway Documentation]]></category>
		<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://87.106.109.73/blog/?p=251</guid>
		<description><![CDATA[This blog post has been superseded with our website documentation 2 classes available in php 5, SendSMS and SendSMSXML (recommended) Class Name: SendSMS (depreciated) This class is a nice and easy way to start sending SMS&#8217;s with the Text Marketer system, please feel free to modify at will. You just need the username &#8230; <a href="http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-simple-sms-send-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.textmarketer.co.uk/developers/simple-sms-api.htm">This blog post has been superseded with our website documentation</a></p>
<p><strong>2 classes available in php 5, SendSMS and SendSMSXML (recommended)</strong></p>
<h2>Class Name: SendSMS (depreciated)</h2>
<p>This class is a nice and easy way to start sending SMS&#8217;s with the Text Marketer system, please feel free to modify at will. You just need the username and password from your account login as the constructor arguments.</p>
<p>Download the <a href="http://www.textmarketer.co.uk/downloads/SendSMS.txt">PHP 5 SMS API Class</a></p>
<h2>Example of use</h2>
<pre>&lt;?</pre>
<pre>$sms = new SendSMS("myUsername","myPassword");
if($sms-&gt;send("4477777777","my message","me")) echo "message sent";
else echo "message failed";</pre>
<pre>?&gt;</pre>
<hr />
<h2>Class Name: SendSMSXML</h2>
<p>An updated class from SendSMS which uses the XML response and is simpler to use, please feel free to modify at will. You just need the username and password from your account login as the constructor arguments.</p>
<p>Download <a href="http://www.textmarketer.co.uk/downloads/SendSMSXML.txt">PHP 5 SMS API Class</a></p>
<h2>Example of use</h2>
<pre>&lt;?</pre>
<pre>$sms = new SendSMSXML("myUsername","myPassword");
if($sms-&gt;send("4477777777","my message","me")){</pre>
<pre> <span style="white-space:pre">	</span>echo "I have ".$sms-&gt;getCreditsRemaining()." left and I used  ".$sms-&gt;getCreditsUsed()." credits";</pre>
<pre>}</pre>
<pre>else {</pre>
<pre>     while($error = $sms-&gt;getError())</pre>
<pre>     {</pre>
<pre>         echo $error;</pre>
<pre>     }</pre>
<pre>}</pre>
<pre>?&gt;</pre>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=251&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-simple-sms-send-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5 Delivery Report Helper Class for our SMS Gateway</title>
		<link>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-delivery-report-helper-class/</link>
		<comments>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-delivery-report-helper-class/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 10:16:51 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[All SMS Gateway Documentation]]></category>
		<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[delivery]]></category>
		<category><![CDATA[gateway]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[reporting]]></category>
		<category><![CDATA[sms]]></category>

		<guid isPermaLink="false">http://87.106.109.73/blog/?p=171</guid>
		<description><![CDATA[Class Name: ProcessDeliveryReport This class enables you to access your delivery reports in a nice simplified way. You can search for the outcome of a message for a given number, specify the outcome and the number, download all the data for the given date or parse the reports line by line. &#8230; <a href="http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-delivery-report-helper-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Class Name: ProcessDeliveryReport</h2>
<p>This class enables you to access your delivery reports in a nice simplified way. You can search for the outcome of a message for a given number, specify the outcome and the number, download all the data for the given date or parse the reports line by line.</p>
<p>The constructor requires the delivery report directory that is shown in your account. To find your  delivery report directory, log into your system click the menu heading &#8220;Reports&#8221; then select &#8220;Delivery Reporting&#8221;, you will see a green icon, roll over this and you can find the directory name at the bottom.</p>
<p>If you have sent a message greater than 160 characters you will find multiple entries for that particular number . For instance if your message was 200 characters you will find 2 reports for that number. A message is built from 160 chunks</p>
<p>Download the <a href="http://www.textmarketer.co.uk/downloads/ProcessDeliveryReport.txt" target="_blank">PHP 5 Delivery Report Class</a></p>
<h2>Example of use</h2>
<pre>&lt;?

$reports = new ProcessDeliveryReport ( "Kt65CZMtxlIiEU328ilCBgC38" ); // your delivery report directory
if ($reports-&gt;setReportDate ( "20-05-09" )) print_r ( $reports-&gt;findNumber ( "44777777777", "r" ) ); // print the array of results
else echo "Not found";

?&gt;</pre>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=171&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-delivery-report-helper-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5 SMS Send Class implementing listeners on our SMS Gateway</title>
		<link>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-sms-send-class-implementing-listeners/</link>
		<comments>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-sms-send-class-implementing-listeners/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 08:33:07 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[All SMS Gateway Documentation]]></category>
		<category><![CDATA[PHP Classes]]></category>
		<category><![CDATA[gateway]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[sms]]></category>

		<guid isPermaLink="false">http://87.106.109.73/blog/?p=167</guid>
		<description><![CDATA[Class Name: SendSMSNotifier This class is designed around the FREE Text Marketer SMS API, you need to sign up here to use it. SendSMSNotifier is a singleton class design, incorporating listeners. Your class must include the following public functions: creditAlert($credits_remaining) and failureAlert($error,$number,$message,$error). Download the class from here Example of use &#8230; <a href="http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-sms-send-class-implementing-listeners/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Class Name: SendSMSNotifier</h2>
<p>This class is designed around the FREE Text Marketer SMS API, you need to sign up here to <a href="http://www.textmarketer.co.uk/free-bulk-sms-software.htm" target="_blank">use</a> it.</p>
<p>SendSMSNotifier is a singleton class design, incorporating listeners. Your class must include the following public functions: creditAlert($credits_remaining) and failureAlert($error,$number,$message,$error).</p>
<p>Download the class from <a href="http://www.textmarketer.co.uk/downloads/SendSMSNotifier.txt" target="_blank">here</a></p>
<h2>Example of use</h2>
<pre>class MyTestClass
{</pre>
<pre>        /// simple array of data in: number, message, sender Id (aka originator) format, ideally the data will come from a database
	private $mySMSdata = array(array("44777777777","Hello World!","Me"),
			                             array("text","This will throw an error, mobile number is text","Me"));

	function __construct()
	{
		$sms = SendSMSNotifier::getInstance("myUsername","myPassword"); // your account login details
		$sms-&gt;addListener($this); // Your class gets added to the listeners that will get notified on an event
		$sms-&gt;setCreditThreshold(5); // You want o be notified when you have 5 credits left

		foreach($this-&gt;mySMSdata as $record) // send some data
		{
			list($number,$message,$sender) = $record;
			$sms-&gt;send($number,$message,$sender); // send to the api
		}
		echo $sms-&gt;getCredits();
		print_r($this-&gt;errors);
	}

	public function failureAlert($error,$number,$message,$originator)
	{
		/// Required function for failure messages, you could stop the sending here or log the results
		$this-&gt;errors[] = array($error,$number,$message,$originator);
	}

	public function creditAlert($credits)
	{
		/// Required function for credit alerts, this basic implementation stops sending anymore SMS's
		die("I have nearly run out of credits($credits left), abandon sending!");
	}
}</pre>
<p>So here&#8217;s what happens. You send a bunch of text messages to the gateway using your class (in this example MyTestClass), if you get a failure such as a badly formatted number your function <strong>failureAlert</strong> gets called with the parameters of the failure and data, you can then decide on the implmentation. In the above example it just stores them in an array, once you have sent all the data you could then use the data to clean your database etc.</p>
<p>If the credit limit (in the above case this is 5 $sms-&gt;setCreditThreshold(5) ) your function <strong>creditAlert</strong> will get called, in this case we think this is bad so we use the hammer of the <strong>die()</strong> function to stop the sending, obviously you would handle this much more elegantly :-)</p>
<img src="http://www.textmarketer.co.uk/wordpress/?ak_action=api_record_view&id=167&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.textmarketer.co.uk/blog/2009/06/sms-gateway/php-5-sms-send-class-implementing-listeners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

