如何使用 PHP 发布到 Facebook 页面

发布于 2025-01-04 11:15:24 字数 1467 浏览 0 评论 0原文

我想使用 PHP 从我的网站发布到我自己的 Facebook 页面墙。

我有以下内容:

  • Facebook 应用程序,带有 AppID、AppSecret、ApiKey
  • Facebook 页面,带有 PageID
  • 我自己的 Facebook 帐户 - 我是管理员,也是上述应用程序和页面的创建者。

例如,我写了一篇博客文章,我想在我的 Facebook 页面墙上获取名称、简短描述和图片。或者我想每天在 Facebook 页面上自动发布一些文章作为 cron 作业。

您能否提供如何实现此目标的分步教程?

我读过这篇关于 Facebook 登录的文章:
https://developers.facebook.com/docs/facebook-login/
但我仍然不知道在我的代码中写什么。


更新 1

这是我发送应用程序访问令牌请求的方式:

$url = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='
        .Yii::app()->params['FacebookAppID']
        .'&client_secret='
        .Yii::app()->params['FacebookSecret'];

响应类似于此(假符号):

access_token=326584076429|ax3-D39YbpDcR9rMRQn_fMvNu_s

什么access_token 是它?应用程序访问令牌?如何获取用户访问令牌?

我尝试使用此处的访问令牌:
https://developers.facebook.com/tools/explorer?method =GET&path=me%2Faccounts 但我收到以下错误消息:

必须使用活动访问令牌来查询有关当前用户的信息

那么我应该如何获取正确的访问令牌呢?

更新 2:

如何在没有任何客户端交互的情况下在我的应用程序中获取正确的 Facebook 令牌?
我是 Facebook 应用程序和 Facebook 页面的管理员和创建者。

I would like to post to my own Facebook page's wall from my website using PHP.

I have the following:

  • Facebook Application with AppID, AppSecret, ApiKey
  • Facebook Page with PageID
  • my own Facebook account - I'm the admin and the creator of the application and page mentioned above.

E.g. I write a blog post, and I'd like to get the name, the short description and a picture on my Facebook page's wall. Or I would like to publish some articles on the Facebook page every day automatically as a cron job.

Could you provide a step-by-step tutorial how to accomplish this?

I've read this article about Facebook Login:
https://developers.facebook.com/docs/facebook-login/
but I still don't know what to write in my code.


UPDATE 1

This is how I send a request for an App Access Token:

$url = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='
        .Yii::app()->params['FacebookAppID']
        .'&client_secret='
        .Yii::app()->params['FacebookSecret'];

The response is similar to this (fake symbols):

access_token=326584076429|ax3-D39YbpDcR9rMRQn_fMvNu_s

What access_token is it? Application Access Token? How to get a User Access Token?

I tried to use the access token from here:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts
but I got the following error message:

An active access token must be used to query information about the current user

So how should I obtain the right access token?

UPDATE 2:

How can I get the right Facebook tokens in my application without any client interaction?
I'm the admin and the creator of the Facebook Application and the Facebook Page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2025-01-11 11:15:24

逐步

  1. 作为页面管理员的用户(您自己)进行身份验证,
  2. 请求扩展访问令牌(在offline_access 消失后获得 60 天的有效期)。请参阅https://developers.facebook.com/docs/offline-access-deprecation/< /a>
  3. 调用 Graph API me/accounts 并搜索结果列表以找到您感兴趣的页面
  4. 从页面中获取页面访问令牌并开始使用它来调用 post
  5. 它可能可以获得扩展访问令牌如步骤 2 中所述的页面访问令牌,请尝试告诉我们是否也可以对页面访问令牌执行此操作。

您可以在 https://developers.facebook.com/tools/explorer 上尝试上述内容

。编码!

编辑

要为任何用户获取无需对话框的访问令牌,您可以使用https: //developers.facebook.com/tools/access_token/ 获取访问令牌。

Step by step

  1. Authenticate a user that is a page admin (yourself)
  2. Request an extended access token (to get a 60 day variety as offline_access is gone). See https://developers.facebook.com/docs/offline-access-deprecation/
  3. Call Graph API me/accounts and search thru the resulting list to find the page you're interested in
  4. Take the page access token from the page and start using that for the calls to post
  5. It might be possible to get an extended access token for a page access token like described in step 2, please try and let us know if that can be done for page access token too.

You can experiment with the above at https://developers.facebook.com/tools/explorer

Happy Coding!

EDIT

For getting an access token without dialogs for any user, you can use https://developers.facebook.com/tools/access_token/ to get an access token.

梦在夏天 2025-01-11 11:15:24

步骤:

  1. 请求管理页面权限(允许此过程):

    https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages&response_type=token
    
  2. 从 URL 获取访问令牌:
    如果管理员允许此权限。您应该被重定向到以下 URL:

    http://YOUR_URL/#access_token=AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD&expires_in=0
    

    在 URL 中使用访问令牌,您应该得到:

    <预><代码>AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD

  3. 使用 Graph API 检查访问令牌:

    https://graph.facebook.com/me/accounts?access_token=TOKEN_FROM_ABOVE
    

    连接将返回每个页面的信息和访问令牌。

  4. 在您的代码中实现它:
    您可以在调用 Facebook Graph API 方法时使用应用程序访问令牌。

