• Facebook RSS page to MODx page#

  • kjell Reply #1, 1 year ago

    Reply
    Got a headache when I got up this code, for most of you know how FB does
    The code works today, but should work with chunks and stylesheet, Is there anyone who feel to fix that?

    You need FACEBOOK_ID, you get it if you check your status image (the url) on Facebook and enter it near the bottom twice in the code.
    The last number in that line is how many posts are shown (3).

    Examples that I mean is:
    <?php
    function fetch_fb_feed($url, $maxnumber) {
         /* The following line is absolutely necessary to read Facebook feeds. Facebook will not recognize PHP as a browser and therefore won't fetch anything. So we define a browser here */
         ini_set('user_agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
    
         $updates = simplexml_load_file($url);  //Load feed with simplexml
    
         $fb_array = array();  //Initialize empty array to store statuses
         foreach ( $updates->channel->item as $fb_update ) {
              if ($maxnumber == 0) {
                   break;
              } else {
                   $desc = $fb_update->description;
    
    
    
                    //Converts UTF-8 into ISO-8859-1 to solve special symbols issues
                  // $desc = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $desc);
    
                    //Get status update time
                   $pubdate = strtotime($fb_update->pubDate);
                   $propertime = gmdate('F jS Y, H:i', $pubdate);  //Customize this to your liking
    
                   //Get link to update
                   $linkback = $fb_update->link;
    
                   //Store values in array
                   $fb_item = array(
                          'desc' => $desc,
                          'date' => $propertime,
                          'link' => $linkback
                   );
                   array_push($fb_array, $fb_item);
    
                   $maxnumber--;
              }          
         }
         //Return array
         return $fb_array;
    }
    
    
    //Run the function with the url and a number as arguments
    $myfb_statuses = fetch_fb_feed('http://www.facebook.com/feeds/page.php?id=FACEBOOK_ID&viewer=FACEBOOK_ID&format=rss20',
     3);
      
    //Print Facebook status updates
    echo '<ul class="fb-updates">';
       foreach ($myfb_statuses as $k => $v) {
          echo '<li>';
          echo '<span class="update">' .$v['desc']. '</span>';
          echo '<span class="date">' .$v['date']. '</span>';
          echo '<span class="link"><a target="_blank" href=http://www.facebook.com'
     .$v['link']. '>Link to status update</a></span>';
          echo '</li>';
       } 
    echo '</ul>';
    ?>


    here can you see my testpage: http://www.modx-verkstan.se/testpage


  • kjell Reply #2, 1 year ago

    Reply
    Has come a bit closer ... but I'm no good at Chunks tpl, probably needs a little help here...

    <?php
    /*
    
    This snippet is only for facebook "pages" at http://www.facebook.com/pages/create.php
    
    
    - Create a new snippet.
    - Call it like FBRSS2.
    - Insert this code in snippet code.
    - Save
    
    
     Fullest use:
     [[RSS2? &fbid=`1234567890` &maxnumber=`3`]]
     
     fbid is same number you have on the facebook page, see the link in the profile picture.
     "http://www.facebook.com/media/set/fbx/?set=pa.1234567890"
    
     
    */
    
    
    //parameters and defaults
    if(!isset($fbid) || $fbid=='')	{return 'No Facebok ID to parse.';}
    if(!isset($maxnumber) || $maxnumber=='')	{$maxnumber = 5;}
    if(!isset($tpl) || !$modx->getChunk($tpl))	{$tpl = '';}
    
    //Run the function with the url and a number as arguments
    $myfb_statuses = fetch_fb_feed('http://www.facebook.com/feeds/page.php?id='.
     $fbid .'&viewer='. $fbid .'&format=rss20', $maxnumber);
    
    
    function fetch_fb_feed($url, $maxnumber) {
         /* The following line is absolutely necessary to read Facebook feeds. Facebook will not recognize PHP as a browser and therefore won't fetch anything. So we define a browser here */
         ini_set('user_agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
    
         $updates = simplexml_load_file($url);  //Load feed with simplexml
    
         $fb_array = array();  //Initialize empty array to store statuses
         foreach ( $updates->channel->item as $fb_update ) {
              if ($maxnumber == 0) {
                   break;
              } else {
                   $desc = $fb_update->description;
    
                   //Add www.facebook.com to hyperlinks
                  // $desc = str_replace('href="', 'href="http://www.facebook.com',
     $desc); 
    
                    //Converts UTF-8 into ISO-8859-1 to solve special symbols issues
                  // $desc = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $desc);
    
                    //Get status update time
                   $pubdate = strtotime($fb_update->pubDate);
                   $propertime = gmdate('F jS Y, H:i', $pubdate);  //Customize this to your liking
    
                   //Get link to update
                   $linkback = $fb_update->link;
    
                   //Store values in array
                   $fb_item = array(
                          'desc' => $desc,
                          'date' => $propertime,
                          'link' => $linkback
                   );
                   array_push($fb_array, $fb_item);
    
                   $maxnumber--;
              }          
         }
         //Return array
         return $fb_array;
    }
    
    
      
    //Print Facebook status updates
    echo '<ul class="fb-updates">';
       foreach ($myfb_statuses as $k => $v) {
          echo '<li>';
          echo '<span class="update">' .$v['desc']. '</span>';
          echo '<span class="date">' .$v['date']. '</span>';
          echo '<span class="link"><a target="_blank" href=http://www.facebook.com'
     .$v['link']. '>Link to status update</a></span>';
          echo '</li>';
       } 
    echo '</ul>';
    ?>
    


  • mconsidine Reply #3, 1 year ago

    Reply
    Here's a different approach based on fetching Twitter feeds with cUrl. Dunno how "legit" it is relative to Facebooks developer API, but I toss it out anyway. It assumes a page is public. And I'm sure a better code than me could come up with a more elegant way of parsing the info returned from curl_exec.
    Hope it helps,
    Matt

    Snippet call :
    [!fetchFB? &fbaccount=`modxcms` &fbnumber=`2`!]


    Snippet :
    <?php
    //fetchFB based on fetchTweets
    //Snippet author : Matt Considine 5/1/2011
    //Based on http://scriptplayground.com/tutorials/php/Twitter-Integration-Class-Using-PHP5-and-cURL/
    
    
    class Facebook
    {
      public $fbentries = array();
      public function __construct($user, $limit = 5)
      {
        //'http://www.facebook.com/feeds/page.php?format=atom10&id='
     . $user;
        //$feed = curl_init('http://www.facebook.com/feeds/page.php?format=atom10&id='
     . $user );
        $feed = curl_init('https://graph.facebook.com/'.$user.'/feed'.'?limit='.$limit
    );
        curl_setopt($feed, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($feed, CURLOPT_HEADER, 0);
        curl_setopt($feed, CURLOPT_SSL_VERIFYPEER, 0);
        $xml = curl_exec($feed);
        //if($xml === false)
        //{
        //    echo 'Curl error: ' . curl_error($feed);
        //}
        curl_close($feed);
    
        $jsonstuff = json_decode($xml);
    
        foreach($jsonstuff as $stuff)
        {
          if (is_array($stuff))
          {
            foreach($stuff as $thing)
            {
              $fbfeed = new stdClass();
              $fbfeed->id = (string) $thing->id;
              $fbfeed->user = (string)$thing->from->name;
              //echo $thing->from->category .'<br />';
              //echo $thing->from->id .'<br />';
              $fbfeed->content = (string)$thing->message;
              //echo $thing->picture .'<br />';
              $fbfeed->permalink = (string)$thing->link;
              //echo $thing->icon .'<br />';
              //echo $thing->type .'<br />';
              //echo $thing->object_id .'<br />';
              //echo $thing->created_time .'<br />';
              $fbfeed->updated = (int) strtotime($thing->updated_time);
    
              if ($thing->likes)
              {
                foreach($thing->likes->data as $item)
                {
                  //echo $item->name .'<br />';
                  //echo $item->id .'<br />';
                }
              }
    
              if ($thing->comments)
              {
                foreach($thing->comments->data as $item)
                {
                  //echo $item->id .'<br />';
                  //echo $item->from->name .'<br />';
                  //echo $item->from->id .'<br />';
                  //echo $item->message .'<br />';
                  //echo $item->created_time .'<br />';
                }
              }
    
              //echo $thing->comments->count .'<br />';
              //echo '<br />';
              array_push($this->fbentries, $fbfeed);
            }
          }
          else
          {
            //echo $stuff->previous .'<br />';
            //echo $stuff->next .'<br />';
          }
        }
    
        unset($feed, $fbfeed, $xml, $jsonstuff, $result, $tweet);
      }
      public function getFBentries() { return $this->fbentries; }
    }
    
    if (!function_exists('humanTiming')) {
    function humanTiming ($origltime)  //MattC from stackoverflow.com
    {
        $timehurdle = 2*3600; //MattC e.g. anything older than 2 hours will just show the timestamp
    
        $time = time() - $origltime; // to get the time since that moment
    
        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );
    
        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            if ($time < $timehurdle)  //MattC
            {
            $timestamp = 'about '.$numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';  //MattC
            }
            else
            {
              //date_default_timezone_set('America/New_York');
              $timestamp = date('M j, Y h:i:s A T',$origltime);  //MattC
            }
            return $timestamp; //MattC
        }
    }
    } //if exists
    
    $feed = new Facebook($fbaccount, $fbnumber);  //change this for feed and number desired
    
    $fbentries = $feed->getFBentries();
    
    $fbcontent = "";
    
    //Print Facebook status updates
    $fbcontent .= '<ul class="fb-updates">';
       foreach ($fbentries as $fbentry) {
          $fbcontent .= '<li>';
          $fbcontent .= '<span class="update">' . $fbentry->content . '</span>';
          $fbcontent .= '<span class="date">' . humanTiming($fbentry->updated) . '</span>';
          $fbcontent .= '<span class="link"><a target="_blank" href="' . $fbentry->permalink . '">Link to status update</a></span>';
          $fbcontent .= '</li>';
       } 
    $fbcontent .= '</ul>';
    
    return $fbcontent;
    ?>
    


  • kjell Reply #4, 1 year ago

    Reply
    Thanks mconsidine for your reply.

    If you run FB with curl, you should first create a "Facebook Application" (API), and I have many FB-sides and to sit down and create that way feels wrong when you can run the RSS directly.


  • skoryk Reply #5, 6 months, 1 week ago

    Reply
    kjell I tried to use your code for extracting facebook feed but there is sometrouble. The page where i use it with snippet call answer me internal server error. Maybe you can help me with it? i`m new in modx and just learn php.