<?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>Down Lifes Road... &#187; Code</title>
	<atom:link href="http://www.downlifesroad.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.downlifesroad.com</link>
	<description></description>
	<lastBuildDate>Wed, 04 Aug 2010 22:37:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Zend Framework Twitter and oAuth in 1.10.7</title>
		<link>http://www.downlifesroad.com/2010/08/04/zend-framework-twitter-and-oauth-in-1-10-7/</link>
		<comments>http://www.downlifesroad.com/2010/08/04/zend-framework-twitter-and-oauth-in-1-10-7/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 21:07:18 +0000</pubDate>
		<dc:creator>Casey</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.downlifesroad.com/?p=40</guid>
		<description><![CDATA[Today I wanted need to switch out some implementations I have in twitter. I was using HTTP Basic for authentication as were most. I went through the Zend Oauth docs, and combined with their examples and those of Pádraic Brady I got this working fine. I&#8217;m not going to give a step by step, as [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wanted need to switch out some implementations I have in twitter. I was using HTTP Basic for authentication as were most. I went through the Zend Oauth docs, and combined with their examples and those of <a href="http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.html">Pádraic Brady</a> I got this working fine.</p>
<p>I&#8217;m not going to give a step by step, as I&#8217;m assuming you&#8217;ve already read <a href="http://framework.zend.com/manual/en/zend.service.twitter.html">various</a> <a href="http://framework.zend.com/manual/en/zend.oauth.introduction.html">docs</a>.</p>
<p>I broke my files out like Pádraic, so if you&#8217;ve read his than you&#8217;ll understand mine.</p>
<p>First, my config file which is included within each subsequent file.</p>
<pre class="brush:php">
$config = array(
        'callbackUrl'    => 'http://www.example.com/callback.php',
        'siteUrl'        => 'http://twitter.com/oauth',
        'consumerKey'    => 'XXXXXXXXXXXXXXXXXXXXX',
        'consumerSecret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
);</pre>
<p>My index page is the starting point of the authentication process. Our persistent storage for this example is in session, though in production I&#8217;m using a database.</p>
<pre class="brush:php">
include_once './config.php';

if (!$_SESSION['TWITTER_REQUEST_TOKEN']) {
    // $config is from our include!
    $consumer = new Zend_Oauth_Consumer($config);

    // fetch a request token
    $token = $consumer->getRequestToken();

    // persist the token to storage
    $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token);

    // redirect the user
    $consumer->redirect();
}
</pre>
<p>The callback url specified will now be triggered</p>
<pre class="brush:php">
include_once './config.php';

// Check if _GET is set and that the request token is being returned
if (!empty($_GET) &#038;&#038; isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {

    $consumer = new Zend_Oauth_Consumer($config);
    $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN']));

    $_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);

    // Now that we have an Access Token, we can discard the Request Token
    $_SESSION['TWITTER_REQUEST_TOKEN'] = null;
} else {
    // Mistaken request? Some malfeasant trying something?
    exit('Invalid callback request. Oops. Sorry.');
}

// Lets just pass on to a test posting page
redirect_url('post.php');
</pre>
<p>Finally, if all went to plan and the user authorized our application, we can pull in their access token and do as we need on their behalf.</p>
<pre class="brush:php">
include_once './config.php';

