且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发布到粉丝页面的墙上,作为另一个PHP网站的页面

更新时间:2023-02-26 13:09:28

这是一个修改后的脚本,其中解释了API中更改的说明。

 <?php 
// facebook application
$ fbconfig ['userid'] =5332534xx;
$ fbconfig ['appid'] =184874081548xxx;
$ fbconfig ['secret'] =a5aa62bb3a8xxxb98d5d9dbe4a368xxx;
$ fbconfig ['pageid'] =121409594622xxx;

$ fbconfig ['1'= 1 //步骤1 - 由loggin返回
$ fbconfig ['token2'] =AAACoJFn1eLABAC2Q8OLnqxjUSKzdn9CzaXhy8nsG61vzp2ufePr5iwHZA7TM7Ibxxxxxxyf868O04FeBxMrIo0RCumrNaB78hZAp2uRrbVlGVPXP; //步骤3 - 这是一个页面访问令牌,页面
$ fbconfig ['my_ulr'] ='http://'.$_SERVER ['SERVER_NAME'];

include_oncefacebook.php;

//创建我们的Application实例。
$ facebook = new Facebook(array(
'appId'=> $ fbconfig ['appid'],
'secret'=> $ fbconfig ['secret'],
));

// 1.我们需要先获取一个用户access_token(用offline_access的旧方法强制收到的令牌不会过期(这里的好例子 - http://developers.facebook.com/docs/ authentication2 /和http://www.howtobe.pro/tag/graph-api)
//运行该文件并查看步骤1说明。

// offline_access已于5月份弃用2012,因此从以下请求中排除了以下
$ token_url1 =https://www.facebook.com/dialog/oauth?\".\"client_id=\".$fbconfig['appid'].\"&redirect_uri= .urlencode($ fbconfig ['my_ulr'])。& scope = manage_pages,publish_stream& response_type = token;

echo&h2>这个小工作示例应该给你一个工作不要过期的令牌用于您的PHP脚本,使其能够以页面的形式发布到您的Facebook页面,而不是用户< / h2>< br>;
echo1 - 点击下面的链接这将重定向到uri,附带令牌)。以管理员的身份登录您尝试发布的页面,然后复制到肯定你会得到地址栏,以便在步骤2的脚本中使用。< br>;
echo< a href ='。$ token_url1。'target ='_ blank'> $ token_url1< / a>;

// 2。然后将您收到的令牌粘贴到上面的配置部分中的步骤1变量中。登录后再次运行此脚本以接收所有信息。
$ token_url2 =https://graph.facebook.com/me?access_token=\".$fbconfig['token1'];
$ me = json_decode(file_get_contents($ token_url2),true);

// echo< hr>< br>此URL为您提供管理员可以访问的所有页面,但这些不是我们需要以页面形式发布到粉丝页面这只是为了它的... ...< br>;
// echo< a href ='$ token_url2'target ='_ blank'> $ token_url2< / a>;
// echo< hr>这是原始服务器回复< br>;
// echo d($ me ['id'])。< hr>;

// API的新更改 - https://developers.facebook.com/roadmap/offline-access-removal/#extend_token
$ new_token_url2 =https://graph.facebook ?。$运行fbconfig [ 'APPID'] 的.com / OAuth的/的access_token CLIENT_ID =安培; client_secret = $运行fbconfig [ '秘密']。 &安培; grant_type = fb_exchange_token&安培; fb_exchange_token = $运行fbconfig ['TOKEN1 ];
$ new_token2 = file_get_contents($ new_token_url2);
$ vars = explode('&',$ new_token2);
// d($ vars);

echo2.我们现在获得一个持久的令牌(基于< a href ='https://developers.facebook.com/roadmap/offline-access-removal/#extend_token'target ='_ blank'>这个< / a>)
< br>< br>我们发送请求< br>'$ new_token_url2。'< br>< br&是:其中峰; br&GT;' $ new_token2。 '&LT峰; br&GT;&LT峰; br> 中;

//http://developers.facebook.com/tools/explorer?method=GET&path=533253476&accounts&access_token=AAACoJFn1eLABAFZBftV0vtlBiHKA7ZAIrukrriyp2coWSavj3L4CbfJ9r3WY76IPi7pwUgt3wsubaI4iBbsr663PrbNyaLdZAhLxneOLAZDZD

回声所以,现在开放此页面< a href ='http://developers.facebook.com/tools/explorer'target ='_ blank'> http://developers.facebook.com/tools/explorer< / a>
然后将上面的令牌放入访问令牌:字段中,然后按Enter键...您将需要从响应窗口右侧的帐户链接
< br>您将看到另一个响应并从那里复制你的页面访问令牌。将这个任务自动化成一个查询不起作用...我尝试了很多次..返回的令牌不是同样的,你会按照指示一步一步...
这种方法不起作用 - < br>;
echohttp://developers.facebook.com/tools/explorer?method=GET&path=\".$me['id.] \"%2Faccounts&\".$vars[0].\"&lt ; BR&GT;&LT; BR&GT;&LT; BR>中;
echo请检查这里< a href ='http://developers.facebook.com/tools/debug'target ='_ blank'> https://developers.facebook.com/tools/debug&lt ; / a>并确保它永不过期...;


