2012-01-16 3 views
1

필자는 Facebook 이벤트를 원격으로 제출하는 작업 스크립트를 마침내 작성했으며 내 사이트의 이벤트 RSS 피드를 FB 이벤트 데이터로 변환하는 문제를 해결했습니다. RSS2HTML을 활용하여 필자는 템플릿 기반 호출을 추가하여 이벤트 전 이틀 동안 각 이벤트를 보냈습니다. 여기 코드는 : 내 브라우저에서 호출하지만, cron 작업에서 호출 할 때, 나는 rss2html 스크립트에서 "템플릿을 열 수 없습니다"오류가 발생하는 경우Cron Job Struggle의 Facebook 이벤트

 // 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을 사용하여 기본적으로 피드를 호출하는 별도의 스크립트를 작성하여이 문제를 해결할 수있었습니다.

유감스럽게도이 기술은 FaceBook Auth 스크립트에서 작동하지 않습니다. 그 이유는 "상태가 일치하지 않기 때문에 CSRF의 피해자 일 수 있습니다."

그래서 나는 바위와 어려운 곳 사이에 있습니다. cURL 호출없이 rss2html 스크립트를 실행할 수 없으며 cURL 호출은 Facebook 로그인을 방해합니다. Here's 누구나 볼 수 있도록 rss2html 스크립트의 텍스트 버전을 그대로 사용하십시오.

누구나 좋은 해결 방법을 생각할 수 있습니까? https://developers.facebook.com/docs/authentication/에서 페이스 북의 인증 문서 도구를 사용하고, 좀 오프라인 액세스 토큰을 잡을 수 있었다 추가 나의 부름에 & 범위 = offline_access을 " ' , 그리고 thusly 히 내 위의 코드를 변경 : DCMS에

덕분에, 해결책은 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을 그 토큰을 잡아서 붙잡아 라. 그런 다음 그래프 API 호출에 해당 토큰을 사용하십시오.

+1

정확합니다. 난 미래의 사용자를 위해 위의 전체 솔루션을 게시했습니다! –