<legend id='vsn7P'><style id='vsn7P'><dir id='vsn7P'><q id='vsn7P'></q></dir></style></legend>
    • <bdo id='vsn7P'></bdo><ul id='vsn7P'></ul>

  • <small id='vsn7P'></small><noframes id='vsn7P'>

  • <i id='vsn7P'><tr id='vsn7P'><dt id='vsn7P'><q id='vsn7P'><span id='vsn7P'><b id='vsn7P'><form id='vsn7P'><ins id='vsn7P'></ins><ul id='vsn7P'></ul><sub id='vsn7P'></sub></form><legend id='vsn7P'></legend><bdo id='vsn7P'><pre id='vsn7P'><center id='vsn7P'></center></pre></bdo></b><th id='vsn7P'></th></span></q></dt></tr></i><div id='vsn7P'><tfoot id='vsn7P'></tfoot><dl id='vsn7P'><fieldset id='vsn7P'></fieldset></dl></div>

      1. <tfoot id='vsn7P'></tfoot>

        Facebook PHP 使用 cronjob 发布到粉丝页面?

        时间:2023-08-19

      2. <small id='dARCs'></small><noframes id='dARCs'>

        <legend id='dARCs'><style id='dARCs'><dir id='dARCs'><q id='dARCs'></q></dir></style></legend>

        • <bdo id='dARCs'></bdo><ul id='dARCs'></ul>
              <tbody id='dARCs'></tbody>

                  <i id='dARCs'><tr id='dARCs'><dt id='dARCs'><q id='dARCs'><span id='dARCs'><b id='dARCs'><form id='dARCs'><ins id='dARCs'></ins><ul id='dARCs'></ul><sub id='dARCs'></sub></form><legend id='dARCs'></legend><bdo id='dARCs'><pre id='dARCs'><center id='dARCs'></center></pre></bdo></b><th id='dARCs'></th></span></q></dt></tr></i><div id='dARCs'><tfoot id='dARCs'></tfoot><dl id='dARCs'><fieldset id='dARCs'></fieldset></dl></div>
                  <tfoot id='dARCs'></tfoot>

                1. 本文介绍了Facebook PHP 使用 cronjob 发布到粉丝页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用以下代码发布到我的 Facebook 粉丝页面,并且运行良好.现在我想使用 cronjob 来发布到 Facebook.我知道我必须用作访问令牌,但我不确定如何设置它.我尝试使用 echo 我自己的访问令牌和页面的访问令牌并在 post api 中使用它,但没有用我收到此错误:

                  I am using the following code to post to my facebook fan page and it is working fine. Now I want to use cronjob in order to post to Facebook. I know I have to use as access token but I am not sure how to set it up. I tried to use echo my own access token and the page's access token and use it in the post api but that did not work I got this error:

                  Uncaught OAuthException: An active access token must be used to query information about the current user.
                  

                  这是我尝试过的代码:

                  require_once('scripts/facebook.php');
                      $config = array('appId' => 'xxx','secret' => 'xxx');
                  
                      $params = array('scope'=>'user_likes,publish_actions,email,offline_access,publish_stream,manage_pages');
                      $facebook = new Facebook($config);
                      $user = $facebook->getUser();
                      if($facebook->getUser()) {
                      try {
                  
                          $user_profile = $facebook->api('/me');
                          $access_token = $facebook->getAccessToken();
                          //echo "1. ".$access_token;
                  
                        } catch(FacebookApiException $e) {
                                          $login_url = $facebook->getLoginUrl($params);
                                          error_log($e->getType());
                                          error_log($e->getMessage());
                        }   
                      } else {
                          $login_url = $facebook->getLoginUrl($params);
                  
                      }    
                  
                  $page_id = "xxxxxxxxxxxxx";
                              $page_access_token = "";
                              $result = $facebook->api("/me/accounts");
                              foreach($result["data"] as $page) {
                                  if($page["id"] == $page_id) {
                                  $page_access_token = $page["access_token"];
                                  //echo '<br>';
                                  //echo "2. ".$page_access_token;
                                  break;
                                      }
                                  }
                  
                  
                          $args = array(
                              'access_token'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                              'message'       => stripslashes($image_caption).$animaged_gif,
                              'name' => stripslashes($image_caption).$animaged_gif,
                              'link' => "http://www.example.com/images.php?i=".$image_name,
                              'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
                              'actions' => array(
                              'name' => 'See Pic',
                              'link' => "http://www.example.com/images.php?i=".$image_name
                              )
                  
                          );
                         $post = $facebook->api("/$page_id/feed","post",$args);
                  

                  如您所见,我尝试使用我在回显自己的访问令牌时获得的实际访问令牌.但这没有用.我还使用了页面的访问令牌,但这也不起作用.你能告诉我我在这里遗漏了什么,或者这样做的正确方法是什么?经过进一步的搜索,我发现了 setAccessToken$page_info = $fb->api("/".$sInfo['pageId']."?fields=access_token"); 但是关于如何使用它们的资源非常有限.我什至不确定这些是否适合这个问题.我只需要知道我需要使用哪个访问令牌以及设置它的适当代码是什么?

                  As you can see that I tried to use the actual access token that I got when I echo out my own access token. But that did not work. I also used the page's access token, but that also did not work. Can you please tell what I am missing here or what is the proper way to do this ? After some further search I did, I came across setAccessToken and $page_info = $fb->api("/".$sInfo['pageId']."?fields=access_token"); but there is very limited resources on how to use them. I am not even sure if those will be appropriate to this problem. All I need to know is which access token I need to use and what is the appropriate code to set it up ?

                  推荐答案

                  你可以这样做:

                  1. 获取一个短期访问令牌.
                  2. 扩展它.
                  3. 获取长期存在的页面访问令牌.(没关系,这里我们每次都会获取新的访问令牌.不那么复杂,也更可靠.)

                  我以为我会使用算法,但我添加了所有可能的代码和文档,因为这也可能对其他人有所帮助.

                  Thought I would go with an algo, instead I am adding all the possible codes and documentations since this may help someone else too.

                  获取短期访问令牌并对其进行扩展:

                  Getting short lived access token and extending it:

                  fetchtoken.php

                  <?php
                  
                  //read more : https://developers.facebook.com/docs/howtos/login/server-side-login/
                  session_start();
                  $app_id = "xxxxxxxxxxxxxx";
                  $app_secret = "xxxxxxxxxxxxxxxx";
                  $my_url = "www.stackoverflow.com/";  // redirect url
                  
                  $code = $_REQUEST["code"];
                  
                  if(empty($code)) {
                      // Redirect to Login Dialog
                      $_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=publish_stream,read_friendlists,email";
                  
                  
                      echo("<script> top.location.href='" . $dialog_url . "'</script>");
                  }
                  if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['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;
                  
                       $response = file_get_contents($token_url);
                       $params = null;
                       parse_str($response, $params);
                       $longtoken=$params['access_token'];
                  
                  
                  //save it to database   
                  } 
                  ?>
                  

                  因此,您现在的数据库、文本文件或其他任何内容中都已准备好一个长期存在的访问令牌.

                  So you now have a long lived accesstoken ready in your database, a text file or whatever.

                  cronjob.php

                  <?
                  require_once('scripts/facebook.php');
                  // Pull access token from the file/db
                  $access_token = "access token from file or db";
                  $facebook->setAccessToken($access_token); // sets our access token as the access token when we call something using the SDK, which we are going to do now.
                  
                  $config = array('appId' => 'xxx','secret' => 'xxx');
                  
                  $params = array('scope'=>'user_likes,publish_actions,email,publish_stream,manage_pages');
                  $facebook = new Facebook($config);
                  $user = $facebook->getUser();
                  if($facebook->getUser()) {
                      try {
                  
                          $user_profile = $facebook->api('/me');
                      } catch(FacebookApiException $e) {
                          $login_url = $facebook->getLoginUrl($params);
                          error_log($e->getType());
                          error_log($e->getMessage());
                      }   
                  }
                  else {
                      $login_url = $facebook->getLoginUrl($params);
                  }    
                  
                  $page_id = "xxxxxxxxxxxxx";
                  $page_access_token = "";
                  $result = $facebook->api("/me/accounts");
                  foreach($result["data"] as $page) {
                      if($page["id"] == $page_id) {
                          $page_access_token = $page["access_token"];
                          //echo '<br>';
                          //echo "2. ".$page_access_token;
                          break;
                      }
                  }
                  
                  $args = array(
                      'access_token'  => $page_access_token,
                      'message'       => stripslashes($image_caption).$animaged_gif,
                      'name' => stripslashes($image_caption).$animaged_gif,
                      'link' => "http://www.example.com/images.php?i=".$image_name,
                      'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
                      'actions' => array(
                              'name' => 'See Pic',
                               'link' => "http://www.example.com/images.php?i=".$image_name
                      )
                  );
                  $post = $facebook->api("/$page_id/feed","post",$args);
                  ?>
                  

                  确保不要在脚本中再次抓取 access_token(user access token/$access_token).

                  此外,$args 中的 access_token 作为页面访问令牌应该由 $page_access_token 变量插入,而不是手动插入.

                  Also, the access_token in $args which is the page access token should be inserted by the $page_access_token variable, and not manually.

                  在范围内设置 offline_access 只会在权限对话框中显示他们可以随时访问他们的数据,而无需通知即可完成.

                  Having offline_access in the scope does nothing than showing them on the permission dialogue box that their data can be accessed any time which can be done without that notification anyway.

                  只要您在开始时访问 fetch.php 一次,并且每 60 天手动访问一次,这应该有效.

                  This should work as long as you visit the fetch.php once at the start, and once every 60 days manually.

                  告诉我.

                  这篇关于Facebook PHP 使用 cronjob 发布到粉丝页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 $_GET 参数传递给 cron 作业 下一篇:如何使用 PHP 以编程方式检查有效(非死)链接?

                  相关文章

                    <bdo id='w1S3W'></bdo><ul id='w1S3W'></ul>

                  <legend id='w1S3W'><style id='w1S3W'><dir id='w1S3W'><q id='w1S3W'></q></dir></style></legend>

                  1. <small id='w1S3W'></small><noframes id='w1S3W'>

                    1. <i id='w1S3W'><tr id='w1S3W'><dt id='w1S3W'><q id='w1S3W'><span id='w1S3W'><b id='w1S3W'><form id='w1S3W'><ins id='w1S3W'></ins><ul id='w1S3W'></ul><sub id='w1S3W'></sub></form><legend id='w1S3W'></legend><bdo id='w1S3W'><pre id='w1S3W'><center id='w1S3W'></center></pre></bdo></b><th id='w1S3W'></th></span></q></dt></tr></i><div id='w1S3W'><tfoot id='w1S3W'></tfoot><dl id='w1S3W'><fieldset id='w1S3W'></fieldset></dl></div>
                    2. <tfoot id='w1S3W'></tfoot>