<?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>Emanuele Feronato &#187; Php</title>
	<atom:link href="http://www.emanueleferonato.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Triqui arcade theme for WordPress updated</title>
		<link>http://www.emanueleferonato.com/2011/10/31/triqui-arcade-theme-for-wordpress-updated/</link>
		<comments>http://www.emanueleferonato.com/2011/10/31/triqui-arcade-theme-for-wordpress-updated/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 18:19:55 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4790</guid>
		<description><![CDATA[WordPress Arcade theme and plugin updated, by Luca Prosepio]]></description>
			<content:encoded><![CDATA[<p>While I am working on a new version of my <a href="http://www.emanueleferonato.com/triqui-mochiads-arcade-theme-for-wordpress-official-page/">WordPress arcade theme</a>, there are some known issues in the current theme which should be fixed.</p>
<p><strong>Luca Prosepio</strong> updated both the <a href="http://www.emanueleferonato.com/triqui-mochiads-arcade-theme-for-wordpress-official-page/">theme</a> and the <a href="http://www.emanueleferonato.com/triqui-mochiads-arcade-plugin-for-wordpress-official-page/">plugin</a> and you can see them working on <a href="http://www.giochi-gratuiti.com/" target="_blank">www.giochi-gratuiti.com</a>.</p>
<p><a href = "/wp-content/uploads/2011/10/themeplugin.zip">Download both the theme and the plugin</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/10/31/triqui-arcade-theme-for-wordpress-updated/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using PHP with PayPal&#8217;s IPN (Instant PayPal Notification) to automate your digital delivery</title>
		<link>http://www.emanueleferonato.com/2011/09/28/using-php-with-paypals-ipn-instant-paypal-notification-to-automate-your-digital-delivery/</link>
		<comments>http://www.emanueleferonato.com/2011/09/28/using-php-with-paypals-ipn-instant-paypal-notification-to-automate-your-digital-delivery/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 20:28:29 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Monetize]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4703</guid>
		<description><![CDATA[You will learn how to use PayPal's IPN (Instant PayPal Notification) to automate digital delivery, without using third parties services or expensive plugins]]></description>
			<content:encoded><![CDATA[<p>When you want to sell something through the internet, PayPal is your best choice. Almost every website selling stuff or subscriptions use PayPal to manage its transactions.</p>
<p>While PayPal back-end allows users to fully manage their accounts, there are some features it still lacks.</p>
<p><strong>Real world example</strong>: let&#8217;s say you are selling a digital good, such as the source code of your latest Flash game, or an ebook.</p>
<p>Since there is no shipment as the buyer will download it or receive it by email, he expects to get the book/game a few minutes after the payment.</p>
<p>That&#8217;s ok if you check your PayPal account every 10 minutes, manually sending emails to buyers, but once in a while you need to sleep&#8230; or have a shower&#8230; who will send your book then?</p>
<p>That&#8217;s why services like <a href="https://www.payloadz.com/" target="_blank">PayLoadz</a> come into play, managing transactions and digital delivery for a monthly subscription and a per transition fee.</p>
<p>If you don&#8217;t want to use third part services, PayPal offers the <strong><a href="https://www.paypal.com/ipn" target="_blank">Instant Payment Notification</a></strong> to automate your digital deliveries.</p>
<p>Instant Payment Notification (IPN) is PayPal&#8217;s message service that sends a notification when a transaction is affected. Once IPN is integrated, sellers can automate their back office so they don’t have to wait for payments to come in to trigger order fulfillment.</p>
<p>The process is simple: once you have a PayPal account, in your profile page click &#8220;Instant Payment Notification Preferences&#8221; under &#8220;Selling Preferences&#8221;, and you will be asked for an URL.</p>
<p>This is the URL of a page on your server which will act as a listener for PayPal payments.</p>
<p>Then, all you have to do is write your own listener based on the information you will find in the <a href="https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&#038;content_ID=developer/e_howto_admin_IPNImplementation" target="_blank">official docs</a>. Wait, I forgot to tell you three things:</p>
<p>1) The script is wrong, it will give an error because there are missing curly brackets</p>
<p>2) The given address where to test the script is wrong, it&#8217;s not <code>www.sandbox.paypal.com</code> anymore</p>
<p>3) The script (once corrected) won&#8217;t work on every server since relies on <code>fsockopen</code>. In my opinion it would have been better to use cURL</p>
<p>So, this is the script I suggest to use:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;cmd=_notify-validate&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$varname</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$varvalue</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;<span style="color: #006699; font-weight: bold;">$varname</span>: <span style="color: #006699; font-weight: bold;">$varvalue</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>  
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_magic_quotes_gpc'</span><span style="color: #009900;">&#41;</span> and <span style="color: #990000;">get_magic_quotes_gpc</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;">$varvalue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varvalue</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #000088;">$request</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;<span style="color: #006699; font-weight: bold;">$varname</span>=<span style="color: #006699; font-weight: bold;">$varvalue</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span>CURLOPT_URL<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;https://www.sandbox.paypal.com/cgi-bin/webscr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//curl_setopt($ch,CURLOPT_URL,&quot;https://www.paypal.com&quot;);</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span>CURLOPT_POST<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span>CURLOPT_POSTFIELDS<span style="color: #339933;">,</span><span style="color: #000088;">$request</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;">$ch</span><span style="color: #339933;">,</span>CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span>CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;VERIFIED&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// verified payment</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;INVALID&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// invalid/fake payment</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
		<span style="color: #666666; font-style: italic;">// any other case (such as no response, connection timeout...)</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Simply comment/uncomment <strong>lines 15-16</strong> if you want to use it in sandbox or real mode.</p>
<p>Once the payment is <code>VERIFIED</code>, you can send the email with the game/book/activation code to the email address you will find in <code>POST[payer_email]</code> variable. You can find the complete list of passed variables at <a href="https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&#038;content_ID=developer/e_howto_admin_IPNReference" target="_blank">this page</a>.</p>
<p>To test it, once you activated your IPN and installed the script on your web server, register as a <a href="https://developer.paypal.com/" target="_blank">PayPal developer</a>, then test your IPN in the <a href="https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session" target="_blank">sandbox page</a>.</p>
<p>In a few days, a fully working example on this site, with some deals at an extraordinary price.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/09/28/using-php-with-paypals-ipn-instant-paypal-notification-to-automate-your-digital-delivery/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to display (some) WordPress sidebar widgets in random order</title>
		<link>http://www.emanueleferonato.com/2011/06/23/how-to-display-some-wordpress-sidebar-widgets-in-random-order/</link>
		<comments>http://www.emanueleferonato.com/2011/06/23/how-to-display-some-wordpress-sidebar-widgets-in-random-order/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 14:28:50 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4333</guid>
		<description><![CDATA[A quick and dirty way to shuffle widgets position, since I did not find any plugin to do it.]]></description>
			<content:encoded><![CDATA[<p>Do you need a plugin to display WordPress sidebar widgets in random order? You won&#8217;t find it. Or you will find messy scripts which need to be tweaked to return some kind of result.</p>
<p>That&#8217;s why I made my 22 lines script to display my sidebar widgets in random order. And not just in &#8220;random order&#8221;, because I needed to keep some widgets in fixed positions, and having the remaining ones rotating.</p>
<p><strong>Motivation</strong></p>
<p>Once I <a href="http://www.emanueleferonato.com/2011/06/10/removing-all-ads-from-the-blog/">removed Adsense and network ads</a> from the blog I received a lot of sponsorship requests.</p>
<p>There&#8217;s nothing wrong with the ones I denied, it&#8217;s just they do not match with the content of the blog. </p>
<p>Anyway, as I am doing some minor changes to the blog, I am starting to place the supporters in the sidebar.</p>
<p>Every block is a widget created from the Appearence menu this way:</p>
<p><img src="/wp-content/uploads/2011/06/widgets.jpg" /></p>
<p>And what I want is having the first and the last widgets (respectively the twitter &#8220;Follow me&#8221; button and the Facebook fan widget) to remain fixed in their first and last position, while the remaining widget should be displayed in random order.</p>
<p>Saying it quick, I needed a script to display in random order widgets from the i-th to the j-th</p>
<p><strong>The theory</strong></p>
<p>If you theme is designed according to WP guidelines, your sidebar.php file should contain something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;sidebar&quot;&gt;
	&lt;ul&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<p>Where <code>dynamic_sidebar</code> function calls each of the active widget callbacks in order, and outputs the sidebar itself.</p>
<p>The generated html is something like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;li id=&quot;some_id&quot; class=&quot;widget widget_text&quot;&gt;1st Widget content&lt;/li&gt;
&lt;li id=&quot;some_id&quot; class=&quot;widget widget_text&quot;&gt;2nd Widget content&lt;/li&gt;
...
&lt;li id=&quot;some_id&quot; class=&quot;widget widget_text&quot;&gt;i-th Widget content&lt;/li&gt;</pre></div></div>

<p>At this time, since there aren&#8217;t <a target = "_blank" href="http://codex.wordpress.org/Plugin_API">hooks</a> to handle widget output, we have to save the generated html, break it into individual widgets&#8217; html, and mix it as required.</p>
<p><strong>The code</strong></p>
<p>The following code shouldn&#8217;t be copied and pasted, and assumes you haven&#8217;t lists inside your widgets. But it can give you a brief idea of what you&#8217;re supposed to do.</p>
<p>First, call your sidebar in <code>sidebar.php</code> this way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;sidebar&quot;&gt;
	&lt;ul&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>random_widgets<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sidebar'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<p>I am calling <code>random_widgets</code> function rather than <code>dynamic_sidebar</code>.</p>
<p>The arguments are the sidebar name, the index of the first widget to be shuffled and the index of the last widget to be shuffled. Every widget outside such indexes won&#8217;t be shuffled.</p>
<p><code>random_widgets</code> function is defined inside <code>functions.php</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> random_widgets<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sidebar</span><span style="color: #339933;">,</span><span style="color: #000088;">$random_start</span><span style="color: #339933;">,</span><span style="color: #000088;">$random_end</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$widgets</span> <span style="color: #339933;">=</span> dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sidebar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$widgets</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     	<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000088;">$widgets_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000088;">$head</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$widgets_array</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$random_start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    		<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$widgets_array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$random_start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$random_end</span><span style="color: #339933;">-</span><span style="color: #000088;">$random_start</span><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;">$tail</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$widgets_array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$random_end</span><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: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000088;">$new_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$head</span><span style="color: #339933;">,</span><span style="color: #000088;">$body</span><span style="color: #339933;">,</span><span style="color: #000088;">$tail</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;">$new_array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$widget</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;">$widget</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     	         <span style="color: #000088;">$final_html</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;li&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$widget</span><span style="color: #339933;">;</span>
     	     <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$final_html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$final_html</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What happens? I am saving the output inside a variable called <code>$html</code>. From such variable, I create an array with all widgets, break the array into sub arrays, shuffle the required array and rebuild the final code.</p>
<p>And that&#8217;s it, without installing any plugin. You can see the result refreshing the page, sidebar widgets will shuffle, with the only exception of the first and the last widgets, which remain in their place.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/06/23/how-to-display-some-wordpress-sidebar-widgets-in-random-order/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CakePHP 1.3 Application Development Cookbook</title>
		<link>http://www.emanueleferonato.com/2011/05/23/cakephp-1-3-application-development-cookbook/</link>
		<comments>http://www.emanueleferonato.com/2011/05/23/cakephp-1-3-application-development-cookbook/#comments</comments>
		<pubDate>Mon, 23 May 2011 09:51:26 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4170</guid>
		<description><![CDATA[<a href="http://link.packtpub.com/RtTf08" target = "_blank"><strong>CakePHP 1.3 Application Development Cookbook</strong></a> is the new book from Packt Publishing which will guide you through the creation of applications using CakePHP]]></description>
			<content:encoded><![CDATA[<p>If you are a PHP developer, you should know what is CakePHP.</p>
<p><a href="http://cakephp.org/" target = "_blank">CakePHP</a> is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.</p>
<p>While the framework has a lot of documentation and reference guides available for beginners, developing more sophisticated and scalable applications require a deeper knowledge of CakePHP features, a challenge that proves difficult even for well established developers.</p>
<p><a href="http://link.packtpub.com/RtTf08" target = "_blank"><strong>CakePHP 1.3 Application Development Cookbook</strong></a> will show you over 60 great recipes for developing, maintaining, and deploying web applications with CakePHP.</p>
<p><a href="http://link.packtpub.com/RtTf08" target = "_blank"><img src="/wp-content/uploads/2011/05/cakephp.jpg" /></a></p>
<p>This is a quick list of what you will learn with this book:</p>
<p>* Add authentication to an application to identify which controllers will need user access, using the Auth component and ACL component<br />
* Allow users to log in using either a user name, or an email, and let the system remember their account details<br />
* Enable and leverage the use of the Containable behavior to deal with model bindings<br />
* Make run time changes to model bindings for a better control of your model relationships<br />
* Perform relatively complex GROUP and COUNT queries using CakePHP&#8217;s built in find types and use virtual fields for a cleaner approach to SQL based calculations<br />
* Creating custom find operations, and paginating its result using CakePHP&#8217;s built in pagination support<br />
* Add multiple validation rules per field using built in validation rules in CakePHP<br />
* Allow users to search address records by proximity by adding location information to our own Address model using the Geocodable behavior<br />
* Implement your own datasource by providing a way to read from and post messages to a twitter account using OAuth as the authentication layer<br />
* Modify an existing datasource by adding better transaction support to the MySQL driver and implementing locking operations<br />
* Take advantage of the routing flexibility in CakePHP by creating your own route handlers<br />
* Build non interactive tasks that run in the command line, including the automation of tasks<br />
* Internationalize all areas of your application, including views and model records, to reach a wider audience</p>
<p>I am currently reading it and I find it very well written, with a lot of examples and pictures which will follow you through the creation of any kind of application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/05/23/cakephp-1-3-application-development-cookbook/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Executing php inside a WordPress widget without any plugin</title>
		<link>http://www.emanueleferonato.com/2011/04/11/executing-php-inside-a-wordpress-widget-without-any-plugin/</link>
		<comments>http://www.emanueleferonato.com/2011/04/11/executing-php-inside-a-wordpress-widget-without-any-plugin/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 09:16:10 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3976</guid>
		<description><![CDATA[A quick snippet of code to allow Text widgets to execute php code without using any plugin.]]></description>
			<content:encoded><![CDATA[<p>Sometimes in your WordPress theme you need to execute custom php code in a widget, because you want to display different information according to the category you are in, or simply because you need to execute a php script into that widget.</p>
<p>There are a lot of plugins doing this task, adding a new type of widget generally called &#8220;php widget&#8221;, but rather than installing a plugin this simple job can be done simply adding in <code>functions.php</code> file of your theme these few lines:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'widget_text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'execute_php'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> execute_php<span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</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;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;?php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;?&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$html</span><span style="color: #339933;">=</span><span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Which will turn the default Text widget into a php enabled widget.</p>
<p>Adding this feature directly to <code>functions.php</code> allows you to create a theme with this built in feature without the need of an external plugin. This is very useful when you plan to distribute your theme.</p>
<p>Let&#8217;s see how does it work:<span id="more-3976"></span></p>
<p><strong>Line 1</strong>: <code>add_filter</code> hooks a function to a specific filter action.</p>
<p>Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. </p>
<p>The first parameter is the name of the filter to hook (<code>widget_text</code>) while te second is the name of the function to be called when the filter is applied (<code>execute_php</code>). The third is the priority, and 100 should grant us a very low priority since lower numbers correspond with earlier execution, and the default value is 10.</p>
<p>widget_text is the filter applied to the widget text of the WordPress Text widget.</p>
<p>So in the end we are telling WordPress to run <code>execute_php</code> function when the content of the text widget is generated.</p>
<p><strong>Line 2</strong>: exexute_php function. Note the argument, the HTML generated by the Text widget.</p>
<p><strong>Line 3</strong>: looking for <code>&lt;?php</code> substring inside the HTML. If we find this substring, it means there is php to be executed inside the widget.</p>
<p>If you wonder why I am looking for <code>"&lt;"."?php"</code> rather than <code>"&lt;?php"</code>, it&#8217;s just to preserve the correct syntax highlighting in most editors.</p>
<p><strong>Line 4</strong>: <code>ob_start()</code> turns output buffering on. While output buffering is active no output is sent from the script, instead the output is stored in an internal buffer. This means the HTML produced by the Text widget is temporarily saved into an internal buffer.</p>
<p><strong>Line 5 </strong>: at this time we need to evaluate the HTML as if it was a php script. <code>eval</code> function does the job, but we need to add <code>&lt;></code> to the HTML (again, split in two to avoid highlight issues) because we must tell <code>eval</code> function the php could not start from the beginning of the HTML string.</p>
<p>An example: if my Text widget contains</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>everything will work fine because the Text starts with php. But in this case:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">your IP: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><code>eval</code> function will fail because everything before <code>&lt;?php</code> is parsed as php, returning an error. That&#8217;s why the text is changed to</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">?&gt;a value: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>to tell eval we don&#8217;t start with php code.</p>
<p><strong>Lines 6 and 7</strong>: once the php has been processed, we need to save the output buffer content, clean the output buffer and turn off output buffering.</p>
<p>Finally at <strong>line 9</strong> the HTML is returned, with the php correctly parsed.</p>
<p>And that&#8217;s all, without needing any plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/04/11/executing-php-inside-a-wordpress-widget-without-any-plugin/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Redesigning the blog</title>
		<link>http://www.emanueleferonato.com/2011/04/04/redesigning-the-blog/</link>
		<comments>http://www.emanueleferonato.com/2011/04/04/redesigning-the-blog/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 20:48:40 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Users contributions]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3955</guid>
		<description><![CDATA[Since the blog is almost five years old, I must seriously redesign it. The problem is: there are so many beautiful blog themes out there I really do not know how to redesign my blog. That&#8217;s why I am asking you some suggestions. How should this blog look like? Which features would you add? Help [...]]]></description>
			<content:encoded><![CDATA[<p>Since the blog is almost <strong>five years old</strong>, I must seriously redesign it. The problem is: there are so many beautiful blog themes out there I really do not know how to redesign my blog.</p>
<p>That&#8217;s why I am asking you some suggestions. How should this blog look like? Which features would you add?</p>
<p>Help me with your ideas and feedback and I&#8217;ll design the blog according to your suggestions, obviously publishing the source code as usual.</p>
<p>I have to say I don&#8217;t like that much the blogs with slideshows in the header, preferring minimalist design like <a target = "_blank" href="http://psd.tutsplus.com/">tutsplus network</a>.</p>
<p>Well, I will wait for some suggestions, I&#8217;d start from the header. Consider I want to keep the logo.</p>
<p>WordPress heroes, this is your time. Show me your ultimate theme.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/04/04/redesigning-the-blog/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Triqui MochiAds plugin fixed to 1.3</title>
		<link>http://www.emanueleferonato.com/2011/01/22/triqui-mochiads-plugin-fixed-to-1-3/</link>
		<comments>http://www.emanueleferonato.com/2011/01/22/triqui-mochiads-plugin-fixed-to-1-3/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:35:54 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3714</guid>
		<description><![CDATA[As promised, here I am with the Triqui MochiAds Arcade plugin for WordPress fixed to start feeding again. The problem is Mochi guys added a new field called &#8220;languages&#8221; that wasn&#8217;t supported by the database table. I hope you will keep on using the plugin, and I want to inform you I am writing the [...]]]></description>
			<content:encoded><![CDATA[<p>As promised, here I am with the <a href="http://www.emanueleferonato.com/triqui-mochiads-arcade-plugin-for-wordpress-official-page/">Triqui MochiAds Arcade plugin for WordPress</a> fixed to start feeding again.</p>
<p>The problem is Mochi guys added a new field called &#8220;languages&#8221; that wasn&#8217;t supported by the database table.</p>
<p>I hope you will keep on using the plugin, and I want to inform you I am writing the 2.0 version, with the capability to feed games from big portals such as Kongregate and Newgrounds.</p>
<p>If you have a wish list, this is the time to add your most wanted feature.</p>
<p>Also, I would like to post on my <a href="http://www.facebook.com/pages/Emanuele-Feronato/50374255154" target = "_blank">Facebook fan page</a> an album with all sites using the plugin or the theme, so if you want to see your site featured, send me a screenshot at info[at]emanueleferonato.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/01/22/triqui-mochiads-plugin-fixed-to-1-3/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Create a Facebook application like &#8220;Friend interview&#8221; &#8211; Step 3</title>
		<link>http://www.emanueleferonato.com/2010/11/24/create-a-facebook-application-like-friend-interview-step-3/</link>
		<comments>http://www.emanueleferonato.com/2010/11/24/create-a-facebook-application-like-friend-interview-step-3/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 17:35:58 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3527</guid>
		<description><![CDATA[Here we go with the 3rd step. In this tutorial you will learn how to publish a full featured feed post to your friends&#8217; wall, and to handle &#8220;umlauts&#8221; characters like &#8220;äöü&#8221; for the pleasure of our German readers, and for every user who has friends whose names contain special characters. This is the kind [...]]]></description>
			<content:encoded><![CDATA[<p>Here we go with the 3rd step. In this tutorial you will learn how to publish a full featured feed post to your friends&#8217; wall, and to handle &#8220;umlauts&#8221; characters like &#8220;äöü&#8221; for the pleasure of our German readers, and for every user who has friends whose names contain special characters.</p>
<p>This is the kind of feed you are going to publish:</p>
<p><img src="/wp-content/uploads/2010/11/fbfeed02.jpg" /></p>
<p>And this is the link of the application, just in case you want to have a look: <a href="http://apps.facebook.com/about_you_game/" target = "_blank">About you Facebook application</a></p>
<p>The code is almost similar to the one we used in <a href="http://www.emanueleferonato.com/2010/10/28/create-a-facebook-application-like-friend-interview-step-2/">step 2</a>:<span id="more-3527"></span></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
60
61
62
63
64
65
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;facebook.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'appId'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'157934074246374'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'secret'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'dontstealmysecretkey'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cookie'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #339933;">,</span><span style="color: #0000ff;">'domain'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'gamesalsa.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</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: #339933;">!</span><span style="color: #000088;">$session</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginUrl</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'canvas'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'fbconnect'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'req_perms'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'publish_stream'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'next'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'http://apps.facebook.com/about_you_game/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cancel_url'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'http://apps.facebook.com/about_you_game/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Redirecting to permission request...&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;top.location.href = '<span style="color: #006699; font-weight: bold;">$url</span>';&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>to_do<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Answer and publish&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$ch</span><span style="color: #339933;">=</span><span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;https://graph.facebook.com/<span style="color: #006699; font-weight: bold;">$_POST[user]</span>/feed&quot;</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;">$ch</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: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</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;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;access_token=<span style="color: #006699; font-weight: bold;">$session[access_token]</span>&amp;message=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;just answered a question about you:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;link=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://apps.facebook.com/about_you_game/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;picture=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.gamesalsa.com/aboutyou/about_you_icon.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;name=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Do you think <span style="color: #006699; font-weight: bold;">$_POST[name]</span> likes this app ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;caption=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;and the answer is...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;description=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>answer<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;actions=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{<span style="color: #000099; font-weight: bold;">\&quot;</span>name<span style="color: #000099; font-weight: bold;">\&quot;</span>: <span style="color: #000099; font-weight: bold;">\&quot;</span>Play now<span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span>link<span style="color: #000099; font-weight: bold;">\&quot;</span>: <span style="color: #000099; font-weight: bold;">\&quot;</span>http://apps.facebook.com/about_you_game/<span style="color: #000099; font-weight: bold;">\&quot;</span>}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$friends</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'me/friends'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$number_of_friends</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$random</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$number_of_friends</span><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;">$friend_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$friend_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://graph.facebook.com/<span style="color: #006699; font-weight: bold;">$friend_id</span>/picture?type=large&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&nbsp;
&lt;head&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=utf-8&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div style = &quot;width:513px;background-color:#edeff4;background-image:url('<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$img</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>');background-repeat:no-repeat;padding:5px 5px 5px 210px;font:normal 16px verdana&quot;&gt;
		Do you think &lt;span style = &quot;color:red;fnt-weight:bold&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/span&gt; likes this app?
		&lt;form method = &quot;post&quot; style =&quot;margin-top:20px&quot;&gt;
		     &lt;textarea name = &quot;answer&quot; style = &quot;border:1px solid #bdc7d8;width:100%;height:100px;font:normal 15px verdana&quot;&gt;&lt;/textarea&gt;
		     &lt;input type = &quot;hidden&quot; name = &quot;user&quot; value = &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_id</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		     &lt;input type = &quot;hidden&quot; name = &quot;name&quot; value = &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_name</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
		     &lt;input type =&quot;submit&quot; value = &quot;Answer and publish&quot; name =&quot;to_do&quot;/&gt; &lt;input type =&quot;submit&quot; value = &quot;Just answer&quot; name =&quot;to_do&quot;/&gt;
		&lt;/form&gt;
	&lt;/div&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
	google_ad_client = &quot;pub-2062493463858889&quot;;
	/* about you */
	google_ad_slot = &quot;0401513269&quot;;
	google_ad_width = 728;
	google_ad_height = 90;
	//--&gt;
	&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot;
	src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
	&lt;/script&gt;
&nbsp;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Using proper html tags such as <code>http-equiv</code> at <strong>line 38</strong> allow us to properly render special characters, while the key of this post is <strong>line 20</strong>, where we pass a lot more information than in <a href="http://www.emanueleferonato.com/2010/10/28/create-a-facebook-application-like-friend-interview-step-2/">step 2</a>.</p>
<p>Let&#8217;s see it in detail:</p>
<p><strong>access_token</strong>: it&#8217;s the access token required to publish on friends&#8217; wall. Its value must be</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span><span style="color: #009900;">&#91;</span>access_token<span style="color: #009900;">&#93;</span></pre></div></div>

<p><strong>message</strong>: it&#8217;s the message which will appear next to your name. I used</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;just answered a question about you:&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>because I wanted my friend to see &#8220;Emanuele Feronato just answered a question about you:&#8221;. I also used <code>urlencode</code> although it&#8217;s useless in this case, just not to forget to encode the string should I change it in the near future with another string with some special characters.</p>
<p><strong>picture</strong>: the absolute URL of the picture to display on the left of your post.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.gamesalsa.com/aboutyou/about_you_icon.png&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>name</strong>: it&#8217;s the name of the link you are publishing in the feed. This is where we will place the question.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Do you think <span style="color: #006699; font-weight: bold;">$_POST[name]</span> likes this app ?&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>link</strong>: it&#8217;s the link attached to the picture and the name. You may want to use the URL of the application itself.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://apps.facebook.com/about_you_game/&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>caption</strong>: the caption of the link, placed just under the link itself. Don&#8217;t write anything important here as it won&#8217;t be noticed that much since the blue color of the link grabs almost all attention. Perfect to write something like &#8220;and the answer is&#8230;&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;and the answer is...&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>description</strong>: it&#8217;s the description of the link and it&#8217;s the longest text you can enter. We&#8217;ll place the answer there.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>answer<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>actions</strong>: you can also add a custom action to the action footer of the post (where you can find &#8220;Like&#8221;, &#8220;Comment&#8221;, and so on). Actions should be added as a JSON list containing &#8220;name&#8221; and &#8220;link&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{<span style="color: #000099; font-weight: bold;">\&quot;</span>name<span style="color: #000099; font-weight: bold;">\&quot;</span>: <span style="color: #000099; font-weight: bold;">\&quot;</span>Play now<span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span>link<span style="color: #000099; font-weight: bold;">\&quot;</span>: <span style="color: #000099; font-weight: bold;">\&quot;</span>http://apps.facebook.com/about_you_game/<span style="color: #000099; font-weight: bold;">\&quot;</span>}&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>And here you, go, have a try with the application: <a href="http://apps.facebook.com/about_you_game/" target = "_blank">About you Facebook application</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/11/24/create-a-facebook-application-like-friend-interview-step-3/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Create a Facebook application like &#8220;Friend interview&#8221; &#8211; Step 2</title>
		<link>http://www.emanueleferonato.com/2010/10/28/create-a-facebook-application-like-friend-interview-step-2/</link>
		<comments>http://www.emanueleferonato.com/2010/10/28/create-a-facebook-application-like-friend-interview-step-2/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 08:55:02 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3450</guid>
		<description><![CDATA[In step 1 you learned how to authenticate and select a random friend of yours. In this second part you will learn how to get more information about your friends and how to post the answer on their wall. This is what you will get: and once you hit &#8220;Answer and publish&#8221; you will post [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/">step 1</a> you learned how to authenticate and select a random friend of yours.</p>
<p>In this second part you will learn how to get more information about your friends and how to post the answer on their wall.</p>
<p>This is what you will get:</p>
<p><img src="/wp-content/uploads/2010/10/friends01.jpg" /></p>
<p>and once you hit &#8220;Answer and publish&#8221; you will post on your friend&#8217;s wall this way:</p>
<p><img src="/wp-content/uploads/2010/10/friends02.jpg" /></p>
<p>You need to know how <a href="http://php.net/manual/en/book.curl.php" target = "_blank">cURL</a> works. I may consider publishing a tutorial about cURL if I have enough requests.</p>
<p>Let&#8217;s  jump straight to the code:<span id="more-3450"></span></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;facebook.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'appId'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'157934074246374'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'secret'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'onceiwasapornsuperstar'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cookie'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #339933;">,</span><span style="color: #0000ff;">'domain'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'gamesalsa.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</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: #339933;">!</span><span style="color: #000088;">$session</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginUrl</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'canvas'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'fbconnect'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'req_perms'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'publish_stream'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'next'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'http://apps.facebook.com/about_you_game/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cancel_url'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'http://apps.facebook.com/about_you_game/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Redirecting to permission request...&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;top.location.href = '<span style="color: #006699; font-weight: bold;">$url</span>';&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>to_do<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Answer and publish&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$ch</span><span style="color: #339933;">=</span><span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;https://graph.facebook.com/<span style="color: #006699; font-weight: bold;">$_POST[user]</span>/feed&quot;</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;">$ch</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: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</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;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;access_token=<span style="color: #006699; font-weight: bold;">$session[access_token]</span>&amp;message=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Do you think <span style="color: #006699; font-weight: bold;">$_POST[name]</span> likes this app ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;link=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://apps.facebook.com/about_you_game/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;name=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>answer<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$friends</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'me/friends'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$number_of_friends</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$random</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$number_of_friends</span><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;">$friend_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$friend_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://graph.facebook.com/<span style="color: #006699; font-weight: bold;">$friend_id</span>/picture?type=large&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&nbsp;
&lt;div style = &quot;width:513px;background-color:#edeff4;background-image:url('<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$img</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>');background-repeat:no-repeat;padding:5px 5px 5px 210px;font:normal 16px verdana&quot;&gt;
Do you think &lt;span style = &quot;color:red;fnt-weight:bold&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/span&gt; likes this app?
&lt;form method = &quot;post&quot; style =&quot;margin-top:20px&quot;&gt;
     &lt;textarea name = &quot;answer&quot; style = &quot;border:1px solid #bdc7d8;width:100%;height:100px;font:normal 15px verdana&quot;&gt;&lt;/textarea&gt;
     &lt;input type = &quot;hidden&quot; name = &quot;user&quot; value = &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_id</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
     &lt;input type = &quot;hidden&quot; name = &quot;name&quot; value = &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$friend_name</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
     &lt;input type =&quot;submit&quot; value = &quot;Answer and publish&quot; name =&quot;to_do&quot;/&gt; &lt;input type =&quot;submit&quot; value = &quot;Just answer&quot; name =&quot;to_do&quot;/&gt;
&lt;/form&gt;
&lt;/div&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Obviously I won&#8217;t comment HTML, so let&#8217;s dive into php exploring the new lines not already explained in <a href="http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/">step 1</a>.</p>
<p><strong>Line 10</strong>: getting the login url with some new parameters:</p>
<p><code>next</code>: the url to go to after a successful login<br />
<code>cancel_url</code>: the url to go to after the user cancels<br />
<code>req_perms</code>: comma separated list of requested extended perms</p>
<p>I am asking for <code>publish_stream</code> permission. It enables your application to post content, comments, and likes to a user&#8217;s stream and to the streams of the user&#8217;s friends. That&#8217;s what I need to publish something on my friends&#8217; wall.</p>
<p>You can find the full docs about permissions at <a href="http://developers.facebook.com/docs/authentication/permissions" target = "_blank">this official page</a>.</p>
<p><strong>Line 15</strong>: I am about to publish the answer about a random friend if the user clicked on &#8220;Answer and publish&#8221; button (<strong>line 43</strong>)</p>
<p><strong>Line 16</strong>: initializing a cURL session</p>
<p><strong>Line 17</strong>: setting the URL where to send the cURL request</p>
<p><strong>Line 18</strong>: saving the result of the cURL session in a string</p>
<p><strong>Line 19</strong>: setting cURL method to <code>POST</code></p>
<p><strong>Line 20</strong>: the core of the script: the variables to be passed in <code>POST</code>.</p>
<p><code>access_token</code>: the authorization token.<br />
<code>message</code>: the URL encoded message (&#8220;Do you think xxx likes this app?&#8221;)<br />
<code>link</code>: the link to the application<br />
<code>name</code>: the answer to the question</p>
<p><strong>Line 21</strong>: executing the cURL session</p>
<p><strong>Line 22</strong>: closing the cURL session</p>
<p>The big profile image on the left of the question can be obtained at <strong>line 29</strong>.</p>
<p>Test the application at <a href="http://apps.facebook.com/about_you_game/" target = "_blank">this page</a>.</p>
<p>Next time, generating and saving random questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/10/28/create-a-facebook-application-like-friend-interview-step-2/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Create a Facebook application like &#8220;Friend interview&#8221;</title>
		<link>http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/</link>
		<comments>http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 21:41:41 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3429</guid>
		<description><![CDATA[Do you know Friend Interview? It&#8217;s a Facebook application which asks a random question about a random friend of yours, and publish the result on his wall. I won&#8217;t publish a snapshot of such app since it&#8217;s full of rubbish and ads, but I am sure you know what I am talking about (and moreover [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know <strong><a href="http://apps.facebook.com/friendinterview/" target = "_blank">Friend Interview</a></strong>?</p>
<p>It&#8217;s a Facebook application which asks a random question about a random friend of yours, and publish the result on his wall. I won&#8217;t publish a snapshot of such app since it&#8217;s full of rubbish and ads, but I am sure you know what I am talking about (and moreover there&#8217;s the link!).</p>
<p>You should know how to create a Facebook application, anyway I&#8217;ll post a recap here.</p>
<p>1) Go to <a href="http://www.facebook.com/developers/" target = "_blank">http://www.facebook.com/developers/</a> and click on &#8220;Set Up New Application&#8221;.</p>
<p><img src="/wp-content/uploads/2010/10/intw01.jpg" /></p>
<p>2) Give a name to your application. You can give it any name as long as it does not infringe copyrights, agree to terms and click on &#8220;Create Application&#8221;.</p>
<p><img src="/wp-content/uploads/2010/10/intw02.jpg" /></p>
<p>3) On &#8220;Facebook Integration&#8221; panel give it a Canvas Page name (this must be unique), and in &#8220;Canvas URL&#8221; write the path on the folder on your server which will contain the application itself, set &#8220;Canvas Type&#8221; as &#8220;IFrame&#8221; and &#8220;IFrame Size&#8221; to &#8220;Auto-resize&#8221;, then press &#8220;Save Changes&#8221; and at the moment you are ready to create your application.</p>
<p><img src="/wp-content/uploads/2010/10/intw03.jpg" /></p>
<p>4) Download the Facebook API from <a href="http://github.com/facebook/php-sdk/archives/v2.1.2">GitHub</a> and copy <code>facebook.php</code> in the folder you specified in the Canvas URL.</p>
<p>Now everything is ready to develop your application. In this first part, we will ask for permissions and choose one random friend.<span id="more-3429"></span></p>
<p>Create a new file called <code>index.php</code> with this code:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;facebook.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'appId'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'157934074246374'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'secret'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'sorrythissecretkeywontwork'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cookie'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #339933;">,</span><span style="color: #0000ff;">'domain'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'gamesalsa.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</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: #339933;">!</span><span style="color: #000088;">$session</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginUrl</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'canvas'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'fbconnect'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Redirecting to permission request...&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text/javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;top.location.href = '<span style="color: #006699; font-weight: bold;">$url</span>';&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$friends</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'me/friends'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$number_of_friends</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;You have <span style="color: #006699; font-weight: bold;">$number_of_friends</span> friends&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$random</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$number_of_friends</span><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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;A random friend name: &lt;strong&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/strong&gt;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;A good question would be: do you think &lt;strong&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$friends</span><span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$random</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/strong&gt; likes this app?&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Let&#8217;s see its meaning:</p>
<p><strong>Line 3</strong>: including the required library.</p>
<p><strong>Line 5</strong>: initializing the Facebook application. In the array you must pass the following elements:</p>
<p><code>appId</code>: the application ID<br />
<code>secret</code>: the application secret<br />
<code>cookie</code>: (optional) boolean true to enable cookie support<br />
<code>domain</code>: (optional) domain for the cookie<br />
<code>fileUpload</code>: (optional) boolean indicating if file uploads are enabled</p>
<p><strong>Line 7</strong>:  getting the session object. At this point, the script splits in two paths, according to $session value.</p>
<p><strong>$session is NULL (lines 9-13)</strong></p>
<p>If <code>$session</code> is <code>NULL</code>, we need to redirect to the login url to let the player allow the application use his personal information. The redirection is made with a simple javascript, using <code>getLoginUrl</code>.</p>
<p><strong>$session is not NULL (lines 14-21)</strong></p>
<p>In this case I put in <code>$friends</code> array an array with all friends names and Ids, then I randomly pick one of them and display its name and a possible question.</p>
<p>And that&#8217;s it, this is what you&#8217;ll get:</p>
<p><img src="/wp-content/uploads/2010/10/kirenia.png" /></p>
<p>You can test it by yourself here: <a href="http://apps.facebook.com/about_you_game/" target="_blank">http://apps.facebook.com/about_you_game/</a></p>
<p>Now you know how to pick a random friend. During next step you will be able to answer a question about him and post it on its wall.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/10/20/create-a-facebook-application-like-friend-interview/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 10/18 queries in 0.012 seconds using disk: basic

Served from: www.emanueleferonato.com @ 2012-02-08 19:40:51 -->