更新:
如果您想在 Facebook SDK 中使用 API 方法,已弃用 REST API 或 FQL 查询...

您必须这样使用 users_accesstoken

  1. 方法一:
    使用您的帐户或用户以offline_access权限登录您的Facebook页面,并在登录成功时使用$facebook->getAccessToken()获取access_token,并将其保存在数据库中以便您随时使用。< br>
    您可以在此处检查令牌的过期时间,令牌带有 offline_access 权限永远不会过期,除非用户更改密码或其他任何内容。

  2. 方法2:
    您可以使用以下代码动态更新您的 access_token(告别过期令牌)。 Facebook 在此处展示了此解决方案,它是执行 FQL 查询的示例代码:

代码:

<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];

//auth user
if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");    
}

//get user access_token
$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);   

Steps:

  1. Request For manage_pages permission ( Allow this process ) :

    https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages&response_type=token
    
  2. Get Access token from URL :
    If the administrator allows this permission. You should be redirected to URL below:

    http://YOUR_URL/#access_token=AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD&expires_in=0
    

    Use Access token in the URL and you should get this:

    AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD
    
  3. Check Access token using Graph API:

    https://graph.facebook.com/me/accounts?access_token=TOKEN_FROM_ABOVE
    

    Connection will return information and access token for each page.

  4. Implement it in your code:
    You can use App access token while call a Facebook Graph API method.

Update:
If you want use API method in Facebook SDK, DEPRECATED REST API or FQL Query...

You have to use users_accesstoken this way:

  1. Method 1:
    Use your account or users to login to your Facebook page with offline_access permissions and grab access_token while login success using $facebook->getAccessToken(), and save it in database so you can use it anytime.
    You can check the expiration time of the token here, token with offline_access permissions never expire except when the user changes his password or maybe anything else.

  2. Method 2:
    You can update your access_token dynamically using the code below (say goodbye to expire token). Facebook shows this solution here, it's a sample code for executing an FQL Query:

Code:

<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];

//auth user
if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");    
}

//get user access_token
$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);   
友谊不毕业 2025-01-11 11:15:24

尝试这个简单的功能来张贴到墙上:

function doWallPost($postName = '', $postMessage = '', $postLink = '', $postCaption = '', $postDescription = '') {
  $FB_APP_ID     = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  $FB_APP_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

  $APP_RETURN_URL = ((substr($_SERVER['SERVER_PROTOCOL'], 0, 4) == "HTTP") ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];

  $code = $_REQUEST["code"];

  if (empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $FB_APP_ID . "&redirect_uri=" . $APP_RETURN_URL . "&scope=publish_stream";
    header("Location:$dialog_url");
  }

  $token_url    = "https://graph.facebook.com/oauth/access_token?client_id=" . $FB_APP_ID . "&redirect_uri=" . urlencode($APP_RETURN_URL) . "&client_secret=" . $FB_APP_SECRET . "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $param1          = explode("&", $access_token);
  $param2          = explode("=", $param1[0]);
  $FB_ACCESS_TOKEN = $param2[1];


  $url        = "https://graph.facebook.com/me/feed";
  $attachment = array(
    'access_token' => $FB_ACCESS_TOKEN,
    'name' => $postName,
    'link' => $postLink,
    'description' => $postDescription,
    'message' => $postMessage,
    'caption' => $postCaption
  );

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
  $result = curl_exec($ch);
  header('Content-type:text/html');
  curl_close($ch);

  return $result;
}

Try this simple function to post onto a wall:

function doWallPost($postName = '', $postMessage = '', $postLink = '', $postCaption = '', $postDescription = '') {
  $FB_APP_ID     = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  $FB_APP_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

  $APP_RETURN_URL = ((substr($_SERVER['SERVER_PROTOCOL'], 0, 4) == "HTTP") ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];

  $code = $_REQUEST["code"];

  if (empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $FB_APP_ID . "&redirect_uri=" . $APP_RETURN_URL . "&scope=publish_stream";
    header("Location:$dialog_url");
  }

  $token_url    = "https://graph.facebook.com/oauth/access_token?client_id=" . $FB_APP_ID . "&redirect_uri=" . urlencode($APP_RETURN_URL) . "&client_secret=" . $FB_APP_SECRET . "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $param1          = explode("&", $access_token);
  $param2          = explode("=", $param1[0]);
  $FB_ACCESS_TOKEN = $param2[1];


  $url        = "https://graph.facebook.com/me/feed";
  $attachment = array(
    'access_token' => $FB_ACCESS_TOKEN,
    'name' => $postName,
    'link' => $postLink,
    'description' => $postDescription,
    'message' => $postMessage,
    'caption' => $postCaption
  );

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
  $result = curl_exec($ch);
  header('Content-type:text/html');
  curl_close($ch);

  return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文