Jump to content

User's demographic data from Facebook


gg39

Recommended Posts

Hello all! I'm new to PHP and MySQL so I'm pretty much clueless as to how to write code. I saw the following script from http://thinkdiff.net/facebook/users-demographic-data-from-facebook/ so I decided to play around with it but I just can't get it to work. I created the table shown in that page but after that, I'm pretty much stuck. So two questions: What do I write to point to my database? And once I do that, what do I write to insert that information into the database? I have no idea what the '$this' part is supposed to do. Thanks for any input!

 

<?php
    include_once "fbmain.php";
    $config['baseurl']  =   "http://thinkdiff.net/demo/newfbconnect1/demographicdata/index.php";

    //if user is logged in and session is valid.
    if ($fbme){
        //collect some data using legacy api
        $param  =   array(
            'method'     => 'users.getinfo',
            'uids'       => $fbme['id'],
            'fields'     => 'birthday_date, interests, locale, political, relationship_status, affiliations',
            'callback'   => ''
        );

        try{
            $info           =   $facebook->api($param);
        }
        catch(Exception $o){
            error_log("Legacy Api Calling Error!");
        }
        //using graph api
        //array data
        $workInfo       =   getWorkInfoAsString($fbme);
        $education      =   getEducationAsString($fbme);

        $moviesArr      =   $facebook->api("/me/movies");
        $musicArr       =   $facebook->api("/me/music");
        $televisionArr  =   $facebook->api("/me/television");

        //format some api data
        $movies         =   getArrayDataAsString($moviesArr['data']);
        $music          =   getArrayDataAsString($musicArr['data']);
        $television     =   getArrayDataAsString($televisionArr['data']);

        //data from legacy api
        $networks       =   '';
        if (!empty($info[0]['affiliations'])){
            $flag       =   true;
            foreach ($info[0]['affiliations'] as $item){
                if (!$flag)  $networks.= ' # ';
                $networks   .=  $item['name'];
                $flag   =   false;
            }
        }

        $now            =   date("Y-m-d G:i:s");
        $insData        =   array(
            'uid'                   =>  $fbme['id'],
            'first_name'            =>  $fbme['first_name'],
            'last_name'             =>  $fbme['last_name'],
            'email'                 =>  isset($fbme['email']) ? $fbme['email'] : '',
            'link'                  =>  $fbme['link'],
            'affiliations'          =>  $networks,
            'birthday'              =>  $info[0]['birthday_date'],
            'current_location'      =>  isset($fbme['location']['name']) ? $fbme['location']['name'] : '',
            'education_history'     =>  $education,
            'work'                  =>  $workInfo,
            'hometown_location'     =>  isset($fbme['hometown']['name']) ? $fbme['hometown']['name'] : '',
            'interests'             =>  $info[0]['interests'],
            'locale'                =>  $info[0]['locale'],
            'movies'                =>  $movies,
            'music'                 =>  $music,
            'political'             =>  $info[0]['political'],
            'relationship_status'   =>  $info[0]['relationship_status'],
            'sex'                   =>  isset($fbme['gender']) ? $fbme['gender'] : '',
            'tv'                    =>  $television,
            'status'                =>  '0',
            'created'               =>  $now,
            'updated'               =>  $now,
        );

        //$this->db->insert('demographic', $insData);
    }

    function getWorkInfoAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['work'])) return '';

        foreach($fbme['work'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag   =   true;
            $info   .=  (isset($item['employer']['name']) ? $item['employer']['name'] : '' ). $delim .
                        (isset($item['location']['name']) ? $item['location']['name'] : '' ). $delim .
                        (isset($item['position'])         ? $item['position']['name'] : '' ). $delim .
                        (isset($item['start_date'])       ? $item['start_date']       : '' ). $delim .
                        (isset($item['end_date'])         ? $item['end_date']         : '' );
        }
        return $info;
    }

    function getEducationAsString($fbme, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        if (empty($fbme['education'])) return '';

        foreach($fbme['education'] as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  (isset($item['school']['name']) ? $item['school']['name'] : '' ). $delim .
                        (isset($item['year']['name'])   ? $item['year']['name']   : '');
        }
        return $info;
    }

    function getArrayDataAsString($data, $delim = '#', $partDelim = ' | '){
        $info       =   "";
        $flag           =   false;
        foreach($data as $item){
            if ($flag)
                $info .= $partDelim;
            $flag    =   true;
            $info   .=  $item['name'];
        }
        return $info;
    }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.