<?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>PHP Functionalism</title>
	<atom:link href="http://www.phpfunctionalism.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpfunctionalism.com</link>
	<description>Just another Digital Functionalism Site</description>
	<lastBuildDate>Sat, 05 May 2012 19:20:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simultaneous Database Connections</title>
		<link>http://www.phpfunctionalism.com/tips-and-tricks/simultaneous-database-connections/</link>
		<comments>http://www.phpfunctionalism.com/tips-and-tricks/simultaneous-database-connections/#comments</comments>
		<pubDate>Sat, 05 May 2012 19:20:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=94</guid>
		<description><![CDATA[Do you have a class or function that requires simultanous connections to different databases? There is a fourth parameter on mysql_connect fucntion, that if set to true will allow simultaneous database connections in php. Otherwise the database resource is only going to the last database you connected to. Add the fourth parameter like this: $link1 [...]]]></description>
			<content:encoded><![CDATA[<p>Do you have a class or function that requires simultanous connections to different databases? There is a fourth parameter on <a href="http://www.php.net/manual/en/function.mysql-connect.php">mysql_connect</a> fucntion, that if set to true will allow simultaneous database connections in php. Otherwise the database resource is only going to the last database you connected to.</p>
<p>Add the fourth parameter like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'database1'</span><span style="color: #339933;">,</span><span style="color: #000088;">$link1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$link2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'database12'</span><span style="color: #339933;">,</span><span style="color: #000088;">$link2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/tips-and-tricks/simultaneous-database-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculate Average Date</title>
		<link>http://www.phpfunctionalism.com/functions/calculate-average-date/</link>
		<comments>http://www.phpfunctionalism.com/functions/calculate-average-date/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 01:53:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=91</guid>
		<description><![CDATA[I had a client that wanted a special calculation for the average of several dates. Usually these types of date comparisons are average number of days from today, or some sort of average age calculation, but this was specific to financial reporting and they wanted the average reporting date. So what I came up with [...]]]></description>
			<content:encoded><![CDATA[<p>I had a client that wanted a special calculation for the average of several dates. Usually these types of date comparisons are average number of days from today, or some sort of average age calculation, but this was specific to financial reporting and they wanted the average reporting date. So what I came up with was converting each date into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), then average these numbers, and convert from Unix timestamp back into a date.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_average_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dates</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dates</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total</span><span style="color: #339933;">/</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dates</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So then the implementation of the function to calculate the average date of a number of dates would be simply passing an array of dates to the function like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2010-01-02'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'2011-09-21'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'2012-01-30'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'2012-05-07'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> calculate_average_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/functions/calculate-average-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Database mysq_num_rows() Equivalent</title>
		<link>http://www.phpfunctionalism.com/zend/zend-database-mysq-num-rows/</link>
		<comments>http://www.phpfunctionalism.com/zend/zend-database-mysq-num-rows/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 05:13:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=86</guid>
		<description><![CDATA[The Zend Database doe not seem to have an exact function for what we PHP/MySQL developers are used to with the mysql_num_rows() function. There are different ways to achieve the same result and I wanted to share my personal favorite with you. Basically you create your SQL statement so that it returns one value, which [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://framework.zend.com/manual/en/zend.db.html">Zend Database</a> doe not seem to have an exact function for what we PHP/MySQL developers are used to with the <a href="http://php.net/manual/en/function.mysql-num-rows.php">mysql_num_rows()</a> function. There are different ways to achieve the same result and I wanted to share my personal favorite with you. Basically you create your SQL statement so that it returns one value, which represents the number of results.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> num <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">TABLE</span></pre></div></div>

<p>So to do something like this using the Zend DB class, say you wanted to count all the active users in a table, based on an active flag, would look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$num_rows</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zend_db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(`id`) FROM `users` WHERE `active` = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/zend/zend-database-mysq-num-rows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Get URL</title>
		<link>http://www.phpfunctionalism.com/tips-and-tricks/php-get-url/</link>
		<comments>http://www.phpfunctionalism.com/tips-and-tricks/php-get-url/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 00:06:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=80</guid>
		<description><![CDATA[To get the current url of the page in PHP you need to access the $_SERVER globals. This is a quick and easy way to get the current url (whether http or https) to use it inside your application. $url = &#40;!empty&#40;$_SERVER&#91;'HTTPS'&#93;&#41;&#41; ? &#34;https://&#34;.$_SERVER&#91;'SERVER_NAME'&#93;.$_SERVER&#91;'REQUEST_URI'&#93; : &#34;http://&#34;.$_SERVER&#91;'SERVER_NAME'&#93;.$_SERVER&#91;'REQUEST_URI'&#93;;]]></description>
			<content:encoded><![CDATA[<p>To get the current url of the page in PHP you need to access the <a href="http://www.php.net/manual/en/reserved.variables.server.php">$_SERVER globals</a>. This is a quick and easy way to get the current url (whether http or https) to use it inside your application.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTPS'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;https://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/tips-and-tricks/php-get-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Database Get Last Insert ID</title>
		<link>http://www.phpfunctionalism.com/tips-and-tricks/zend-database-get-last-insert-id/</link>
		<comments>http://www.phpfunctionalism.com/tips-and-tricks/zend-database-get-last-insert-id/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 22:33:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=66</guid>
		<description><![CDATA[If you use the Zend Database class to normalize your database connections, this is how you get the id of the last inserted record. Just as in PHP you would call the mysql_insert_id() function to get the id of the last inserted record of a MySQL database connection. $lastInsertId = $this-&#62;getAdapter&#40;&#41;-&#62;lastInsertId&#40;&#41;; and a more complete [...]]]></description>
			<content:encoded><![CDATA[<p>If you use the <a href="http://framework.zend.com/manual/en/zend.db.html">Zend Database</a> class to normalize your database connections, this is how you get the id of the last inserted record. Just as in PHP you would call the <a href="http://php.net/manual/en/function.mysql-insert-id.php">mysql_insert_id()</a> function to get the id of the last inserted record of a MySQL database connection.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$lastInsertId</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and a more complete example, would look something like this, where you connect to a database, insert a record to the users table, and then get the id of the user that was inserted.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend/Db/Adapter/Pdo/Mysql.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'host'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'secret'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dbname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'db2'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$zend_db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Adapter_Pdo_Mysql<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$zend_db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zend_db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/tips-and-tricks/zend-database-get-last-insert-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Get File Extension</title>
		<link>http://www.phpfunctionalism.com/tips-and-tricks/php-get-file-extension/</link>
		<comments>http://www.phpfunctionalism.com/tips-and-tricks/php-get-file-extension/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 17:35:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=60</guid>
		<description><![CDATA[I see a lot of people providing examples of getting the file extension of a file in PHP using string manipulation techniques, splitting on the dot, etc and just wanted to share the way that I do it. Basically the php pathinfo() function will return the extension as one of its parameters, or only the [...]]]></description>
			<content:encoded><![CDATA[<p>I see a lot of people providing examples of getting the file extension of a file in PHP using string manipulation techniques, splitting on the dot, etc and just wanted to share the way that I do it. Basically the <a href="http://php.net/manual/en/function.pathinfo.php">php pathinfo() function</a> will return the extension as one of its parameters, or only the extension if you specify the optional second parameter as PATHINFO_EXTENSION. I use the <a href="http://php.net/manual/en/function.basename.php">php basename() function</a> as well so that I can use the same code snippet when I want to pass in a filename that contains a path, instead of first pulling out the path.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_file_extension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #b1b100;">return</span> <span style="color: #990000;">pathinfo</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>PATHINFO_EXTENSION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So a few simple usage examples and this post is done <img src='http://www.phpfunctionalism.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> get_file_extension<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// php</span>
<span style="color: #b1b100;">echo</span> get_file_extension<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// php</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/tips-and-tricks/php-get-file-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google URL Shortener API Example using PHP and CURL</title>
		<link>http://www.phpfunctionalism.com/functions/google-url-shortener-api-example-using-php-and-curl/</link>
		<comments>http://www.phpfunctionalism.com/functions/google-url-shortener-api-example-using-php-and-curl/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 21:25:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=49</guid>
		<description><![CDATA[URL shortening is a technique that substantially shortens a url in length by creating a new url that when accessed, redirects the user to the original url. Another use for url shortening might be to disguise the original url&#8230; I recently needed to generate shortened urls and decided to try out the Google URL Shortener [...]]]></description>
			<content:encoded><![CDATA[<p>URL shortening is a technique that substantially shortens a url in length by creating a new url that when accessed, redirects the user to the original url. Another use for url shortening might be to disguise the original url&#8230; I recently needed to generate shortened urls and decided to try out the <a href="http://code.google.com/apis/urlshortener/v1/getting_started.html">Google URL Shortener API</a>. Signing up for a <a href="https://code.google.com/apis/console">free API key</a> allows up to 1,000,000 queries per day.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$google_api_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'INSERT_YOUR_API_KEY_HERE'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$long_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.domain.com/?utm_source=PHP%2BFunctionalism&amp;amp;utm_medium=post&amp;amp;utm_campaign=PHP%2BFunctionalism'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$short_url</span> <span style="color: #339933;">=</span> shorten<span style="color: #009900;">&#40;</span><span style="color: #000088;">$long_url</span><span style="color: #339933;">,</span><span style="color: #000088;">$google_api_key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The php fucntion to integrate with the Google URL Shortener API using PHP and CURL. For the json_decode() and json_encode() functions you will need PHP 5.2+.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> shorten<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;https://www.googleapis.com/urlshortener/v1/url?key=<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HTTPHEADER<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'longUrl'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$json</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/functions/google-url-shortener-api-example-using-php-and-curl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Follow Redirect Return Final URL</title>
		<link>http://www.phpfunctionalism.com/functions/follow-redirect-return-final-url/</link>
		<comments>http://www.phpfunctionalism.com/functions/follow-redirect-return-final-url/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 01:00:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Screen Scraping]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=34</guid>
		<description><![CDATA[If you want to scrape a link and follow the redirect(s) to get the final url, you can use the following php function. This will work to get what is behind the redirect of a tracking link, to see where a bit.ly or other shortened url resolves to, etc. function follow&#40;$url&#41;&#123; &#160; $ch = curl_init&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to scrape a link and follow the redirect(s) to get the final url, you can use the following php function. This will work to get what is behind the redirect of a tracking link, to see where a bit.ly or other shortened url resolves to, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> follow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FRESH_CONNECT<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_MAXREDIRS<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_AUTOREFERER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYPEER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYHOST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$header</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">curl_getinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/functions/follow-redirect-return-final-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Database Escape Parameter</title>
		<link>http://www.phpfunctionalism.com/zend/zend-database-escape-parameter/</link>
		<comments>http://www.phpfunctionalism.com/zend/zend-database-escape-parameter/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 00:22:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=39</guid>
		<description><![CDATA[Using PHP if you wanted to write a simple query and escape a single parameter inline in an sql statement you could use the mysql_real_escape_string() function, which has been a standard function in PHP for a long time. $id = mysql_result&#40;mysql_query&#40;&#34;SELECT `id` FROM `table` WHERE `field` = '&#34;.mysql_real_escape_string&#40;$value&#41;.&#34;; LIMIT 1&#34;&#41;,0&#41;; When using the Zend database [...]]]></description>
			<content:encoded><![CDATA[<p>Using PHP if you wanted to write a simple query and escape a single parameter inline in an sql statement you could use the mysql_real_escape_string() function, which has been a standard function in PHP for a long time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT `id` FROM `table` WHERE `field` = '&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;; LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When using the Zend database abstraction class there is a different way though. You would use the zend database quote() function. Note that the zend database quote() function escapes the value and also adds single quotes around the escaped value.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$zend</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT `id` FROM `table` WHERE `field` = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$zend</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/zend/zend-database-escape-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Format Phone Number</title>
		<link>http://www.phpfunctionalism.com/tips-and-tricks/format-phone-number/</link>
		<comments>http://www.phpfunctionalism.com/tips-and-tricks/format-phone-number/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 00:30:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.digitalfunctionalism.com/php/?p=43</guid>
		<description><![CDATA[A simple function that I use to format phone numbers in the format (123) 456-7980 from virtually any other format. This is a great way to standardize phone data coming from html forms, as they get entered in a variety of different formats. Just store the relevant digits in the database and then format the [...]]]></description>
			<content:encoded><![CDATA[<p>A simple function that I use to format phone numbers in the format (123) 456-7980 from virtually any other format. This is a great way to standardize phone data coming from html forms, as they get entered in a variety of different formats. Just store the relevant digits in the database and then format the phone number nicely in the output.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> format_phone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$digits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'([^[:digit:]])'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$digits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">') '</span><span style="color: #339933;">.</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">.</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpfunctionalism.com/tips-and-tricks/format-phone-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

