<?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>SoliTech GmbH &#187; Bit.LY</title>
	<atom:link href="http://www.solitechgmbh.com/tag/bit-ly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solitechgmbh.com</link>
	<description>Connecting businesses to people through effective online marketing strategy</description>
	<lastBuildDate>Thu, 13 May 2010 19:14:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>New Bit.ly Class to use with CakePHP or by it self</title>
		<link>http://www.solitechgmbh.com/2009/08/06/new-bit-ly-class-to-use-with-cakephp-or-by-it-self/</link>
		<comments>http://www.solitechgmbh.com/2009/08/06/new-bit-ly-class-to-use-with-cakephp-or-by-it-self/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 23:53:52 +0000</pubDate>
		<dc:creator>Gyuri</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Bit.LY]]></category>
		<category><![CDATA[cake]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.solitechgmbh.com/?p=515</guid>
		<description><![CDATA[If you use service like Twitter where you only get a limited number of characters to post or if you are just sick of dealing with unruly long urls you might be familliar with url shortening services such as Bit.ly. I personally like them because of they offer a nice API and statistics all for ...<br /><a href="http://www.solitechgmbh.com/2009/08/06/new-bit-ly-class-to-use-with-cakephp-or-by-it-self/">Read more...</a>]]></description>
			<content:encoded><![CDATA[<p>If you use service like Twitter where you only get a limited number of characters to post or if you are just sick of dealing with unruly long urls you might be familliar with url shortening services such as Bit.ly.</p>
<p>I personally like them because of they offer a nice API and statistics all for free. It is a pretty quick service too. You can generate hundreds of URLs in just a few seconds. All you have to do is register and you get a username and API key which you will need in the following section.</p>
<p>Here is the code &#8230; As you can see I only implemented the shorten for now but I will probably add some of the other stuff later. For some reason WP won&#8217;t let me post curl_exec( so be sure to remove the extra character.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> bitly <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * A PHP class to utilize the bitly API
	 * through the Bitly API. Handles:
	 * - Shorten
	 *
	 * Requires PHP5, CURL, JSON and a Bitly account http://bit.ly/
	 *
	 * Bitly API documentation: http://code.google.com/p/bitly-api/wiki/ApiDocumentation
	 */</span>
&nbsp;
	protected <span style="color: #000088;">$appkey</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$login</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$version</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	* Contruct the Bitly Class
	*
    * @param string, your Bitly account key
    * @param string, your Bitly account login
	*/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$login</span><span style="color: #339933;">,</span> <span style="color: #000088;">$appkey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'2.0.1'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>login <span style="color: #339933;">=</span> <span style="color: #000088;">$login</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>appkey <span style="color: #339933;">=</span> <span style="color: #000088;">$appkey</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>version <span style="color: #339933;">=</span> <span style="color: #000088;">$version</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	* Retrieve a shortened URL
	*
    * @param string, url to shorten
    * @param string, version of Bitly to use
	*/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//create the URL</span>
		<span style="color: #000088;">$api_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.bit.ly/shorten?version='</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>version<span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;longUrl='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;format=xml&amp;amp;login='</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>login<span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;apiKey='</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>appkey<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//call the API</span>
		<span style="color: #000088;">$curl</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>
		<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$api_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> curl_ex_ec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//parse the XML response and return the url</span>
		<span style="color: #000088;">$xml_object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml_object</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>results<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nodeKeyVal<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>shortUrl<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you want to use this in CakePHP &#8230; you will want to do something like this.<br />
Put this line before the class &#8230; I actually use this in a helper since this is a view level class</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Bitly'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file&quot;</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">&quot;bitly/bitly.class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you can just invoke it in one of your functions somewhere with two simple calls!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bitly</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bitly<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'your_API_key_here'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$short_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bitly</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>shorten<span style="color: #009900;">&#40;</span><span style="color: #000088;">$long_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Simple as that &#8230; oh and one more thing. Be sure to have CURL installed else this won&#8217;t work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solitechgmbh.com/2009/08/06/new-bit-ly-class-to-use-with-cakephp-or-by-it-self/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
