{"id":1448,"date":"2022-08-30T15:16:37","date_gmt":"2022-08-30T15:16:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/19\/facebook-api-get-random-friend-with-php-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:37","modified_gmt":"2022-08-30T15:16:37","slug":"facebook-api-get-random-friend-with-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/facebook-api-get-random-friend-with-php-collection-of-common-programming-errors\/","title":{"rendered":"Facebook API: get random friend with PHP-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to obtain a random friend. I found this thread on SO and it looks it should work. Here is my code:<\/p>\n<pre><code>            $config = array(\n                'appId'  =&gt; APP_ID,\n                'secret' =&gt; SECRET,\n                'cookie' =&gt; true,\n            );\n            $facebook = new Facebook($config);\n\n            $params = array(\n                'method' =&gt; 'fql.query',\n                'query' =&gt; \"SELECT uid, name, sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) order by rand() limit 1\",\n            );\n\n            $result = $facebook-&gt;api($params);\n            echo print_r($result);\n<\/code><\/pre>\n<p>But I am getting the error:<\/p>\n<pre><code>Fatal error: Uncaught Exception: 102: Requires user session thrown in \/home\/path\/to\/base_facebook.php on line 1050\n<\/code><\/pre>\n<p>Where is the problem? I&#8217;ve tried to search possible problem, but I wasn&#8217;t much successful&#8230;<\/p>\n<ol>\n<li>\n<p>Yes, as Jason commented, looks like you&#8217;ve not got an authenticated user.<\/p>\n<p>Since you&#8217;re using the PHP SDK, the quickest way to fix this is as follows<\/p>\n<pre><code>$config = array(\n    'appId'  =&gt; APP_ID,\n    'secret' =&gt; SECRET,\n    'cookie' =&gt; true,\n);\n$facebook = new Facebook($config);\n\n\/\/This will redirect the user to a login\/authentication dialog.\nif (!$facebook-&gt;getUser()) {\n    echo \"window.location = ' . $facebook-&gt;getLoginUrl() .'\";\n    exit();\n}\n\n$params = array().. etc, the rest of your code\n<\/code><\/pre>\n<p>Lack of best practice code notwithstanding &#8211; That should do the trick<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-19 13:17:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to obtain a random friend. I found this thread on SO and it looks it should work. Here is my code: $config = array( &#8216;appId&#8217; =&gt; APP_ID, &#8216;secret&#8217; =&gt; SECRET, &#8216;cookie&#8217; =&gt; true, ); $facebook = new Facebook($config); $params = array( &#8216;method&#8217; =&gt; &#8216;fql.query&#8217;, &#8216;query&#8217; =&gt; &#8220;SELECT uid, name, sex FROM user [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1448","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1448"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1448\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}