{"id":1601,"date":"2009-03-12T22:52:33","date_gmt":"2009-03-12T21:52:33","guid":{"rendered":"http:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/?p=1601"},"modified":"2009-03-15T22:51:05","modified_gmt":"2009-03-15T21:51:05","slug":"updating-profile-boxes-with-a-facebook-app","status":"publish","type":"post","link":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/2009\/03\/updating-profile-boxes-with-a-facebook-app\/","title":{"rendered":"Updating profile boxes with a facebook app"},"content":{"rendered":"<p>Hi there!<\/p>\n<p>A few days ago, I started to write <strong>my first own facebook application<\/strong>. It all went quite okay until one certain point: I wanted my application to update the profile box of the user, which uses the app at that single moment. And that wasn&#8217;t as easy, as I thought.<\/p>\n<p>The problem was: after the profile box has been updated, <strong>all profile boxes of all users had the same content<\/strong> &#8211; the content of the user who last updated his profile box.<\/p>\n<p>I will come to the solution later. First of all, a quick explanation of what I needed to do:<\/p>\n<ol>\n<li>I <strong>read the documentation<\/strong> of facebook&#8217;s application guide to understand the <a title=\"API\" href=\"http:\/\/wiki.developers.facebook.com\/index.php\/API\" target=\"_blank\">API<\/a> and the <a title=\"FBML\" href=\"http:\/\/wiki.developers.facebook.com\/index.php\/FBML\" target=\"_blank\">FBML language<\/a><\/li>\n<li>I <strong>set up my own application<\/strong> using the <a title=\"step-by-step guide\" href=\"http:\/\/developers.facebook.com\/get_started.php\" target=\"_blank\">step-by-step guide<\/a><\/li>\n<li>I <strong>set up my new application on my own host<\/strong> including facebook&#8217;s PHP libraries<\/li>\n<\/ol>\n<p><strong>Here&#8217;s the code step-by-step <\/strong>(just put all sniplets together)<strong>:<\/strong><\/p>\n<p><strong><code>Code sniplet #1<\/code><\/strong><\/p>\n<pre class=\"brush: php\">require_once &#039;facebook-platform\/php\/facebook.php&#039;;\r\n\r\n$appid = &#039;xxxxxxxxxxxx&#039;;\r\n$appapikey = &#039;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#039;;\r\n$appsecret = &#039;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#039;;\r\n\r\n$facebook = new Facebook($appapikey, $appsecret);\r\n$user_id = $facebook-&gt;require_login();<\/pre>\n<p><strong>Above:<\/strong> first of all, we need to include facebook&#8217;s <strong>PHP libraries<\/strong>, define several <strong>variables<\/strong> for our application, <strong>initialize our facebook object<\/strong> and <strong>retrieve the UID<\/strong> of the user who currently uses the application.<\/p>\n<p><strong><code>Code sniplet #2<\/code><\/strong><\/p>\n<pre class=\"brush: php\">$fb_box = &quot;&lt;H4 class=&#039;box_header clearfix&#039;&gt;&lt;span&gt;&quot;;\r\n$fb_box .= &quot;Hello World!&quot;;\r\n$fb_box .= &quot;&lt;\/span&gt;&lt;\/H4&gt;&quot;;\r\n$fb_box .= &quot;&lt;div&gt;I am &quot;;\r\n$fb_box .= &quot;&lt;fb:name uid=\\&quot;$user_id\\&quot; useyou=\\&quot;false\\&quot; \/&gt;.&quot;;\r\n$fb_box .= &quot;Nice you&#039;re on my page.&lt;\/div&gt;&quot;;\r\n$fb_box .= &quot;Some more content in HTML or FBML...&quot;;<\/pre>\n<p><strong>Above:<\/strong> we then define the <strong>content<\/strong> which we want to be displayed <strong>within the profile box<\/strong>. In this examble, it will be the name of the user, greeting the visitor.<\/p>\n<p><strong><code>Code sniplet #3<\/code><\/strong><\/p>\n<pre class=\"brush: php\">$fb_box_handle = &#039;pb_&#039; . $appid . &#039;_&#039; . $user_id;<\/pre>\n<p><strong>Above:<\/strong> this is <strong>important<\/strong>! At this point, I got stuck for a while. The reason was, facebook <strong>caches<\/strong> profile boxes content until the application&#8217;s canvas page is requested for the next time. <strong>The handle is very important<\/strong> for that caching mechanism, since it has to be <strong>a unique identifier<\/strong> for each application and each user. For that reason, I simply use the prefix &#8216;<strong>pb_<\/strong>&#8216; followed by the <strong>applications ID<\/strong> (defined above) and the <strong>UID<\/strong> of the current user (also defined above)!<\/p>\n<p><strong><code>Code sniplet #4<\/code><\/strong><\/p>\n<pre class=\"brush: php\">$facebook-&gt;api_client-&gt;call_method(&#039;facebook.profile.setFBML&#039;,\r\narray(\r\n&#039;api_key&#039; =&gt; $appapikey,\r\n&#039;v&#039; =&gt; &#039;1.0&#039;,\r\n&#039;uid&#039; =&gt; $user_id,\r\n&#039;profile&#039; =&gt; &#039;&lt;fb:narrow&gt;&lt;fb:ref handle=&quot;&#039; . $fb_box_handle . &#039;&quot; \/&gt;&lt;\/fb:narrow&gt;&#039;,\r\n&#039;profile_main&#039; =&gt; &#039;&lt;fb:ref handle=&quot;&#039; . $fb_box_handle . &#039;&quot; \/&gt;&#039;,\r\n)\r\n);<\/pre>\n<p><strong>Above:<\/strong> the rest is quite simple. We make an <strong>API call<\/strong> to facebook and send our <strong>application key<\/strong>, the <strong>protocol version<\/strong>, the <strong>UID<\/strong> of the facebook user whose profile box ought to be updated and, of course, the <strong>content<\/strong> for that box. The content will be sent as the <strong>reference handle<\/strong> we defined earlier.<\/p>\n<p>If you want to update multiple profile boxes with one single API call, I guess, this could work if you send an array containing the UIDs. If not, you can still get all those UIDs, walk through the array and perform multiple API calls. I read somewhere, some users had problem updating large numbers of profiles&#8230; Just give it a try.<\/p>\n<p>Comments (if it worked for you or even if it didn&#8217;t) are appreciated \ud83d\ude42<\/p>\n<p><code>UPDATE because of comment #2:<\/code><\/p>\n<p>The example above is about updating content in a users profile, <strong>not<\/strong> about setting up a profile box on a profile. You should read the manual carefully.<\/p>\n<p>A user has to approve a certain application to create a profile box. Therefore, you need to <strong>render a button<\/strong>, which sets up the box.  You can easily do this by:<\/p>\n<pre class=\"brush: php\">$appapikey = &#039;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#039;;\r\n$appsecret = &#039;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#039;;\r\n$facebook = new Facebook($appapikey, $appsecret);\r\n$user_id = $facebook-&gt;require_login();\r\n$facebook-&gt;api_client-&gt;profile_setFBML($appapikey, $user_id, &#039;profile&#039;, NULL, &#039;mobile_profile&#039;, &#039;profile_main&#039;);\r\necho &quot;&lt;fb:add-section-button section=&#039;profile&#039; \/&gt;&quot;;<\/pre>\n<p>The last line renders the button, you want to have on your canvas page.<\/p>\n<p>Good luck!  Thomas<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi there! A few days ago, I started to write my first own facebook application. It all went quite okay until one certain point: I wanted my application to update the profile box of the user, which uses the app at that single moment. And that wasn&#8217;t as easy, as I thought. The problem was: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"footnotes":""},"categories":[11],"tags":[231,211,221,241,151],"class_list":["post-1601","post","type-post","status-publish","format-standard","hentry","category-tech","tag-api","tag-facebook","tag-fbml","tag-php","tag-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/posts\/1601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/comments?post=1601"}],"version-history":[{"count":46,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/posts\/1601\/revisions"}],"predecessor-version":[{"id":2081,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/posts\/1601\/revisions\/2081"}],"wp:attachment":[{"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/media?parent=1601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/categories?post=1601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thomasgericke.de\/v4\/interactive\/blog\/wp-json\/wp\/v2\/tags?post=1601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}