2012-01-16 37 views
1

我終於有一個工作腳本遠程提交Facebook活動,並已完成將我的網站的事件RSS源轉換爲FB事件數據的問題。利用RSS2HTML,我添加了一個基於模板的調用,在事件發生前兩天發送每個事件。下面的代碼:Facebook的活動由Cron工作奮鬥

 // Post Today's Game 
     if (strstr($template, "~~~TwitterToday~~~")) 
       {        
       //Build Arrays for games (when there are more than one per day... 
       $name = array('name'); 
       $desc = array('description'); 
       $venue = array('location'); 
       $s_time = array('start_time'); 
       $e_time = array('end_time'); 
       $pic = array('picture'); 
       $priv = array('privacy'); 
       //Build Main Facebook Array for All games to draw from 
       $fbook = array(
       $name, 
       $desc, 
       $venue, 
       $s_time, 
       $e_time, 
       $pic, 
       $priv, 
       ); 

     $template = str_replace("~~~TwitterToday~~~", "", $template); 
       $mycount = 1; 

      for ($y = 1; $y < count($rss_parser->Items)+1; $y++) //come back through events 
          { 
          //find each event's information to look for today's 
          $gamedate = date('n/j/Y', $rss_parser->Items[$y]->pubDate_t); 
          $todaysdate = date('n/j/Y');  
          $tomorrowsdate = date('n/j/Y',mktime(0,0,0,date('m'), date('d')+1, date('Y'))); 
          $gametime = date('Y-m-d H:i:s',$rss_parser->Items[$y]->pubDate_t); 
          $title = $rss_parser->Items[$y]->title; 
          $description = $rss_parser->Items[$y]->description; 

         if ($gamedate == $tomorrowsdate) //found it 
           { 
           $mycount++; 

           //Fill the arrays 
           $name[] = $title; 
           $desc[] = $description; 
           $venue[] = "Home"; 
           $s_time[] = $gametime; 
           $e_time[] = ""; 
           $pic[] = ""; 
           $priv[] = "OPEN";  
           } 
       } // end $y loop 

       //Populate Main Facebook Array 
       $fbook[0] = $name; 
       $fbook[1] = $desc; 
       $fbook[2] = $venue; 
       $fbook[3] = $s_time; 
       $fbook[4] = $e_time; 
       $fbook[5] = $pic; 
       $fbook[6] = $priv; 

       // Let's run with it   
       if (strpos($title,"Special Event") === false) 
        { 
        $page_id = "xxxxxxxxxxxxxx"; //First Page Id 
        } 
       else 
        { 
       $page_id = "xxxxxxxxxxxxxxxxxxx"; //Special Event Page Id 
        } 
       $app_id = "xxxxxxxxxxxxx"; 
       $app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
       $my_url = "http://mydomain.com/feeds/rss2html.php"; // URL to THIS script 

       //Going to get the PAGE access code 
       //First to get USER Access Code 
       session_start(); 
       $code = $_REQUEST["code"]; 

       if (empty($code)) 
        { 
         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection 
         $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state'] . "&scope=create_event&scope=manage_pages"; 
         echo("<script> top.location.href='" . $dialog_url . "'</script>"); 
         } 

       if ($_REQUEST['state'] == $_SESSION['state']) 
        { 
        $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code; 
        $access_token = @file_get_contents($token_url); 
        $params = null; 
        parse_str($access_token, $params); 

        $graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token']; 
        $user = json_decode(file_get_contents($graph_url)); 
         } 
       else 
        { 
        echo("The state does not match. You may be a victim of CSRF."); 
         } 
      //Now, getting the PAGE Access token, using the user access token 
       $page_token_url = "https://graph.facebook.com/" . $page_id . "?fields=access_token&" . $access_token; 
       $response = file_get_contents($page_token_url); 

       // Parse the return value and get the Page access token 
       $resp_obj = json_decode($response,true); 
       $page_access_token = $resp_obj['access_token']; 

       for ($s = 1; $s < $mycount+1; $s++) 
         {           
         //Let's go post it up! 
         $url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $page_access_token; 

         $params = array(); 
         // Prepare Event fields    
         $params = array(
           'name' => $fbook[0][$s], 
           'description' => $fbook[1][$s], 
           'location' => $fbook[2][$s], 
           'start_time' => $fbook[3][$s], 
//        'end_time' => $fbook[4][$s], //These need to be excluded if they are empty 
//        'picture' => $fbook[5][$s], 
           'privacy' => $fbook[6][$s], 
           ); 

         // Start the Graph API call 
         $ch = curl_init(); 
         curl_setopt($ch, CURLOPT_URL,$url); 
         curl_setopt($ch, CURLOPT_POST, true); 
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
         $result = curl_exec($ch); 
         $decoded = json_decode($result, true); 
         curl_close($ch); 
         if (is_array($decoded) && isset($decoded['id'])) 
          { 
          $msg = "Event created successfully: {$decoded['id']}"; 
          } 
         echo '<hr />' . $msg; 
         } 
        /* End FaceBook Code */ 
        } 

該腳本可以創造奇蹟,當我把它從我的瀏覽器,但是從cron作業調用它時,我在rss2html腳本得到一個「無法打開模板」的錯誤。在過去,我一直能夠通過爲cron作業創建一個單獨的腳本來解決這個問題,本質上使用cURL來調用該提要,並且它可以創造奇蹟。

不幸的是,這項技術不適用於FaceBook Auth腳本,因爲它會返回「狀態不匹配,您可能是CSRF的犧牲品」。

所以,我在一塊石頭和一塊硬地之間。如果沒有cURL調用,則無法運行rss2html腳本,並且cURL調用會阻止Facebook登錄。 Here's rss2html腳本的文本版本,以防萬一任何人想看到它。

任何人都可以想出一個很好的解決方法嗎?

由於DCMS,解去正是如此: 使用Facebook的身份驗證文件在https://developers.facebook.com/docs/authentication/和添加'&範圍= offline_access」我的電話,我能夠抓住一些離線訪問令牌,正是如此改變了我上面的代碼:

//Going to get the PAGE access code 
       //First to get USER Access Code 
       session_start(); 

       for ($s = 1; $s < $mycount+1; $s++) 
         {           
         //Let's go post it up! 
         $url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $page_access_token; 

         $params = array(); 
         // Prepare Event fields    
         $params = array(
           'name' => $fbook[0][$s], 
           'description' => $fbook[1][$s], 
           'location' => $fbook[2][$s], 
           'start_time' => $fbook[3][$s], 
//        'end_time' => $fbook[4][$s], //These need to be excluded if they are empty 
//        'picture' => $fbook[5][$s], 
           'privacy' => $fbook[6][$s], 
           ); 

         // Start the Graph API call 
         $ch = curl_init(); 
         curl_setopt($ch, CURLOPT_URL,$url); 
         curl_setopt($ch, CURLOPT_POST, true); 
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
         $result = curl_exec($ch); 
         $decoded = json_decode($result, true); 
         curl_close($ch); 
         if (is_array($decoded) && isset($decoded['id'])) 
          { 
          $msg = "Event created successfully: {$decoded['id']}"; 
          } 
         echo '<hr />' . $msg; 
         } 
        /* End FaceBook Code */ 
        } 

感謝您的幫助,我希望這可以幫助任何人在未來同樣的問題一起走!

回答

2

您的解決方案可能是存儲訪問令牌。請求offline_access一nd抓住這個標記並堅持下去。然後將該標記用於Graph API調用。

+1

你是對的,先生!我已經爲未來的用戶發佈了完整的解決方案! – 2012-01-17 14:44:39