1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Storing Facebook User Location, Work and Education History Data in MySQL

Discussion in 'Facebook API' started by ramerika, Apr 1, 2013.

  1. #1
    I'm having issues trying to store Facebook users location, work and education history data and I've spent 2 days trying to figure it out without much luck and could use some help. I'm using the up to date Facebook PHP SDK.

    I'm able to save data fields: name, email, birthday and gender but nothing else.

    Below is the code I'm using with my app specifics removed and replaced with generic info:

    if(!isset($_SESSION['user']))
    {
    //Application Configurations
    $app_id    = "myappid";
    $app_secret = "myappsecret";
    $site_url  = "mysiteurl";
     
    try{
        include_once "fbapi/facebook.php";
    }catch(Exception $e){
        error_log($e);
    }
    // Create our application instance
    $facebook = new Facebook(array(
        'appId'    => $app_id,
        'secret'    => $app_secret,
        ));
     
    // Get User ID
    $user = $facebook->getUser();
    // We may or may not have this data based
    // on whether the user is logged in.
    // If we have a $user id here, it means we know
    // the user is logged into
    // Facebook, but we don’t know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    //print_r($user);
    if($user){
        // Get logout URL
        $logoutUrl = $facebook->getLogoutUrl();
    }else{
        // Get login URL
        $loginUrl = $facebook->getLoginUrl(array(
            'scope' => 'email, user_work_history, user_education_history, user_location',
            'redirect_uri'  => $site_url,
            ));
    }
     
    if($user){
     
        try{
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        //Connecting to the database. You would need to make the required changes in the common.php file
        //In the common.php file you would need to add your Hostname, username, password and database name!
        mysqlc();
     
        $name = GetSQLValueString($user_profile['name'], "text");
        $email = GetSQLValueString($user_profile['email'], "text");
        $birthday = GetSQLValueString($user_profile['birthday'], "text");
        $gender = GetSQLValueString($user_profile['gender'], "text");
        $location = GetSQLValueString($user_profile['location'], "text");     
        $query = sprintf("SELECT * FROM mytable WHERE email = %s",$email);
        $res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
        if(mysql_num_rows($res) == 0)
        {
            $iquery = sprintf("INSERT INTO mytable values('',%s,%s,%s,%s,%s)",$name,$email,$birthday,$gender,$location);
            $ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
            $_SESSION['user'] = $user_profile['email'];
            $_SESSION['id'] = $user_profile['id'];
        }
        else
        {
            $row = mysql_fetch_array($res);
            $_SESSION['user'] = $row['email'];
            $_SESSION['id'] = $user_profile['id'];
        }
        }catch(FacebookApiException $e){
                error_log($e);
                $user = NULL;
            }
     
    }
    }
    PHP:
    I did find this set of code but it didn't work to save location info either:

                $facebook_location = $facebook_profile['location'];
                $facebook_city_country = $facebook_location['name'];
                $city_country=explode(',',$facebook_city_country);
                $city=$city_country[0];
                $country=$city_country[1];
    PHP:
    I asked on StackOverview but no response so hoping someone here can help. Any help will be greatly appreciated!
     
    ramerika, Apr 1, 2013 IP
  2. PopaWoody

    PopaWoody Well-Known Member

    Messages:
    314
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Are you asking for the correct permissions? To be able to read certain data sets you may have to request an access token to retrieve them. Not sure if what you are requesting is covered under the access token you get when you make your request.

    Good luck, hope that points you in the right direction.
     
    PopaWoody, Apr 12, 2013 IP
  3. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Yeah I pulled some of the code for permissions straight from Facebook Developers page but I figured it out ... I have to use Facebook FQL instead of Graph API to retrieve the data I'm wanting. When I'm done with it I'll post a generic version for anyone who might need it in the future. BTW Thanks!
     
    ramerika, Apr 12, 2013 IP
  4. radrivan

    radrivan Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #4
    Hello if you need help don't hesitate to pm me on skype "adrivanrex"
     
    radrivan, May 1, 2013 IP