<?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>stuntSnippets &#187; PHP</title>
	<atom:link href="http://stuntsnippets.com/snippets/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuntsnippets.com</link>
	<description></description>
	<lastBuildDate>Thu, 17 May 2012 11:59:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Get coordinates from postcode or address using Google API</title>
		<link>http://stuntsnippets.com/get-coordinates-from-postcode-or-address-using-google-api/</link>
		<comments>http://stuntsnippets.com/get-coordinates-from-postcode-or-address-using-google-api/#comments</comments>
		<pubDate>Wed, 02 May 2012 11:23:33 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[coordinates]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Maps]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1155</guid>
		<description><![CDATA[Sometimes you need to get exact location by address entered by user on your website. Google Maps API offers a service that will return exact coordinate by the request you send it. See following snippet to see how to get coordinates from postcode or address by using Google Maps API. $address = '124 Norfolk Street, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fget-coordinates-from-postcode-or-address-using-google-api%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fget-coordinates-from-postcode-or-address-using-google-api%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Sometimes you need to get exact location by address entered by user on your website. Google Maps API offers a service that will return exact coordinate by the request you send it. See following snippet to see how to get coordinates from postcode or address by using Google Maps API.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$address</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'124 Norfolk Street, Glasgow, United Kingdom'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://maps.googleapis.com/maps/api/geocode/json?address='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$address</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;sensor=true'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Latitude:'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">geometry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">location</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lat</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Longitude:'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">geometry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">location</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lng</span><span style="color: #339933;">;</span></pre></div></div>

<p>I hope you enjoy it and make useful scripts for your project!</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/get-coordinates-from-postcode-or-address-using-google-api/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/get-coordinates-from-postcode-or-address-using-google-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validate Select Field in Zend Form</title>
		<link>http://stuntsnippets.com/validate-select-field-in-zend-form/</link>
		<comments>http://stuntsnippets.com/validate-select-field-in-zend-form/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 14:54:16 +0000</pubDate>
		<dc:creator>Darjan Vukusic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[Validate Select Field in Zend Form]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Form]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend Select]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1146</guid>
		<description><![CDATA[When setting your Zend Form with Select field you don&#8217;t always wish to have label on that filed but rather “Select one” as a first option. Setting the required flag on Zend Select, or adding notEmpty validator won&#8217;t always work especially if your box is being populated dynamically. Let&#8217;s say you have something like this: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fvalidate-select-field-in-zend-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fvalidate-select-field-in-zend-form%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When setting your Zend Form with Select field you don&#8217;t always wish to have label on that filed but rather “Select one” as a first option.</p>
<p>Setting the required flag on Zend Select, or adding notEmpty validator won&#8217;t always work especially if your box is being populated dynamically.</p>
<p>Let&#8217;s say you have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;select name=&quot;example&quot;&gt;
    &lt;option value=&quot;Select one&quot;&gt;Select one&lt;/option&gt;
    &lt;option value=&quot;value1&quot;&gt;value1&lt;/option&gt;
    &lt;option value=&quot;value2&quot;&gt;value2&lt;/option&gt;
    &lt;option value=&quot;value3&quot;&gt;value3&lt;/option&gt;
&lt;/select&gt;</pre></div></div>

<p>You could easily validate this by adding Zend_Validate_Regex and passing any pattern to it<br />
as follows in snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Regex<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/[^Select\sone]/'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You need to select one value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This one will check if the value is not &#8220;Select one&#8221; and let your user pass validation only in that case.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/validate-select-field-in-zend-form/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/validate-select-field-in-zend-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP fluent interface</title>
		<link>http://stuntsnippets.com/php-fluent-interface/</link>
		<comments>http://stuntsnippets.com/php-fluent-interface/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 10:01:02 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fluent interface]]></category>
		<category><![CDATA[PHP fluent interface]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1134</guid>
		<description><![CDATA[PHP fluent interfaces have been around for quite some time. They are often used just for chaining methods of an object so that we get nice readable code. Good fluent interface takes a while to build, and will take some planning to make object methods work seamlessly. However, if you still haven&#8217;t experienced the PHP [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-fluent-interface%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-fluent-interface%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>PHP fluent interfaces have been around for quite some time. They are often used just for chaining methods of an object so that we get nice readable code.</p>
<p>Good fluent interface takes a while to build, and will take some planning to make object methods work seamlessly. However, if you still haven&#8217;t experienced the <strong>PHP fluent interface</strong>, there is a good way to start, just check out the following snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Php_Fluent_Interface_Demo <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_first</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_second</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_third</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFirst<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstVal</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first <span style="color: #339933;">=</span> <span style="color: #000088;">$firstVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSecond<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondVal</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_second <span style="color: #339933;">=</span> <span style="color: #000088;">$secondVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setThird<span style="color: #009900;">&#40;</span><span style="color: #000088;">$thirdVal</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_third <span style="color: #339933;">=</span> <span style="color: #000088;">$thirdVal</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You have noticed that all setters have one line in common <strong>return $this;</strong> and this is how you set your methods to be able to chain. Example of usage follows in next snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$phpFluentInterface</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Php_Fluent_Interface_Demo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$phpFluentInterface</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFirst</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSecond</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setThird</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I hope that this simple example is enough to get you started if you still haven&#8217;t tried PHP fluent interface. Good luck!</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/php-fluent-interface/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/php-fluent-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework CSRF protection</title>
		<link>http://stuntsnippets.com/zend-framework-csrf-protection/</link>
		<comments>http://stuntsnippets.com/zend-framework-csrf-protection/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 17:23:20 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CSRF]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1126</guid>
		<description><![CDATA[CSRF or Cross-Site Request Forgery is basically a way of requesting an unauthorized commands from a website by using an authorised user. For example: Let&#8217;s assume that you are logged in on your blog and I know that. I could then send you an e-mail with following content: &#60;img src=&#8221;http://yoursite.com/?action=delete-article&#038;id=12&#8243; /&#62;. Although you wouldn&#8217;t see [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-csrf-protection%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-csrf-protection%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>CSRF or Cross-Site Request Forgery is basically a way of requesting an unauthorized commands from a website by using an authorised user.</p>
<p>For example: Let&#8217;s assume that you are logged in on your blog and I know that. I could then send you an e-mail with following content: &lt;img src=&#8221;http://yoursite.com/?action=delete-article&#038;id=12&#8243; /&gt;.</p>
<p>Although you wouldn&#8217;t see the image &#8211; request for deleting article with id 12 would be sent. This can be prevented by setting up a hidden input in your Zend Framework form with a value that will be posted with your request and validated when request is sent in order to prevent possible CSRF attack.</p>
<p>It is quite easy in Zend Framework to set CSRF protection and here is the snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hash'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'csrf_token'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'salt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'stunt@c0d3rs~!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In order to see a full example of <strong>Zend Framework CSRF protection</strong> I have prepared an example of simple form with no elements on it.</p>
<h3>Zend Framework CSRF protection</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Stunt_Form <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hash'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'csrf_token'</span><span style="color: #339933;">,</span>
                    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'salt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'stunt@c0d3rs~!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/zend-framework-csrf-protection/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/zend-framework-csrf-protection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Analytics results on your website</title>
		<link>http://stuntsnippets.com/google-analytics-results-on-your-website/</link>
		<comments>http://stuntsnippets.com/google-analytics-results-on-your-website/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 15:29:58 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1089</guid>
		<description><![CDATA[Have you ever wondered if you could let user see his website visits on his own admin page without having to leave backend of his system? I have&#8230; Actually &#8211; the CMS I made for my clients (written in Zend Framework) has this option for quite some time, and users love it. So &#8211; in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fgoogle-analytics-results-on-your-website%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fgoogle-analytics-results-on-your-website%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Have you ever wondered if you could let user see his website visits on his own admin page without having to leave backend of his system? I have&#8230;</p>
<p>Actually &#8211; the CMS I made for my clients (written in Zend Framework) has this option for quite some time, and users love it.</p>
<p>So &#8211; in order to implement this function on your own website (written in Zend Framework) I have shared following code. First &#8211; copy model:</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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Default_Model_Analytics
<span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> auth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_email@domain.com&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$passwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> Zend_Gdata_ClientLogin<span style="color: #339933;">::</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$passwd</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;analytics&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_CaptchaRequiredException <span style="color: #000088;">$cre</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'URL of CAPTCHA image: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Token ID: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_AuthException <span style="color: #000088;">$ae</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Problem authenticating: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ae</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exception</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Gdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdClient</span><span style="color: #339933;">,</span> <span style="color: #000088;">$startDate</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2010-07-01'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$endDate</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ga:region&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ga:city&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metrics</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ga:visits&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ga:pageviews&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdClient</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$endDate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$endDate</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$reportURL</span>	<span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;https://www.google.com/analytics/feeds/data?ids=ga:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$yourAccountNumber</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;dimensions=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">@</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;metrics=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">@</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$metrics</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;start-date=<span style="color: #006699; font-weight: bold;">{$startDate}</span>&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;end-date=<span style="color: #006699; font-weight: bold;">{$endDate}</span>&amp;&quot;</span> <span style="color: #339933;">.</span>
							<span style="color: #0000ff;">&quot;sort=-ga:visits&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$gdClient</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFeed</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reportURL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$titleRow</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// To output a row of column labels</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rep</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$titleRow</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;">$rep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionElements</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$elem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$titles</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionAttributes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;td&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titles</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$titleRow</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionElements</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$elem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">extensionAttributes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;tr&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;td&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/td&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/tr&gt;<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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/table&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Caught exception: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Message: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<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>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Then &#8211; run following code in your controller so user can see Analytics results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$analyticsModel</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Analytics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//get: date start and date end of Analytics results</span>
<span style="color: #000088;">$postParams</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParams</span><span style="color: #009900;">&#40;</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$gdata</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$analyticsModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">auth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$analyticsModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gdata</span><span style="color: #339933;">,</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'year1'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month1'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-01'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'year2'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$postParams</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month2'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-31'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><em>Notice:</em> This is not the proper way to write code in Zend Framework, but is used as an example.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/google-analytics-results-on-your-website/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/google-analytics-results-on-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Add attribute to customer in Magento</title>
		<link>http://stuntsnippets.com/add-attribute-to-customer-in-magento/</link>
		<comments>http://stuntsnippets.com/add-attribute-to-customer-in-magento/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 12:33:20 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[add attribute]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[customer]]></category>
		<category><![CDATA[Magento customer]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1080</guid>
		<description><![CDATA[Simple question that developers in Magento sometimes ask is: &#8220;How can I add custom attribute to customers when using Magento&#8221;. The answer is simple &#8211; it is shown in the snippet below. But before you use it lets imagine what we want to do&#8230; Lets say that we want to give discount to users that [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fadd-attribute-to-customer-in-magento%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fadd-attribute-to-customer-in-magento%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Simple question that developers in Magento sometimes ask is: &#8220;How can I add custom attribute to customers when using Magento&#8221;. The answer is simple &#8211; it is shown in the snippet below. But before you use it lets imagine what we want to do&#8230; Lets say that we want to give discount to users that payed by using preferred method, and that we want to be able to change that <strong>customer attribute</strong> in our <strong>Magento</strong> admin. So &#8211; we should run following code &#8211; once &#8211; anywhere in your Magento installation.</p>
<p>After you have run this script, your Magento customer will have new attribute in administration area. You will be able to access it from your code and allow administrators to change this attribute. This added customer attribute in your Magento admin will have label &#8220;Preferred payment method&#8221;, and variable name &#8220;client_payment_method&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$setup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mage_Eav_Model_Entity_Setup<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'core_setup'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$setup</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'customer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'client_payment_method'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'label'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Preferred payment method'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'type'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'input'</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'visible'</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'required'</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'position'</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once you ran this script &#8211; you can delete it from your code. Enjoy the functionality you have achieved with custom added attribute to Magento customer!</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/add-attribute-to-customer-in-magento/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/add-attribute-to-customer-in-magento/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zend Framework get domain name</title>
		<link>http://stuntsnippets.com/zend-framework-get-domain-name/</link>
		<comments>http://stuntsnippets.com/zend-framework-get-domain-name/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 15:30:09 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1042</guid>
		<description><![CDATA[There is an easy way to get current domain name in Zend Framework. To get domain name in Zend Framework &#8211; use following snippet in your view file. echo $this-&#62;serverUrl&#40;&#41;;]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-get-domain-name%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fzend-framework-get-domain-name%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>There is an easy way to get current domain name in Zend Framework. To get domain name in Zend Framework &#8211; use following snippet in your view file.</p>

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

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/zend-framework-get-domain-name/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/zend-framework-get-domain-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get number of Twitter followers with PHP</title>
		<link>http://stuntsnippets.com/get-number-twitter-followers-php/</link>
		<comments>http://stuntsnippets.com/get-number-twitter-followers-php/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 14:07:33 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[get number of Twitter followers]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=1020</guid>
		<description><![CDATA[Get number of Twitter followers using this PHP snippet is easy. Once you get all the data from Twitter, you just have to isolate followers_count. Besides followers count, you can extract following data as well: id name screen_name location description profile_image_url url friends_count created_at lang Now, finally, to get number of Twitter followers, use following [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fget-number-twitter-followers-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fget-number-twitter-followers-php%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Get number of Twitter followers</strong> using this PHP snippet is easy. Once you get all the data from Twitter, you just have to isolate <em>followers_count</em>. Besides followers count, you can extract following data as well:</p>
<ol>
<li>id</li>
<li>name</li>
<li>screen_name</li>
<li>location</li>
<li>description</li>
<li>profile_image_url</li>
<li>url</li>
<li>friends_count</li>
<li>created_at</li>
<li>lang</li>
</ol>
<p>Now, finally, to <strong>get number of Twitter followers</strong>, use following snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$screenName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'stuntsnippets'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$getData</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'followers_count'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://twitter.com/users/show.xml?screen_name='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$screenName</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;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$getData</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;(.*)&lt;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</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: #b1b100;">echo</span> <span style="color: #000088;">$match</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>Notice: Don&#8217;t forget to do some <a href="http://stuntsnippets.com/php-caching/">PHP caching</a> for your script. You don&#8217;t want to have too many requests toward Twitter.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/get-number-twitter-followers-php/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/get-number-twitter-followers-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Magento static block</title>
		<link>http://stuntsnippets.com/magento-static-block/</link>
		<comments>http://stuntsnippets.com/magento-static-block/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 15:30:51 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[Mage]]></category>
		<category><![CDATA[static block]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=991</guid>
		<description><![CDATA[Have you ever wondered how to display Magento static block inside your app/store? It is an easy task, so just use following snippet. First, you have to create your static block in Magento admin, and than use its name to load it in your web application. 1 2 $block = Mage::getModel&#40;'cms/block'&#41;-&#62;setStoreId&#40;Mage::app&#40;&#41;-&#62;getStore&#40;&#41;-&#62;getId&#40;&#41;&#41;-&#62;load&#40;&#34;Magento_static_block_name&#34;&#41;; echo $block;]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fmagento-static-block%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fmagento-static-block%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Have you ever wondered how to display Magento static block inside your app/store? It is an easy task, so just use following snippet. First, you have to create your static block in Magento admin, and than use its name to load it in your web application.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$block</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cms/block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStoreId</span><span style="color: #009900;">&#40;</span>Mage<span style="color: #339933;">::</span><span style="color: #004000;">app</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Magento_static_block_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$block</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/magento-static-block/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/magento-static-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP contact form</title>
		<link>http://stuntsnippets.com/php-contact-form/</link>
		<comments>http://stuntsnippets.com/php-contact-form/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 12:05:11 +0000</pubDate>
		<dc:creator>Dejan Jacimovic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[contact form]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[PHP contact form]]></category>

		<guid isPermaLink="false">http://stuntsnippets.com/?p=966</guid>
		<description><![CDATA[So, almost every website needs contact form - is it a real contact form or just a feedback from your users. We need to make a contact form as easy to deal with as possible.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin: 10px; clear: both;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-contact-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstuntsnippets.com%2Fphp-contact-form%2F&amp;style=normal&amp;service=cli.gs&amp;service_api=b67df0a250a7f070ac77eb0b22b289fc&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>So, almost every website needs contact form &#8211; is it a real contact form or just a feedback from your users. We need to make a contact form as easy to deal with as possible. There is a simple way of dealing with all elements on your contact form by doing a simple trick. We are going to use a name convention to easily get all elements entered by user, to wrap them, and finally &#8211; to get them sent to the website owner.</p>
<p>First we shall start with a little bit of HTML as it is necessary for our contact form to live. Here &#8211; we are going to use our name convention to easily deal with details.</p>
<p>HTML contact form:</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
25
26
27
28
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;contact.php&quot; method=&quot;post&quot;&gt;
&lt;dl class=&quot;php_contact_form&quot;&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[email]&quot;&gt;E-mail:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;text&quot; name=&quot;php_contact_form[email]&quot; /&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[subject]&quot;&gt;Subject:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;text&quot; name=&quot;php_contact_form[subject]&quot; /&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
		&lt;label for=&quot;php_contact_form[message]&quot;&gt;Message:&lt;/label&gt;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;textarea type=&quot;text&quot; name=&quot;php_contact_form[message]&quot; &gt;&lt;/textarea&gt;
	&lt;/dd&gt;
	&lt;dt&gt;
&nbsp;
	&lt;/dt&gt;
	&lt;dd&gt;
		&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
	&lt;/dd&gt;
&lt;/dl&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Now &#8211; you might have noticed that all names are in &#8220;array&#8221; naming convention. They all belong to a single parameter that is going to be posted: <strong>php_contact_form</strong>.</p>
<p>As we are posting data from our PHP contact form to a contact.php, this is how contact.php should look like:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//if there is any data posted - we should process it</span>
<span style="color: #b1b100;">if</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;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//we shall be sending e-mail with data from our php contact form,</span>
  <span style="color: #666666; font-style: italic;">//so let just fill in a message</span>
  <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'php_contact_form'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$headers</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MIME-Version: 1.0'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$headers</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Content-type: text/html; charset=utf-8'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//now that we have a message from our php contact form</span>
  <span style="color: #666666; font-style: italic;">//we should send it to recipient</span>
  <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact@domain.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'New message from our php contact form'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$headers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Thank you for contacting us!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Notice:</strong> Real contact form should have some form of validation on both client and server side. It should also be protected with (at least) <a href="http://stuntsnippets.com/simple-php-captcha/">simple php captcha</a> to prevent spam bots from sending a large amount of SPAM mail. However &#8211; this is just an example of how PHP contact form can be made for easy handling.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin: 10px 0;width:68px;"><g:plusone size="standard" count="1" href="http://stuntsnippets.com/php-contact-form/"></g:plusone></div>]]></content:encoded>
			<wfw:commentRss>http://stuntsnippets.com/php-contact-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

