2012-01-16 8 views
1

私は最終的にFacebookのイベントを遠隔から提出する作業スクリプトを用意しています。私のサイトのイベントRSSフィードをFBイベントデータに変換する問題に取り組んでいます。 RSS2HTMLを利用して、テンプレートベースのコールを追加して、イベントの2日前に各イベントを送信しました。ここでは、コードがあります:Facebookのイベントby Cron Job Struggle

 // 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へ

おかげで、解決策は以下のようなものを行ってきました:https://developers.facebook.com/docs/authentication/でFacebookの認証ドキュメントを使用して 、私のコールに「&スコープ= offline_access」を追加することを、私はいくつかのオフラインアクセスのトークンをつかむことができた、とthusly私の上記のコードを変更しました:

//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そのトークンをつかみ、それを押さえてください。次に、そのトークンをGraph API呼び出しに使用します。

+1

あなたは正しいですよ!私は、将来のユーザーのための上記の完全なソリューションを投稿しました! –

関連する問題