{"id":7924,"date":"2015-11-10T11:30:18","date_gmt":"2015-11-10T11:30:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/when-using-codeigniter-facebook-php-sdk-getuser-always-returns-0-open-source-projects-facebookarchive-facebook-php-sdk\/"},"modified":"2015-11-10T11:30:18","modified_gmt":"2015-11-10T11:30:18","slug":"when-using-codeigniter-facebook-php-sdk-getuser-always-returns-0-open-source-projects-facebookarchive-facebook-php-sdk","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/when-using-codeigniter-facebook-php-sdk-getuser-always-returns-0-open-source-projects-facebookarchive-facebook-php-sdk\/","title":{"rendered":"When using CodeIgniter + Facebook PHP SDK: getUser() always returns 0-open source projects facebookarchive\/facebook-php-sdk"},"content":{"rendered":"<p>I am attempting to use the Facebook PHP SDK in conjunction with CodeIgniter to allow users to login to my site using Facebook Connect. No matter what I try, getUser() always returns 0, even after (apparently) successful authentication via Facebook.<\/p>\n<p>CodeIgniter version: 2.1.3<\/p>\n<p>Facebook PHP SDK version: 3.2.2<\/p>\n<p>I have created a config file, facebook.php, in the application\/config folder and I am loading the Facebook PHP SDK via CodeIgniter&#8217;s $this-&gt;load-&gt;library(&#8230;) method. The library is indeed getting loaded and I can successfully call many of the get&#8230;() methods including getAccessToken(), getAppId() and getAppSecret(), all of which return their expected values.<\/p>\n<p>Here is a stripped down version of my login controller: (note that I also provide an alternate method of logging in via email, hence the CodeIgniter session code sprinkled throughout)<\/p>\n<pre><code>class Login extends CI_Controller {\n\n    public function __construct()\n    {\n        \/\/Call parent constructor\n        parent::__construct();\n\n        \/\/Magic sauce - not sure if this is required but a lot of other people\n        \/\/are recommending it to be included (happy to remove it if necessary)\n        parse_str($_SERVER['QUERY_STRING'], $_REQUEST);\n\n        \/\/Load facebook library\n        $facebook_config = $this-&gt;load-&gt;config('facebook');\n        $this-&gt;load-&gt;library('facebook', $facebook_config);\n    }\n\n   public function index()\n    {\n        \/\/Check if user is logged in\n        $user_id = $this-&gt;session-&gt;userdata('user_id');\n        $is_logged_in = $this-&gt;session-&gt;userdata('is_logged_in');\n\n        if(($is_logged_in) &amp;&amp; ($user_id != 0)) {\n            \/\/Logged in - redirect to game\n            redirect('game');\n        } else {\n            \/\/Not logged in\n            \/\/Get facebook login url\n            $facebook_data = array(\n                'redirect_uri' =&gt; 'hxxp:\/\/xxxxxxxx.com\/facebook_login\/',\n                'scope' =&gt; 'email'\n            );\n            $data['facebook_login_url'] = $this-&gt;facebook-&gt;getLoginUrl($facebook_data);\n            \/\/Redirect to login form\n            $this-&gt;load-&gt;view('login\/login_form', $data);\n        }\n    }\n\n    public function facebook_login()\n    {\n        \/\/Always returns 0!! Even after authenticating via facebook!\n        $facebook_user_id = $this-&gt;facebook-&gt;getUser();\n\n        if ($facebook_user_id) {\n            try {\n                $user_profile = $this-&gt;facebook-&gt;api('\/me');\n                print_r($user_profile);\n            } catch (FacebookApiException $e) {\n                echo $e-&gt;getMessage();\n            }\n        } else {\n            echo \"Could not log in with Facebook\";\n        }\n    }\n}\n<\/code><\/pre>\n<p>The stripped down view (login_form.php) is as follows:<\/p>\n<pre><code>\n    \n        Facebook Connect Test<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am attempting to use the Facebook PHP SDK in conjunction with CodeIgniter to allow users to login to my site using Facebook Connect. No matter what I try, getUser() always returns 0, even after (apparently) successful authentication via Facebook. CodeIgniter version: 2.1.3 Facebook PHP SDK version: 3.2.2 I have created a config file, facebook.php, [&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-7924","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7924","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=7924"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7924\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}