// Verify we're good to go
if (isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {

    // Unserialize and add the token to the config
    $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
    $config['accessToken'] = $token;

    // Pass the config into Twitter, and act upon the users account
    $twitter = new Zend_Service_Twitter($config);

    $twitter->status->update('I am so posting from your account. Rock!');
}
</pre>
<p>That&#8217;s all really. I do wish the Zend Service Twitter docs would be updated to reflect some examples. I saw in SVN Pádraic has already changed some of the docs, they&#8217;ve just not been updated on site yet.</p>
<p>Thanks to the documentation authors and those I borrowed from to get my working example going and so I could more easily move forward with this project.</p>
<p>-Casey</p>
]]></content:encoded>
			<wfw:commentRss>http://www.downlifesroad.com/2010/08/04/zend-framework-twitter-and-oauth-in-1-10-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Form Element Radio and Default value</title>
		<link>http://www.downlifesroad.com/2009/10/27/zend-form-element-radio-and-default-value/</link>
		<comments>http://www.downlifesroad.com/2009/10/27/zend-form-element-radio-and-default-value/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 17:44:59 +0000</pubDate>
		<dc:creator>Casey</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.downlifesroad.com/?p=21</guid>
		<description><![CDATA[I setup a Zend Form object and needed radio buttons, but also needed a default value. Took me a few minutes to find, but here&#8217;s an example of use. /** * Setting some values for our radio buttons. */ $options = array( '1' => 'Option 1', '2' => 'Option 2', '3' => 'Option 3' ); [...]]]></description>
			<content:encoded><![CDATA[<p>I setup a Zend Form object and needed radio buttons, but also needed a default value. Took me a few minutes to find, but here&#8217;s an example of use.</p>
<pre lang="php">
/**
 * Setting some values for our radio buttons.
 */
$options = array(
    '1' => 'Option 1',
    '2' => 'Option 2',
    '3' => 'Option 3'
);

/**
 * The array('value' => 2) below sets our default value.
 */
$radio = new Zend_Form_Element_Radio('elementName', array('value' => '2'));
$radio->addMultiOptions($options);
$this->addElement($radio);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.downlifesroad.com/2009/10/27/zend-form-element-radio-and-default-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Old code.</title>
		<link>http://www.downlifesroad.com/2008/09/18/old-code/</link>
		<comments>http://www.downlifesroad.com/2008/09/18/old-code/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 22:35:59 +0000</pubDate>
		<dc:creator>Casey</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.downlifesroad.com/?p=12</guid>
		<description><![CDATA[I love coming across old code *cough* garbage *cough* The purpose of this was to take a title, and create something url safe and in the fashion a client was wanting. The first function was old code, so don&#8217;t blame me. function cleanUrl($url) { $find = array('/--/', '/ - /','/ /', '/!/', '/"/', "/'/", '/--/'); [...]]]></description>
			<content:encoded><![CDATA[<p>I love coming across old code *cough* garbage *cough*</p>
<p>The purpose of this was to take a title, and create something url safe and in the fashion a client was wanting. The first function was old code, so don&#8217;t blame me.</p>
<pre lang="php">
function cleanUrl($url)
{
  $find =        array('/--/', '/ - /','/ /', '/!/', '/"/', "/'/", '/--/');
  $replace =    array('-', '-', '-', '', '', '', '-');

  $result = strtr($url, "`~@#$%^&#038;*()_=+|[]{};:,./<>?", "---------------------------");

  return strtolower(preg_replace($find, $replace, strip_tags(stripslashes(trim($result)))));
}
</pre>
<p>Isn&#8217;t it horrible?</p>
<p>I rewrote the function to&#8230;</p>
<pre lang="php">
function cleanUrl($url)
{
  $url = strtolower($url);

  $url = preg_replace("/[^a-z0-9\s+]/", '', $url);
  $url = preg_replace("/[\s]{1,}/", '-', $url);

  return $url;
}
</pre>
<p>That makes me happier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.downlifesroad.com/2008/09/18/old-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ewerl Function</title>
		<link>http://www.downlifesroad.com/2008/06/29/ewerl-function/</link>
		<comments>http://www.downlifesroad.com/2008/06/29/ewerl-function/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 02:16:39 +0000</pubDate>
		<dc:creator>Casey</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.downlifesroad.com/2008/06/29/ewerl-function/</guid>
		<description><![CDATA[Ewerl is a new URL rewritting site with cool features. Here&#8217;s a simple function to return the Ewerl url. It&#8217;s only being used for the main url, but could easily be adjusted to return an array. I just didn&#8217;t have a need for it in my case at this time. /** * Returns an Ewerl [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ewerl.com" title="Ewerl" target="_blank">Ewerl</a> is a new URL rewritting site with cool features.</p>
<p>Here&#8217;s a simple function to return the Ewerl url. It&#8217;s only being used for the main url, but could easily be adjusted to return an array. I just didn&#8217;t have a need for it in my case at this time.</p>
<pre lang="php">
/**
 * Returns an Ewerl url if available.
 * Otherwise the original that was passed in.
 * Returns false if..
 * 1. JSON extension isn't installed.
 * 2. No url was entered.
 * 3. No response from server.
 *
 * @todo Add URL validation.
 *
 * @param string $url
 * @return string URL, or false.
 * @author Casey Wilson <caseyw@downlifesroad.com>
 */
function getEwerl($url) {
	/**
	 * Verify JSON extension is loaded.
	 */
	if (!extension_loaded('JSON')) return false;

	/**
	 * Url wasn't passed
	 */
	if (!$url) return false;

	/**
	 * Assign / Prep vars.
	 */
	$postUrl = 'http://ewerl.com/api/v1/make/url=';
	$urlEncoded = base64_encode($url);

	$response = file_get_contents($postUrl . $urlEncoded);

	if (!$response) {
		return $url;
	}

	/**
	 * Should have returned a JSON object.
	 */
	$jsonObj = json_decode($response);

	/**
	 * If either Status not set, or status is false, return original url.
	 */
	if (!isset($jsonObj->status) || $jsonObj->status == 'FALSE') {
		return $url;
	}

	return $jsonObj->url_main;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.downlifesroad.com/2008/06/29/ewerl-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Url to TinyUrl Function</title>
		<link>http://www.downlifesroad.com/2008/06/21/url-to-tinyurl-function/</link>
		<comments>http://www.downlifesroad.com/2008/06/21/url-to-tinyurl-function/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 03:29:19 +0000</pubDate>
		<dc:creator>Casey</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.downlifesroad.com/2008/06/21/url-to-tinyurl-function/</guid>
		<description><![CDATA[I needed to transform some long urls into tiny ones :p Figured I&#8217;d post something up here that may be useful to someone else. Code was updated. /** * Get TinyUrl for $url * * @param string $url Must be a valid url */ function getTinyUrl($url) { if (! $url) { return false; } if [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to transform some long urls into tiny ones :p</p>
<p>Figured I&#8217;d post something up here that may be useful to someone else.</p>
<p>Code was updated.</p>
<pre lang="php">
/**
 * Get TinyUrl for $url
 *
 * @param string $url Must be a valid url
 */
function getTinyUrl($url) {
	if (! $url) {
		return false;
	}

	if (strlen ( $url ) < 30) {
		return $url;
	}

	$response = file_get_contents ( 'http://tinyurl.com/api-create.php?url=' . $url );

	return ($response) ? $response : $url;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.downlifesroad.com/2008/06/21/url-to-tinyurl-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
