События в Facebook от Cron Job Struggle - PullRequest
1 голос
/ 16 января 2012

Наконец-то у меня есть рабочий скрипт для удаленной отправки событий Facebook, и я решил проблему преобразования RSS-канала событий моего сайта в данные FB Events. Используя RSS2HTML, я добавил вызов на основе шаблона для отправки каждого события за два дня до события. Вот код:

     // 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».

Итак, я между молотом и наковальней. Невозможно запустить скрипт rss2html без вызова cURL, а вызов cURL препятствует входу в Facebook. Вот текстовая версия скрипта rss2html в том виде, в каком он есть, на тот случай, если кто-то захочет его увидеть.

Кто-нибудь может придумать хороший обходной путь в одну или другую сторону?

Благодаря DCMS решение пошло так: Используя документы аутентификации Facebook по номеру https://developers.facebook.com/docs/authentication/ и добавив к своему вызову '& scope = 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 */
                    }

Спасибо за помощь, и я надеюсь, что это поможет любому в будущем решить эту проблему!

1 Ответ

2 голосов
/ 17 января 2012

Возможно, вы решили сохранить токен доступа. Запросите offline_access и возьмите этот жетон и держите его. Затем используйте этот токен для вызовов Graph API.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...