5 Replies - 587 Views - Last Post: 28 May 2015 - 05:20 PM Rate Topic: -----

#1 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 04:06 PM

Hey all I inherited some app code that needs to be fixed, it's a pretty basic video contest app. When a user clicks on the facebook page tab its supposed to load the app but I keep getting an error that Column 'uid' cannot be null which is correct its supposed to insert the facebook ID. I am somewhat of a PHP newbie and am not really familiar with the facebook API - Here is the code..

<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Users extends CI_Model
	{
        public $userdata;
        public $facebook_uid;

        public function get_login_url($contest, $app_data) {
            $redirect_url = base_url().'app/fb_login?fb_login=1&fb_page_id='.$contest->fb_page_id;
            
            $login_data = array(
                'canvas'    => 1,
                'fbconnect' => 0,
                'redirect_uri' => $redirect_url,
                'scope' => 'email publish_stream'
            );
            return $this->facebook->getLoginUrl($login_data);
        }

        /**
         * @param      $contest
         * @param      $signed_request
         * @param bool $app_data
         */
        public function auth($contest, $signed_request, $app_data = FALSE) {

            $login_url = $this->get_login_url($contest, $app_data);
            
            $this->facebook_uid = $this->facebook->getUser();

            $this->session->set_userdata('facebook_uid', $this->facebook_uid);

            // if user is not logged in
            if(!$this->facebook_uid){
                redirect($login_url);
            }

            try {
                $this->userdata = $userData = $this->facebook->api('/me');
            }
            catch(Exception $e) {
                header ("Location: ". $login_url);
            }

            if($userData != '')
            {
                $userDbData = $this->user_exists($userData['id']);
               
                if(!$userDbData){
                    $this->users->create_user($userData, $this->facebook->getAccessToken());
                    if($contest->auto_post) {
                        $publishStream = $this->facebook->api("/" . $userData['id'] . "/feed", 'post', array(
                            'message'		=> $contest->message,
                            'link'			=> $contest->app_link,
                            'picture'		=> $contest->picture,
                            'name'			=> $contest->name,
                            'caption'		=> $contest->caption,
                            'description'	=> $contest->description
                        ));
                    }

                }
                else {
                    $option['access_token'] = $this->facebook->getAccessToken();
                    $this->update_user($option, $userDbData->id, $userDbData->uid);
                }
            }
        }


        /**
         * @param $userArray
         * @param $access_token
         * @Description: Create or Update user if exists on register / FB Auth process
         * @return bool
         */
        public function create_user($userArray, $access_token) {
            if($userArray) {

                $user_exist = $this->check_email_exist($userArray['email'], false);

                $userObject['name'] = $userArray['name'];
                $userObject['email'] = $userArray['email'];
                $userObject['uid'] = $userArray['uid'];

                if(!$user_exist){

                    $userObject['access_token'] = $access_token;
                    $userObject['registered_date'] = date('Y-m-d H:i:s');

                    $this->db->insert('users', $userObject);
                    $newID = $this->db->insert_id();

                    return $newID;



Is This A Good Question/Topic? 0
  • +

Replies To: php facebook app not getting facebook ID and inserting into DB

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2247
  • View blog
  • Posts: 6,858
  • Joined: 15-January 14

Re: php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 04:40 PM

Not all of the code is there, but where is the error coming from? How are you getting the information that is missing? What information is there now?
Was This Post Helpful? 0
  • +
  • -

#3 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 04:44 PM

It seems like the error is coming from this part of the code which is supposed to read my profile data
$this->userdata = $userData = $this->facebook->api('/me');


From what I am reading it sounds like this may be depreciated but I don't know how to fix it. It is partly working because In the error the only thing that is NULL is my name and my user ID, its generating a token and my e-mail address.

This code is working correctly to get the access token
$option['access_token'] = $this->facebook->getAccessToken();


This post has been edited by jvanasco: 28 May 2015 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2247
  • View blog
  • Posts: 6,858
  • Joined: 15-January 14

Re: php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 04:55 PM

You can use print_r on that $userData array to check what is being returned.
Was This Post Helpful? 0
  • +
  • -

#5 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 05:08 PM

is there a way I can use print_r and hide the database error that is showing up so I can see what is being printed?
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2247
  • View blog
  • Posts: 6,858
  • Joined: 15-January 14

Re: php facebook app not getting facebook ID and inserting into DB

Posted 28 May 2015 - 05:20 PM

You can use print_r and then exit to stop the script at that point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1