$ pageid = $ fbconfig ['pageid'];
尝试{

//步骤3.运行此脚本WHEN LOGGED IN并将生成的令牌粘贴到上面的步骤3变量中以检查功能

/ * $ page_info = $ facebook-> api(/ $ pageid?fields = access_token&$ new_token2); //错误的方法来使用这个直线。这是我们需要的访问令牌。但是,只有在用户登录后才会有效。所以
echo< hr>这是一个page_info细目;
d($ page_info);
echo,您需要粘贴到fbconfig ['token2']变量的访问令牌是:< br>;
echo $ page_info ['access_token']; * /

$ args = array(
'access_token'=> $ fbconfig ['token2'],//不要尝试在这里插入$ page_info ['access_token']。 ..一旦你注销了Facebook
'message'=>'这是一个测试提要消息'
'link'=>'http://www.test.com ',
'picture'=>'https://www.google.com/intl/en_com/images/srpr/logo3w.png',
'name'=>'测试图片' ,
'description'=>'测试图片的说明!'
);

//在您准备发布后,取消注释,您可以在最后一步看到所有访问令牌。然后注释掉所有的echo和d(),使脚本静默...
// $ post_id = $ facebook-> api(/ $ pageid / feed,post,$ args) ;
echo< hr>这将显示一旦消息被发布 - post_id是:< br>;
d($ post_id);
} catch(FacebookApiException $ e){
error_log($ e);
}

函数d($ d){
echo'< pre>';
print_r($ d);
echo'< / pre>';
}
?>


I know that there are lot's of questons on this, but all seem to be needing a user to be logged in... I have been using code snippets from all possible tutorials, but none seem to work.

Here is the scenario: I have a photo community running on PHP and I have a fan page on Facebook. When an image on the main site collects a certain amount of votes a function is triggered to post the image link to the Facebook wall. The link (post) is posted as a page and not as admin. Admin of course will not be online... Is this even possible to do these days? I have the latest PHP SDK and this is the function that I need to get working in stand alone mode before pluggin' into the main site.

OK. This code works perfectly if I am logged into the Facebook, but if I am not - it will not post... The App has all necessary and unnecessary :) permissions to interact with my page on my (admin) behalf. Any ideas will be appreciated.

    <?php
//facebook application
$fbconfig['appid' ]     = "1848740815xxxxx";
$fbconfig['secret']     = "a5aa62bb3a8ddcb98d5d9dbe4a3xxxxx";
$fbconfig['pageid']     = "121409594622865";

$user  =   null; //facebook user uid
try{
    include_once "facebook.php";
}
catch(Exception $o){
    error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret']
));
//Facebook Authentication part
$user       = $facebook->getUser();
$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope'         => 'offline_access,publish_stream'
            )
        );
$logoutUrl  = $facebook->getLogoutUrl();
$pageid = $fbconfig['pageid'];

if ($user) {
      try {
        $page_info = $facebook->api("/$pageid?fields=access_token");

        if( !empty($page_info['access_token']) ) {
        $args = array(
            'access_token'  => $page_info['access_token'],
            'message' => 'This is a test feed message', 
            'link'    => 'http://www.fotodvor.com',
            'picture' => 'http://www.fotodvor.com/data/media/15/1319971991.jpg',
            'name'    => 'Test Picture',
            'description'=> 'Description of the test picture!'
        );
        $post_id = $facebook->api("/$page_id/feed","post",$args);
    }
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
?>

Thanks in advance

here is a modified script with explanations adjusted for the changes in the API

<?php
//facebook application
$fbconfig['userid']    = "5332534xx";
$fbconfig['appid' ]    = "184874081548xxx";
$fbconfig['secret']    = "a5aa62bb3a8xxxb98d5d9dbe4a368xxx";
$fbconfig['pageid']    = "121409594622xxx";

$fbconfig['token1']     = "AAACoJFn1eLABAKpL9W0nZBrw0e3zzdSNVsTg6FWDMhSnOUeinjid6yAQ2z9JDxxxxxxc1hMHBC3GG18KZBwppGDehWMEwLe56wagZDZD";  // step 1 - returned by loggin in.
$fbconfig['token2']     = "AAACoJFn1eLABAC2Q8OLnqxjUSKzdn9CzaXhy8nsG61vzp2ufePr5iwHZA7TM7Ibxxxxxxyf868O04FeBxMrIo0RCumrNaB78hZAp2uRrbVlGVPXP"; // step 3 - this is a page access token as page
$fbconfig['my_ulr']     = 'http://'.$_SERVER['SERVER_NAME'];

include_once "facebook.php";

// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret'],
));

