<?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>sudocode</title>
	<atom:link href="http://sudocode.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://sudocode.net</link>
	<description>When you can just make it look like it works.</description>
	<lastBuildDate>Sun, 21 Feb 2010 14:59:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL Abstraction/Helper/Wrapper Class for PHP</title>
		<link>http://sudocode.net/2010/02/21/mysql-abstraction-wrapper-helper-class-for-php/</link>
		<comments>http://sudocode.net/2010/02/21/mysql-abstraction-wrapper-helper-class-for-php/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 12:02:14 +0000</pubDate>
		<dc:creator>arvin</dc:creator>
				<category><![CDATA[Low Level]]></category>
		<category><![CDATA[abstraction]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[class.xcurl.php]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[wrapper]]></category>
		<category><![CDATA[xdata]]></category>

		<guid isPermaLink="false">http://sudocode.net/?p=128</guid>
		<description><![CDATA[I was making a Twitter bot earlier this week and I already finished the logic/control aspect of the bot and all I needed was the part where I needed to store or load the data for every user. And after weighing the choice between using just flat files or a database, I opted for the [...]]]></description>
			<content:encoded><![CDATA[<p>I was making a Twitter bot earlier this week and I already finished the logic/control aspect of the bot and all I needed was the part where I needed to store or load the data for every user. And after weighing the choice between using just flat files or a database, I opted for the latter because the app needed it. The thing is, I have not made  anything that uses a database for a while, or anything at all :P I made an abstraction class before so I got it from the server of my other website and tried to use it. Tried was the word.</p>
<p>I read my old scripts because I wasn&#8217;t sure how to use the class that I made. And I&#8217;ll cut the story short, it didn&#8217;t work.</p>
<p>I wish it was as simple as using a flat file data source, which I already have a different abstraction class for. So instead of debugging 600+ lines of code, I just made a new one from new file. Back to mysql_connect all over again. And yeah, you read that right, I made a ridiculously long 600-line abstraction class for a couple of PHP functions. It was the time where I started OOP in PHP so I got it all in the box: database server object, database object, table, field, data set, data request, and a whole lot of helper functions and mini classes.</p>
<p>It had a well thought-of data integrity verification process. You could list 20+ different ways to invoke the very same select statement. It could take in arrays, associative arrays, stdclasses, or comma-separated strings for the data. It used <a href="http://php.net/manual/en/function.func-num-args.php">func_num_args</a> and <a href="http://www.php.net/manual/en/function.func-get-args.php">func_get_args</a> to give you the idea.</p>
<p>But what I need is just a simple way to store data and load it at a later time.<br />
<span id="more-128"></span></p>
<p>I wanted to make a class that is simple to use. Something which has just a few moving parts, so it&#8217;ll be easier to figure out or debug after some time. My old class needed 4 lines of code to invoke a select statement. I&#8217;ve even seen others that need 6 lines. I wanted to make it to need just 1 function invocation to use it, and I thought of my other abstraction class, <a href="http://sudocode.net/sources/includes/class-xcurl-php/">xcurl</a>. I then made <a href="http://sudocode.net/sources/includes/class-xdata-php/">xdata</a>. Below is the syntax for a select statement:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// SELECT field1,field2 FROM database.table WHERE field3='value' ORDER BY field3 DESC LIMIT 0, 10</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/database/table/field3/value?fields=field1,field2&amp;amp;orderby=field3+DESC#0,10'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><br /></div></div>

<p>Or if you prefer to indicate the options via an associative array, you can pass it on the second argument of xdata::get().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'table'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'table'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'fields'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'field1,field2'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'where'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'field3=\'value\''</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'orderby'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'field3 DESC'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'limit'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'0,10'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><br /></div></div>

<p>You can also mix the two. Put the database and table name on the URL and if the WHERE clause is complex, just set it on the second argument. The function xdata::get() will return a multi-row array and each row is an associative array, which you can handle by somewthing like the one below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">/*
    $data = array(
        array(field1=&gt;value1, field2=&gt;value2, ... ),
        array(field1=&gt;value1, field2=&gt;value2, ... ),
        ...
    );
*/</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$field</span>: <span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre><br /></div></div>

<p>If you need, you can also get just the value of a single cell, which is normally would be returned as a string. Just include in the URL the .scalar extension, as you can see below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// SELECT field1 FROM database.table WHERE ID=14</span>
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/database/table/14.scalar?fields=field1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span></pre><br /></div></div>

<p>Other values for the extension are .resource, .json, .xml, .scalar, .array and the default (if there is no extension) .assoc. Although not all of it is already implemented. And by the way, you can use xdata::select() instead of xdata::get() if you prefer.</p>
<p>Now to do an Insert query:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// INSERT INTO database.table (field1,field2) VALUES(value1,value2)</span>
<span style="color: #000088;">$last_insert_id</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/database/table?field1=value1&amp;field2=value2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// or</span>
&nbsp;
<span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'field1'</span><span style="color: #339933;">-&gt;</span><span style="color: #0000ff;">'value1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'field2'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'value2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$last_insert_id</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/database/table'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// and yes, you can do multi-row inserts</span></pre><br /></div></div>

<p>Finally, to do an Update:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// UPDATE database.table SET field1='value1', field2='value2' WHERE ID=14</span>
<span style="color: #000088;">$num_affected_rows</span> <span style="color: #339933;">=</span> xdata<span style="color: #339933;">::</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql://username:password@hostname/database/table/14?field1=value1&amp;field2=value2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><br /></div></div>

<p>Finally, I can finish that bot.</p>
<p>Interested? You can view added documentation and/or copy the code at the source page of <a href="http://sudocode.net/sources/includes/class-xdata-php/">xdata</a>. As like the other code posted here, anyone can copy, use or modify them.</p>
]]></content:encoded>
			<wfw:commentRss>http://sudocode.net/2010/02/21/mysql-abstraction-wrapper-helper-class-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Talk to Someone Random</title>
		<link>http://sudocode.net/2010/02/11/talk-to-someone-random/</link>
		<comments>http://sudocode.net/2010/02/11/talk-to-someone-random/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 01:23:10 +0000</pubDate>
		<dc:creator>arvin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[chat]]></category>
		<category><![CDATA[claim]]></category>
		<category><![CDATA[imified]]></category>
		<category><![CDATA[omegle]]></category>
		<category><![CDATA[stranger]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://sudocode.net/?p=89</guid>
		<description><![CDATA[Would you like to talk to someone random today?
I created an omegle-like app but instead of it being a website, you can add it on your IM buddies:) Check out the project page for the details.
]]></description>
			<content:encoded><![CDATA[<p>Would you like to talk to <a title="Someone random Bot" href="http://sudocode.net/projects/someone-random/">someone random</a> today?</p>
<p>I created an <a href="http://omegle.com">omegl</a>e-like app but instead of it being a website, you can add it on your IM buddies:) Check out the <a href="http://sudocode.net/projects/someone-random/">project page</a> for the details.</p>
]]></content:encoded>
			<wfw:commentRss>http://sudocode.net/2010/02/11/talk-to-someone-random/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending a Google Voice SMS using PHP</title>
		<link>http://sudocode.net/2009/11/29/sending-a-google-voice-sms-using-php/</link>
		<comments>http://sudocode.net/2009/11/29/sending-a-google-voice-sms-using-php/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 13:04:26 +0000</pubDate>
		<dc:creator>arvin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[clientlogin]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[gvoice]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[require:class.xcurl.php]]></category>
		<category><![CDATA[sms]]></category>
		<category><![CDATA[voice]]></category>
		<category><![CDATA[xcurl]]></category>

		<guid isPermaLink="false">http://kode.dr.ag/on/?p=66</guid>
		<description><![CDATA[The very first obstacle that I encountered after getting my Google Voice invite was that it needed a US number to be activated. Good thing my girlffriend lives in Texas so we used their home phone for my account&#8217;s forwarding number, but before that, I tried using my Bueno account, but Google doesn&#8217;t seem to [...]]]></description>
			<content:encoded><![CDATA[<p>The very first obstacle that I encountered after getting my Google Voice invite was that it needed a US number to be activated. Good thing my girlffriend lives in Texas so we used their home phone for my account&#8217;s forwarding number, but before that, I tried using my <a href="http://bueno.com">Bueno</a> account, but Google doesn&#8217;t seem to pick up the button presses on Bueno&#8217;s softphone. Gone through all that, the very first thing I tried was to be able to send an SMS message from a php script.<span id="more-66"></span></p>
<p>The very first thing to do to use GVoice is to login to the service. Now, I tried curling my way to it but it seemed that the curl cookiejar setting on our server at <a href="http://kundiman.net">kundiman.net</a> is not working. Then I got to see this code written in Java, <a href="http://code.google.com/p/google-voice-java/">google-voice-java</a>. To login to GVoice without emulating a browser, we could use Google&#8217;s <a href="http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html">Client Login API</a>. We just need to post the login credentials to the API&#8217;s service endpoint and we will then get an <strong>Auth</strong> key.</p>
<p>To illustrate this, we will use the <a href="http://sudocode.net/source/includes/class-xcurl-php/">xcurl</a> class to post the data:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'accountType'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GOOGLE'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// indicates a Google account</span>
  <span style="color: #0000ff;">'Email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'gvoice_username'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// full email address</span>
  <span style="color: #0000ff;">'Passwd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'gvoice_password'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'service'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'grandcentral'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Name of the Google service</span>
  <span style="color: #0000ff;">'source'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sudocode.net-example-1.0'</span> <span style="color: #666666; font-style: italic;">// Application's name, e.g. companyName-applicationName-versionID</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> xcurl<span style="color: #339933;">::</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.google.com/accounts/ClientLogin'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Google will then respond with a 200 if successful or a 403 if an error occured. There are several other <a href="http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Errors">error codes</a> that might be sent. If successful, the body of the response will contain 3 lines of the data, the last line, containing the Auth key.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">SID</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">DQAAAGgA7Zg8CTN</span>
<span style="color: #000099;">LSID</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">DQAAAGsAlk8BBbG</span>
<span style="color: #000099;">Auth</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">DQAAAGgAdk3fA5</span></pre></td></tr></table></div>

<p>The lines are much longer than the example above. The first two lines, SID and LSID are not used. We just need the 3rd line for our purposes and we just use preg_match to get the Auth value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Auth=([-_a-zA-Z0-9]+)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Getting the Auth key is only half of the login process. To be able to send SMS, we will also need the value for <strong>_rnr_se_</strong>. Don&#8217;t ask, I have no idea what this variable&#8217;s name means, but it&#8217;s needed. The only way to get it, as far as I know is by emulating a web browser through a series of URL calls and fetches, getting GALX and posting the login credentials, etc. It&#8217;s a long process.</p>
<p>But to get to the other side of the river easier, just open a real web browser, like Firefox and login to your <a href="http://google.com/voice">Google Voice</a> account, go to your inbox and view the page&#8217;s <a href="view-source:https://www.google.com/voice#inbox">source</a> then look for <strong>_rnr_se</strong>. As basic and manual as that.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;_rnr_se&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;aadsklLSHdlSD88sad5asd=&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre><br /></div></div>

<p>You can find it as a hidden field on a form somewhere on your inbox. Get the value and hardcode it on your script. Just think of it as an API key so you won&#8217;t feel guilty. Thinking &#8217;bout it, who silly programmer will curl his or her way to get an application&#8217;s Yahoo! Application ID? Plus, you won&#8217;t be making the few unnecessary calls to get it because it&#8217;s value does not change.</p>
<p>Well, just for now, <strong>_rnr_se</strong>&#8217;s value does not change. The thing is, Google has not yet released any proper documentation for it.</p>
<p>Now we are ready to send our SMS message through Google Voice! It&#8217;s easy, we just need to post the needed variables, and what better way to explain it than to do it in code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'_rnr_se'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$rnrse</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'phoneNumber'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'639198022751'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// International notation of phone numbers</span>
    <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'This is a test SMS sent from PHP using Google Voice!'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auth'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'auth' and not 'Auth' like what ClientLogin uses</span>
    <span style="color: #0000ff;">'id'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>  <span style="color: #666666; font-style: italic;">// thread ID of message, GVoice's way of threading the messages like GMail</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> xcurl<span style="color: #339933;">::</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.google.com/voice/sms/send/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If all goes well, the recipient should be getting the message in a few moments after making the call.</p>
<p>Just a few notes; the last call, sending the SMS, will return a status OK even if the message was not actually sent, like for example, a wrong number. So be sure to make tests on validity of the number like format-wise at least or a valid <a href="http://wikitravel.org/en/List_of_country_calling_codes">country code</a>. Also check the length of the message because going over 160 characters will generate an error. If your script suddenly stops working, be sure to check the value of <strong>_rnr_se</strong> (manually again) because it might have changed.</p>
<p>If you want to access the other GVoice features, the endpoints are listed on <a href="http://posttopic.com/topic/google-voice-add-on-development">posttopic</a>, compiled by <a href="http://thatsmith.com/">Chad Smith</a>. To access the other API URLs, we just need to pass the <strong>auth</strong> value as a GET variable.</p>
<p>For example, to get a list of SMS messages on your inbox:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> xcurl<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.google.com/voice/inbox/recent/sms/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'auth'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$auth</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><br /></div></div>

<p>The SMS messages can then be parsed, and when I last checked, the response is an xml file, with the messages contained on a json field</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #339933;">&lt;![CDATA[{&quot;messages&quot;:{&quot;a81568b401b3f557437e7741b753a52696ac3072&quot;:{&quot;id&quot;:&quot;a81568b401b3f557437e7741b753a52696ac3072&quot;,&quot;phoneNumber&quot;:&quot;+639174328247&quot;,&quot;displayNumber&quot;:&quot;+6391781238247&quot;,&quot;startTime&quot;:&quot;1259449068996&quot;,&quot;displayStartDateTime&quot;:&quot;11/29/09 6:57 AM&quot;,&quot;displayStartTime&quot;:&quot;6:57 AM&quot;,&quot;relativeStartTime&quot;:&quot;13 hours ago&quot;,&quot;note&quot;:&quot;&quot;,&quot;isRead&quot;:true,&quot;isSpam&quot;:false,&quot;isTrash&quot;:false,&quot;star&quot;:false,&quot;labels&quot;:[&quot;sms&quot;,&quot;all&quot;],&quot;type&quot;:11,&quot;children&quot;:&quot;&quot;},</span>
<span style="color: #339933;">...</span>
<span style="color: #339933;">]]&gt;</span>
  <span style="color: #808080; font-style: italic;">&lt;!--[CDATA[</span>
<span style="color: #808080; font-style: italic;">...</span></pre><br /></div></div>

<p>Finally, the complete PHP code to send an SMS using Google Voice:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'class.xcurl.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'accountType'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GOOGLE'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// indicates a Google account</span>
  <span style="color: #0000ff;">'Email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'gvoice_username'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// full email address</span>
  <span style="color: #0000ff;">'Passwd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'gvoice_password'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'service'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'grandcentral'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Name of the Google service</span>
  <span style="color: #0000ff;">'source'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sudocode.net-example-1.0'</span> <span style="color: #666666; font-style: italic;">// Application's name, e.g. companyName-applicationName-versionID</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> xcurl<span style="color: #339933;">::</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.google.com/accounts/ClientLogin'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Auth=([-_a-zA-Z0-9]+)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'_rnr_se'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$rnrse</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'phoneNumber'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'639198022751'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// International notation of phone numbers</span>
    <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'This is an test SMS sent from php using Google Voice!'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auth'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'auth' and not 'Auth' like what ClientLogin uses</span>
    <span style="color: #0000ff;">'id'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>  <span style="color: #666666; font-style: italic;">// thread ID of message, GVoice's way of threading the messages like GMail</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> xcurl<span style="color: #339933;">::</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.google.com/voice/sms/send/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For more info on the <a href="http://sudocode.net/source/includes/class-xcurl-php/">xcurl</a> class, just visit it&#8217;s source page.</p>
<p>Using this info, I made an <a href="http://sudocode.net/projects/email-to-sms-gateway/">Email to SMS Gateway</a> for easily sending international SMS messages for free.</p>
]]></content:encoded>
			<wfw:commentRss>http://sudocode.net/2009/11/29/sending-a-google-voice-sms-using-php/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