// 1. we need to get a user access_token first, (old approach with offline_access forces the tokens received not to expire (good examples here - http://developers.facebook.com/docs/authentication2/ and http://www.howtobe.pro/tag/graph-api)
// run the file and see step 1 instructions.

//offline_access has been deprecated in May 2012 and therefore excluded from the request below
$token_url1 = "https://www.facebook.com/dialog/oauth?"."client_id=".$fbconfig['appid']."&redirect_uri=".urlencode($fbconfig['my_ulr'])."&scope=manage_pages,publish_stream&response_type=token";

echo "<h2>This little working example should give you a working non expiring token to use in your PHP script to enable it to post to your Facebook page as a page and not as a user</h2><br>";
echo "1 - Click on the link below (this redirects to uri with token attached). Log in as admin of the page you are trying to post to. Then copy the token you will get in the address bar to be used in the script in step 2.<br>";
echo "<a href='".$token_url1."' target='_blank'>$token_url1</a>";

//2. then paste the token you received into "step 1" variable in the config section above. Run this script again when logged in to receive all info.
$token_url2 = "https://graph.facebook.com/me?access_token=".$fbconfig['token1'];
$me = json_decode(file_get_contents($token_url2), true);

//echo "<hr><br>this URL gives you all pages that you as admin have access to, but these are NOT what we need to post to the fan page as PAGE so this is just for the heck of it...<br>";
//echo "<a href='".$token_url2."' target='_blank'>$token_url2</a>";
//echo "<hr>this is a raw server reply<br>";
//echo d($me['id'])."<hr>";

//new changes to API - https://developers.facebook.com/roadmap/offline-access-removal/#extend_token
$new_token_url2 = "https://graph.facebook.com/oauth/access_token?client_id=".$fbconfig['appid']."&client_secret=".$fbconfig['secret']."&grant_type=fb_exchange_token&fb_exchange_token=".$fbconfig['token1'];
$new_token2 = file_get_contents($new_token_url2);
$vars = explode('&', $new_token2);
//d($vars);

echo "2. We now obtain a long lasting token (based on <a href='https://developers.facebook.com/roadmap/offline-access-removal/#extend_token' target='_blank'>this</a>)
<br><br>We send the request<br>'".$new_token_url2."'<br><br>and the reply is:<br>'".$new_token2."'<br><br>";

//http://developers.facebook.com/tools/explorer?method=GET&path=533253476&accounts&access_token=AAACoJFn1eLABAFZBftV0vtlBiHKA7ZAIrukrriyp2coWSavj3L4CbfJ9r3WY76IPi7pwUgt3wsubaI4iBbsr663PrbNyaLdZAhLxneOLAZDZD

echo"So now open this page <a href='http://developers.facebook.com/tools/explorer' target='_blank'>http://developers.facebook.com/tools/explorer</a>, 
then put the token above to put into the 'Access Token: ' field and press enter... You will need to press 'accounts' link to the right from the response window.
<br>you will see another response and copy your page access token from there. Automating this task into one query did not work... I tried many times.. the token returned IS NOT THE SAME as you would get following the instructions step by step...
This approach does not work - <br>";
echo "http://developers.facebook.com/tools/explorer?method=GET&path=".$me['id']."%2Faccounts&".$vars[0]."<BR><BR><BR>";
echo "Please check it here <a href='http://developers.facebook.com/tools/debug' target='_blank'>https://developers.facebook.com/tools/debug</a> and make sure it never expires...";


$pageid = $fbconfig['pageid'];
try {

//Step 3. Run this script WHEN LOGGED IN to and paste the resulting token into step 3 variable above to check the functionality

/*     $page_info = $facebook->api("/$pageid?fields=access_token&".$new_token2);  //wrong approach to use this straight. THIS is the access token is that we need. BUT this will work only if user is logged in. so 
    echo "<hr>this is a page_info breakdown";
    d($page_info);
    echo "and the access token you needs to paste into fbconfig['token2'] variable is this:<br>";
    echo $page_info['access_token']; */

    $args = array(
            'access_token'  => $fbconfig['token2'], //do not attempt to plug the $page_info['access_token'] here... it will be empty once you log off Facebook
            'message' => 'This is a test feed message', 
            'link'    => 'http://www.test.com',
            'picture' => 'https://www.google.com/intl/en_com/images/srpr/logo3w.png',
            'name'    => 'Test Picture',
            'description'=> 'Description of the test picture!'
        );

         //uncomment this once you are ready to post and you can see all the access token in the last step. Then comment out all echo and d()'s to make the script silent...            
        //$post_id = $facebook->api("/$pageid/feed","post",$args);
        echo "<hr>This will show once the message is posted - post_id is: <br>";
        d($post_id); 
  } catch (FacebookApiException $e) {
    error_log($e);
  }

function d($d){
    echo '<pre>';
    print_r($d);
    echo '</pre>';
}
?>